def load_challenges_csv(dict_reader): schema = ChallengeSchema() errors = [] for i, line in enumerate(dict_reader): # Throw away fields that we can't trust if provided _ = line.pop("id", None) _ = line.pop("requirements", None) flags = line.pop("flags", None) tags = line.pop("tags", None) hints = line.pop("hints", None) challenge_type = line.pop("type", "standard") # Load in custom type_data type_data = json.loads(line.pop("type_data", "{}") or "{}") line.update(type_data) response = schema.load(line) if response.errors: errors.append((i + 1, response.errors)) continue ChallengeClass = get_chal_class(challenge_type) challenge = ChallengeClass.challenge_model(**line) db.session.add(challenge) db.session.commit() if flags: flags = [flag.strip() for flag in flags.split(",")] for flag in flags: f = Flags(type="static", challenge_id=challenge.id, content=flag,) db.session.add(f) db.session.commit() if tags: tags = [tag.strip() for tag in tags.split(",")] for tag in tags: t = Tags(challenge_id=challenge.id, value=tag,) db.session.add(t) db.session.commit() if hints: hints = [hint.strip() for hint in hints.split(",")] for hint in hints: h = Hints(challenge_id=challenge.id, content=hint,) db.session.add(h) db.session.commit() if errors: return errors return True
def patch(self, challenge_id): data = request.get_json() # Load data through schema for validation but not for insertion schema = ChallengeSchema() response = schema.load(data) if response.errors: return {"success": False, "errors": response.errors}, 400 challenge = Challenges.query.filter_by(id=challenge_id).first_or_404() challenge_class = get_chal_class(challenge.type) challenge = challenge_class.update(challenge, request) response = challenge_class.read(challenge) return {"success": True, "data": response}
def post(self): data = request.form or request.get_json() # Load data through schema for validation but not for insertion schema = ChallengeSchema() response = schema.load(data) if response.errors: return {"success": False, "errors": response.errors}, 400 challenge_type = data["type"] challenge_class = get_chal_class(challenge_type) challenge = challenge_class.create(request) response = challenge_class.read(challenge) return {"success": True, "data": response}
def get(self, skill_id): skills = (Competences.query.filter(Competences.id == skill_id)).all() schema = ChallengeSchema(only=["id"], many=True) for skill in skills: data = schema.dump(skill.challenge_id).data chall_ids = [] for chall in data: chall_ids += chall.values() challenges = (Challenges.query.filter( Challenges.id.in_(chall_ids))).all() log("debug", "{date} - {data} ", data=challenges) tag_schema = TagSchema(view="user", many=True) comp_schema = CompetenceSchema(view="user", many=True) response = [] for challenge in challenges: challenge_type = get_chal_class(challenge.type) response.append({ "id": challenge.id, "type": challenge_type.name, "name": challenge.name, "value": challenge.value, "result": challenge.result, "category": challenge.category, "level": challenge.level, "tags": tag_schema.dump(challenge.tags).data, "competences": comp_schema.dump(challenge.competences).data, "template": challenge_type.templates["view"], "script": challenge_type.scripts["view"], }) db.session.close() return {"success": True, "data": response}