Ejemplo n.º 1
0
def seed_db():
    # Seed database with example data

    from models.Post import Post
    from models.User import User
    from main import bcrypt
    from faker import Faker
    import random

    faker = Faker()
    users = []

    for i in range(5):
        user = User()
        user.email = f"test{i}@test.com"
        user.name = faker.name()
        user.password = bcrypt.generate_password_hash("123456").decode("utf-8")
        user.created_at = datetime.now()
        db.session.add(user)
        users.append(user)

    db.session.commit()

    for i in range(20):
        post = Post()
        post.caption = faker.catch_phrase()
        post.created_at = datetime.now()
        post.updated_at = datetime.now()
        post.total_likes = 0
        post.total_comments = 0
        post.user_id = random.choice(users).id
        db.session.add(post)

    db.session.commit()
    print("Tables seeded")
Ejemplo n.º 2
0
def post_create(user):
    #Create a new post
    post_fields = post_schema.load(request.json)

    new_post = Post()
    new_post.caption = post_fields["caption"]
    new_post.created_at = datetime.now()
    new_post.updated_at = datetime.now()
    new_post.total_comments = 0
    new_post.total_likes = 0
    new_post.total_dislikes = 0

    user.posts.append(new_post)

    db.session.commit()
    
    return jsonify(post_schema.dump(new_post))