|
| 1 | +import json |
| 2 | +from ..BaseHandlers import BaseHandler |
| 3 | +from libs.SecurityDecorators import apikey, restrict_ip_address |
| 4 | +from models.Box import Box |
| 5 | +from models.Corporation import Corporation |
| 6 | +from models.GameLevel import GameLevel |
| 7 | +import logging |
| 8 | +from models import dbsession |
| 9 | + |
| 10 | +logger = logging.getLogger() |
| 11 | + |
| 12 | + |
| 13 | +class BoxApiHandler(BaseHandler): |
| 14 | + |
| 15 | + @apikey |
| 16 | + @restrict_ip_address |
| 17 | + def get(self, id=None): |
| 18 | + if id is None or id == "": |
| 19 | + data = {"data": [box.to_dict() for box in Box.all()]} |
| 20 | + else: |
| 21 | + box = Box.by_id(id) |
| 22 | + if box is not None: |
| 23 | + data = {"data": box.to_dict()} |
| 24 | + else: |
| 25 | + data = {"message": "Box not found"} |
| 26 | + self.write(json.dumps(data)) |
| 27 | + |
| 28 | + @apikey |
| 29 | + @restrict_ip_address |
| 30 | + def post(self, *args, **kwargs): |
| 31 | + data = json.loads(self.request.body) |
| 32 | + logger.info(f"Posted data : {data}") |
| 33 | + if "corporation" not in data: |
| 34 | + data = { |
| 35 | + "data": {"corporation": None}, |
| 36 | + "message": "Corporation is required", |
| 37 | + } |
| 38 | + self.write(json.dumps(data)) |
| 39 | + return |
| 40 | + if "name" not in data: |
| 41 | + data = {"data": {"box": None}, "message": "Name is required"} |
| 42 | + self.write(json.dumps(data)) |
| 43 | + return |
| 44 | + |
| 45 | + if Box.by_name(data["name"]) is not None: |
| 46 | + data = { |
| 47 | + "data": {"box": None}, |
| 48 | + "message": "This box already exists", |
| 49 | + } |
| 50 | + self.write(json.dumps(data)) |
| 51 | + return |
| 52 | + |
| 53 | + if Corporation.by_name(data["corporation"]) is None: |
| 54 | + data = { |
| 55 | + "data": {"corporation": data["corporation"]}, |
| 56 | + "message": "This corporation does not exist", |
| 57 | + } |
| 58 | + self.write(json.dumps(data)) |
| 59 | + return |
| 60 | + |
| 61 | + new_box = Box() |
| 62 | + new_box.name = data["name"] |
| 63 | + new_box.description = data["description"] if "description" in data else "" |
| 64 | + new_box.corporation_id = ( |
| 65 | + Corporation.by_name(data["corporation"]).id |
| 66 | + if "corporation" in data |
| 67 | + else None |
| 68 | + ) |
| 69 | + |
| 70 | + flag_submission_type = ( |
| 71 | + data["flag_submission_type"] |
| 72 | + if "flag_submission_type" in data |
| 73 | + else "CLASSIC" |
| 74 | + ) |
| 75 | + if flag_submission_type not in ["CLASSIC", "TOKEN"]: |
| 76 | + new_box.flag_submission_type = "CLASSIC" |
| 77 | + else: |
| 78 | + new_box.flag_submission_type = flag_submission_type |
| 79 | + |
| 80 | + new_box.game_level_id = GameLevel.by_number(0).id |
| 81 | + |
| 82 | + dbsession.add(new_box) |
| 83 | + dbsession.commit() |
| 84 | + data = { |
| 85 | + "data": {"box": new_box.to_dict()}, |
| 86 | + "message": "This box has been created", |
| 87 | + } |
| 88 | + self.write(json.dumps(data)) |
| 89 | + |
| 90 | + @apikey |
| 91 | + @restrict_ip_address |
| 92 | + def delete(self, id: str): |
| 93 | + raise NotImplementedError() |
| 94 | + |
| 95 | + @apikey |
| 96 | + @restrict_ip_address |
| 97 | + def put(self, *args, **kwargs): |
| 98 | + raise NotImplementedError() |
| 99 | + |
| 100 | + def check_xsrf_cookie(self): |
| 101 | + pass |
0 commit comments