示例#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()
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
示例#3
0
def seed_db():
    """Seed the database with some default users"""
    for i in range(10000):
        # Initialize AES
        key = bytes.fromhex(f'100000000000000000000000000{str(10000 + i)}')
        IV = bytes.fromhex(f'a00000000000000000000000000{str(10000 + i)}')
        pad = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)

        user = User(f'User{i}',
                    hashlib.sha256(f'User{i}'.encode('utf8')).hexdigest())
        user.gender = 'M'
        user.country = 'Belgium'
        user.city = 'Antwerp'
        user.zip_code = '2000'
        user.street = f'Keyserlei {i}'
        user.card_type = 'VISA'
        aes = AES.new(key, AES.MODE_CBC, IV)
        user.card_holder_name = base64.b64encode(
            IV + aes.encrypt(pad(f'User {i}'))).decode('utf-8')
        aes = AES.new(key, AES.MODE_CBC, IV)
        user.card_number = base64.b64encode(
            IV +
            aes.encrypt(pad(f'45000000000{str((10000 + i))}'))).decode('utf-8')
        aes = AES.new(key, AES.MODE_CBC, IV)
        user.expiration_date_month = base64.b64encode(
            IV + aes.encrypt(pad('4'))).decode('utf-8')
        aes = AES.new(key, AES.MODE_CBC, IV)
        user.expiration_date_year = base64.b64encode(
            IV + aes.encrypt(pad('2023'))).decode('utf-8')
        aes = AES.new(key, AES.MODE_CBC, IV)
        user.cvv = base64.b64encode(IV +
                                    aes.encrypt(pad('203'))).decode('utf-8')
        token = str(hash(user))
        user.token = token
        db.session.add(user)

    db.session.commit()