-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulate_db.py
48 lines (40 loc) · 1.22 KB
/
populate_db.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
from app import create_app
from app.models.user import User
from app.models.note import Note
from app.models.tag import Tag
from app.models.note_tags import note_tags
from faker import Faker
import random
# Create a Flask app context
app = create_app()
# Initialize the app context
app.app_context().push()
# Initialize the database
from db import db
# Create a Faker instance for generating fake data
fake = Faker()
# Create users and add notes for each user
for _ in range(5): # Create 5 users
user = User(
username=fake.user_name(),
email=fake.email(),
full_name=fake.name(),
password=fake.password(),
)
db.session.add(user)
for _ in range(random.randint(1, 5)): # Add random notes for each user
note = Note(
title=fake.sentence(),
content=fake.paragraph(),
user=user,
)
db.session.add(note)
# Add random tags to the note
for _ in range(random.randint(1, 3)):
tag_name = fake.word()
tag = Tag.query.filter_by(name=tag_name).first()
if not tag:
tag = Tag(name=tag_name)
note.tags.append(tag)
# Commit the changes to the database
db.session.commit()