-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_dummy_data.py
53 lines (44 loc) · 1.42 KB
/
generate_dummy_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from random import randrange
from sqlite3 import Connection as SQLite3Connection
from datetime import datetime
from faker import Faker
from sqlalchemy import event
from sqlalchemy.engine import Engine
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import server
# app
app = Flask(__name__)
# config
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///sqlitedb.file"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = 0
# configure sqlite3 to enforce foreign key contraints
@event.listens_for(Engine, "connect")
def _set_sqlite_pragma(dbapi_connection, connection_record):
if isinstance(dbapi_connection, SQLite3Connection):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON;")
cursor.close()
db = SQLAlchemy(app)
now = datetime.now()
faker = Faker()
# create dummy users
for i in range(200):
name = faker.name()
address = faker.address()
phone = faker.msisdn()
email = f'{name.replace(" ", "_")}@email.com'
new_user = server.User(name=name, address=address, phone=phone, email=email)
db.session.add(new_user)
db.session.commit()
# create dummy blog posts
for i in range(200):
title = faker.sentence(5)
body = faker.paragraph(190)
date = faker.date_time()
user_id = randrange(1, 200)
new_blog_post = server.BlogPost(
title=title, body=body, date=date, user_id=user_id
)
db.session.add(new_blog_post)
db.session.commit()