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 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 save_competition(self, competition_name: str): if competition_name not in self.competitions: log.warn("Competition not loaded, can not save competition!") return jsonify({ "status": "ERROR", "error_message": "COMPETITION_NOT_LOADED" }) else: self.competitions[competition_name].save() return jsonify({ "status": "SUCCESS" })
def import_archers(self, competition_name: str): if competition_name not in self.competitions: log.warn("Competition not loaded, can not import archers!") return if request.method == "POST": if "file" not in request.files: log.warn("No file in request") else: file = request.files["file"] path = self.app.config["UPLOAD_FOLDER"] file.save(os.path.join(path, competition_name + "_archers")) self.competitions[competition_name].import_archers(path + "/{0}_archers".format(competition_name)) return jsonify({ "status": "SUCCESS", "new_archers": len(self.competitions[competition_name].archers) })
def add_archer(self, name: str, bow_type: str, archer_class: str, club: str): if name not in self.archers: if bow_type in self.bow_types: if archer_class in self.archer_classes: new_archer = Archer(name, bow_type, archer_class, club) self.archers.append(new_archer) else: log.warn("Class {0} does not exists!".format(archer_class)) else: log.warn("Bow {0} does not exists!".format(bow_type)) else: log.warn("Archer {0} already exists!".format(name))
def remove_class(self, name: str): if name in self.archer_classes: self.archer_classes.remove(name) else: log.warn("Class {0} does not exist!".format(name))
def add_class(self, name: str): if not name in self.archer_classes: self.archer_classes.append(name) else: log.warn("Class {0} already exists!".format(name))
def remove_bow(self, name: str): if name in self.bow_types: self.bow_types.remove(name) else: log.warn("Bow {0} does not exist!".format(name))
def add_bow(self, name: str): if name not in self.bow_types: self.bow_types.append(name) else: log.warn("Bow {0} already exists!".format(name))