Exemplo n.º 1
0
def seed_db():
    """Seeds the database."""
    with open("tags.json") as f:
        tags = json.load(f)
        for tag in tags:
            tag_object = Tag()
            tag_object.name = tag["name"]
            db.session.add(tag_object)
    db.session.commit()

    with open("users.json") as f:
        users = json.load(f)
        for user in users:
            user_object = User()
            user_object.email = user["email"]
            user_object.name = user["name"]
            user_object.birthdate = dateutil.parser.parse(user["birthdate"])
            user_object.gender = user["gender"]
            user_object.interested_in = user["interestedIn"]
            user_object.picture_url = user["pictureUrl"]
            user_object.hashed_password = user["hashedPassword"]
            user_object.description = user["description"]
            for tag_id in user["tags"]:
                tag_object = Tag.query.filter_by(tag_id=tag_id).first()
                user_object.tags.append(tag_object)
            db.session.add(user_object)
    db.session.commit()

    with open("ratings.json") as f:
        ratings = json.load(f)
        for rating in ratings:
            rating_object = Rating()
            rating_object.from_user_id = rating["fromUserId"]
            rating_object.to_user_id = rating["toUserId"]
            rating_object.stars = rating["stars"]
            rating_object.comment = rating["comment"]
            db.session.add(rating_object)
    db.session.commit()

    with open("messages.json") as f:
        messages = json.load(f)
        for message in messages:
            message_object = Message()
            message_object.from_user_id = message["fromUserId"]
            message_object.content = message["content"]
            message_object.date_time = dateutil.parser.parse(
                message["date_time"])
            db.session.add(message_object)
    db.session.commit()
Exemplo n.º 2
0
def register():
    """Creates a user account"""
    response = {}
    request_json = request.get_json()

    # Validation
    keys = [
        "email", "name", "birthdate", "gender", "interestedIn", "description", "pictureURL",
        "password", "tags",
    ]
    for key in keys:
        if key not in request_json:
            response["message"] = "Missing {} key in request body".format(key)
            return jsonify(response), 400

    email = request_json["email"]
    user = User.query.filter_by(email=email).first()
    if user is not None:
        response["message"] = "Email already exists"
        return jsonify(response), 409

    # Create user
    user = User()
    user.email = email
    user.name = request_json["name"]
    user.birthdate = dateutil.parser.parse(request_json["birthdate"])
    user.gender = request_json["gender"]
    user.interested_in = request_json["interestedIn"]
    user.description = request_json["description"]
    user.picture_url = request_json["pictureURL"]
    password = request_json["password"].encode("utf-8")
    user.hashed_password = hashlib.sha256(password).hexdigest()
    for tag_id in request_json["tags"]:
        tag = Tag.query.filter_by(tag_id=tag_id).first()
        user.tags.append(tag)
    db.session.add(user)
    db.session.commit()
    response["message"] = "Account created successfully"
    return jsonify(response), 201