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}