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("贸易站等级错误") if data.skill not in [i[0] for i in RunOrder.SKILL_CHOICES]: raise APIException("跑单技能错误") if data.level != 3 and data.skill == RunOrder.Tequila: 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, ) return run_order_data.id api = NinjaExtraAPI() api.register_controllers(RunOrderAPI)