Browse Source

Chapter 11: Generate fake users and posts (11c)

master
T. Meissner 6 years ago
parent
commit
3c68168f40
5 changed files with 42 additions and 1 deletions
  1. +37
    -0
      app/fake.py
  2. +1
    -1
      app/templates/user.html
  3. +0
    -0
      requirements/common.txt
  4. +3
    -0
      requirements/dev.txt
  5. +1
    -0
      requirements/prod.txt

+ 37
- 0
app/fake.py View File

@ -0,0 +1,37 @@
from random import randint
from sqlalchemy.exc import IntegrityError
from faker import Faker
from . import db
from .models import User, Post
def users(count=100):
fake = Faker()
i = 0
while i < count:
u = User(email=fake.email(),
username=fake.user_name(),
password='password',
confirmed=True,
name=fake.name(),
location=fake.city(),
about_me=fake.text(),
member_since=fake.past_date())
db.session.add(u)
try:
db.session.commit()
i += 1
except IntegrityError:
db.session.rollback()
def posts(count=100):
fake = Faker()
user_count = User.query.count()
for i in range(count):
u = User.query.offset(randint(0, user_count - 1)).first()
p = Post(body=fake.text(),
timestamp=fake.past_date(),
author=u)
db.session.add(p)
db.session.commit()

+ 1
- 1
app/templates/user.html View File

@ -26,7 +26,7 @@
<p>
Member since {{ moment(user.member_since).format('L') }}.
Last seen {{ moment(user.last_seen).fromNow() }}.
<p>{{ user.posts.count() }} blog posts.</p>
<p>{{ user.posts.count() }} blog post{% if user.posts.count() > 1 %}s{% endif %}.</p>
</p>
<p>
{% if user == current_user %}


requirements.txt → requirements/common.txt View File


+ 3
- 0
requirements/dev.txt View File

@ -0,0 +1,3 @@
-r common.txt
Faker==1.0.0
text-unidecode==1.2

+ 1
- 0
requirements/prod.txt View File

@ -0,0 +1 @@
-r common.txt

Loading…
Cancel
Save