def __init__(self, app, db_folder: str): self.app = app self.app.config['UPLOAD_FOLDER'] = db_folder self.db_folder = db_folder self.competitions = {} self.url_endpoints = [ ["/competition/existing", "existing_competitions", API.existing_competitions, ["GET"]], ["/competition/new", "new_competition", self.new_competition, ["POST"]], ["/competition/<string:competition_name>/update", "update_competition", self.update_competition_info, ["POST"]], ["/competition/<string:competition_name>/data", "get_competition_data", self.get_competition_data, ["GET"]], ["/competition/<string:competition_name>/load", "load_competition", self.load_competition, ["GET"]], ["/competition/<string:competition_name>/save", "save_competition", self.save_competition, ["GET"]], ["/competition/<string:competition_name>/import_archers", "import_archers", self.import_archers, ["GET"]], ["/competition/<string:competition_name>/archers", "get_archers", self.get_archers, ["GET"]], ["/competition/<string:competition_name>/sorted_archers", "get_sorted_archers", self.get_sorted_archers, ["GET"]], ["/competition/<string:competition_name>/add_archer", "add_archer", self.add_archer, ["POST"]], ["/competition/<string:competition_name>/import_archers", "import_archers", self.import_archers, ["POST"]], ["/competition/<string:competition_name>/remove_archer", "remove_archer", self.remove_archer, ["POST"]], ["/competition/<string:competition_name>/add_bow", "add_bow_type", self.add_bow_type, ["POST"]], ["/competition/<string:competition_name>/remove_bow", "remove_bow_type", self.remove_bow_type, ["POST"]], ["/competition/<string:competition_name>/add_class", "add_class", self.add_class, ["POST"]], ["/competition/<string:competition_name>/remove_class", "remove_class", self.remove_class, ["POST"]], ["/competition/<string:competition_name>/update_score", "update_score", self.update_score, ["POST"]], ] for endpoint in self.url_endpoints: self.app.add_url_rule(endpoint[0], endpoint[1], endpoint[2], methods=endpoint[3]) log.info("Initialized API")
def __init__(self, app: object): self.app = app self.url_endpoints = [ [ "/static/js/<string:file>/", "static_js", self.static_js, ["GET"] ], [ "/static/css/<string:file>/", "static_css", self.static_css, ["GET"] ], ["/", "index_html", self.index, ["GET"]], ["/scoring/", "scoring_html", self.scoring, ["GET"]], ["/archers/", "archers_html", self.archers, ["GET"]], [ "/edit_competition/", "edit_competition_html", self.edit, ["GET"] ], ["/print_archers/", "print_archers", self.print_archers, ["GET"]], ["/print_scores/", "print_scores", self.print_scores, ["GET"]], ] for endpoint in self.url_endpoints: self.app.add_url_rule(endpoint[0], endpoint[1], endpoint[2]) log.info("Initialized Frontend")
def add_archer(self, competition_name: str): if competition_name in self.competitions: request_data = request.get_json() if request_data: competition = self.competitions[competition_name] name = request_data["name"] bow_type = request_data["bow"] archer_class = request_data["class"] archer_club = request_data["club"] competition.add_archer(name, bow_type, archer_class, archer_club) log.info("Added new archer (name: {0}, bow: {1}, class: {2}".format(name, bow_type, archer_class)) return jsonify({ "status": "SUCCESS" }) else: log.error("No competition with name {0} loaded".format(competition_name)) return jsonify({ "status": "ERROR", "error_message": "UNKNOWN_COMPETITION" })
def import_archers(self, archer_file: str): with open(archer_file, "r", encoding='utf-8') as file: file = json.load(file) archers = file["Archers"] bow_types = file["Bows"] archer_classes = file["Classes"] archer_count = 0 for bow in bow_types: self.add_bow(bow_types[bow]) for archer_class in archer_classes: self.add_class(archer_classes[archer_class]) for archer in archers: self.add_archer(archers[archer]["name"], archers[archer]["bow"], archers[archer]["class"], archers[archer]["club"]) archer_count += 1 log.info("Loaded {0} archers, from file: {1}".format( archer_count, archer_file)) remove(archer_file)
def load_competition(config_path: str): with open(config_path, "r") as config_file: config = json.load(config_file) competition = Competition(config["name"], config["path"]) try: competition.date = config["date"] competition.description = config["description"] except: pass competition.bow_types = config["bows"] competition.archer_classes = config["classes"] archers = [] for archer in config["archers"]: archer_object = Archer(config["archers"][archer]["name"], config["archers"][archer]["bow"], config["archers"][archer]["class"], config["archers"][archer]["club"]) for score in config["archers"][archer]["scores"]: archer_object.add_score(score) archer_object.calculate_score() archers.append(archer_object) competition.archers = archers log.info("Loaded competition {0}!".format(competition.name)) return competition
def new_competition(self): try: competition = Competition(request.json["name"], self.db_folder + request.json["name"]) competition.set_date(request.json["date"]) competition.set_description(request.json["description"]) competition.save() log.info("Created competition {0}".format(request.json["name"])) self.competitions.update({request.json["name"]: competition}) return jsonify({ "status": "SUCCESS" }) except KeyError: log.warn("Received invalid request for creating competition") return jsonify({ "status": "ERROR" })
def load_competition(self, competition_name: str): if competition_name not in self.competitions: try: competition = Competition.load_competition(self.db_folder + competition_name) self.competitions.update({competition_name: competition}) return jsonify({ "status": "SUCCESS", "competition_name": competition_name, "new": False }) except OSError: log.error("Could not find config for competition {0}".format(competition_name)) return jsonify({ "status": "ERROR", "error_message": "ALREADY_LOADED", "competition_name": competition_name, "new": False }) except Exception as exc: log.info("Could not find config for competition {0} \n Exception: {1}".format(competition_name, exc)) return jsonify({ "status": "ERROR", "error_message": "CONFIG_LOADING_ERROR", "competition_name": competition_name, "new": False }) elif competition_name in self.competitions: return jsonify({ "status": "ERROR", "error_message": "ALREADY_LOADED" })