mower-data-collection/run_order/api.py

42 lines
1.3 KiB
Python
Raw Normal View History

2023-12-13 11:58:38 +08:00
from ninja import Schema
from ninja_extra import NinjaExtraAPI, api_controller, route
from ninja_extra.exceptions import APIException
from uuid import UUID
from .models import RunOrder, MowerVersion, AnonymousUser
class RunOrderSchema(Schema):
version: str
uuid: UUID
level: int
skill: str
grandet: bool
@api_controller()
class RunOrderAPI:
@route.post("/run-order")
def run_order(self, data: RunOrderSchema):
if data.level < 1 or data.level > 3:
raise APIException("贸易站等级错误")
2023-12-13 12:53:00 +08:00
if data.skill not in [i[0] for i in RunOrder.SKILL_CHOICES]:
2023-12-13 11:58:38 +08:00
raise APIException("跑单技能错误")
2023-12-13 12:53:00 +08:00
if data.level != 3 and data.skill == RunOrder.Tequila:
2023-12-13 11:58:38 +08:00
raise APIException("跑单技能与贸易站等级不匹配")
version = MowerVersion.objects.get_or_create(version=data.version)[0]
anonymous = AnonymousUser.objects.get_or_create(uuid=data.uuid)[0]
run_order_data = RunOrder.objects.create(
version=version,
uuid=anonymous,
facility_level=data.level,
skill=data.skill,
grandet_mode=data.grandet,
)
2023-12-13 13:11:28 +08:00
return {"message": "上报成功", "id": run_order_data.id}
2023-12-13 11:58:38 +08:00
api = NinjaExtraAPI()
api.register_controllers(RunOrderAPI)