def create_user(): required_fields = {'username', 'email', 'password'} user_json = request.json if user_json is None: return jsonify({"msg": "Userdata malformed."}), HTTPStatus.BAD_REQUEST user_json = {k: v for k, v in user_json.items() if v} fields = set(user_json.keys()) if fields < required_fields: missing = ', '.join(required_fields - fields) return jsonify({"msg": "Missing fields: %s." % missing }), HTTPStatus.BAD_REQUEST query_filter = or_(User.username == user_json['username'], User.email == user_json['email']) existing_user = User.query.filter(query_filter).first() if existing_user is not None: return jsonify({"msg": "Username or email already in use." }), HTTPStatus.BAD_REQUEST new_user = User(**user_json) db.session.add(new_user) db.session.commit() return jsonify(new_user._asdict())
def signup(): data = request.json new_user = User(from_dict=request.json) new_user.id = None footprints = data.get('footprints')[0] BaseObject.check_and_save(new_user) objects_to_save = [] for footprint in footprints.get('footprints'): footprint_obj = Footprint(from_dict=footprint) footprint_obj.user_id = int(new_user.get_id()) objects_to_save.append(footprint_obj) # TODO: c'est pas beau mais c'était plus rapide :( answers = footprints.get('answers') for key, value in answers.items(): property_obj = Property.query.filter_by(property_name=key).first() answer_obj = UserProperty() answer_obj.user_id = int(new_user.get_id()) answer_obj.property_id = property_obj.id answer_obj.value = float(value) objects_to_save.append(answer_obj) BaseObject.check_and_save(*objects_to_save) login_user(new_user) return jsonify(new_user._asdict(include=USER_INCLUDES)), 201
def signup(): data = request.json new_user = User(from_dict=request.json) new_user.id = None footprints = data.get('footprints')[0] BaseObject.check_and_save(new_user) objects_to_save = [] for footprint in footprints.get('footprints'): if footprint.get('type') == 'home_mates': new_user.home_mates = int(footprint.get('value')) BaseObject.check_and_save(new_user) else: footprint_obj = Footprint(from_dict=footprint) footprint_obj.user_id = int(new_user.get_id()) objects_to_save.append(footprint_obj) answers = footprints.get('answers') details = footprints.get('details') for footprint_detail in details: logger.info(footprint_detail) footprint_details = FootprintDetails() footprint_details.user_id = int(new_user.get_id()) footprint_details.category = footprint_detail['category'] footprint_details.value = float(footprint_detail['value']) footprint_details.type = FootprintType({'label': footprint_detail['type']}) objects_to_save.append(footprint_details) for key, value in answers.items(): property_value = Answer.query.filter_by(answer_name=key).first() if property_value is None: logger.info("Form seems to be broken: %s" % key) continue answer_obj = UserProperty() answer_obj.user_id = int(new_user.get_id()) answer_obj.answer_id = int(property_value.id) objects_to_save.append(answer_obj) BaseObject.check_and_save(*objects_to_save) login_user(new_user) return jsonify(new_user._asdict(include=USER_INCLUDES)), 201
def signup(): new_user = User(from_dict=request.json) new_user.id = None Wrapper.check_and_save(new_user) login_user(new_user) return jsonify(new_user._asdict(include=USER_INCLUDES)), 201