from faker import Faker
from random import randint
from manage import app
app.app_context().push()
from dog.models import db, User, Role, Post

fake = Faker('zh-CN')
Role.insert_roles()
role_user = Role.query.filter_by(name='User').first()

def iter_users():
    for i in range(11):
        yield User(
            name = fake.name(),
            age = randint(11, 44),
            gender = ['MALE', 'FEMALE'][randint(0, 1)],
            location = fake.city_name(),
            email = fake.email(),
            call_num = fake.phone_number(),
            role = role_user,
            password = 'shiyanlou',
            about_me = fake.sentence(nb_words=44),
            confirmed = 1
        )

def iter_posts():
    for user in iter_users():
        db.session.add(user)
        for i in range(randint(2, 5)):
            yield Post(
                author = user,
                body = fake.text(max_nb_chars=222),
                timestamp = fake.date_time_this_year()
            )

def run():
    for post in iter_posts():
        db.session.add(post)
        db.session.commit()

if __name__ == '__main__':
    run()
