|
| 1 | +from random import choice |
| 2 | +from random import randint |
| 3 | + |
| 4 | +from faker import Faker |
| 5 | +from models import Address |
| 6 | +from models import Category |
| 7 | +from models import Comment |
| 8 | +from models import db |
| 9 | +from models import Department |
| 10 | +from models import Employee |
| 11 | +from models import Order |
| 12 | +from models import OrderItem |
| 13 | +from models import OrderStatus |
| 14 | +from models import Payment |
| 15 | +from models import PaymentMethod |
| 16 | +from models import Post |
| 17 | +from models import Product |
| 18 | +from models import Profile |
| 19 | +from models import Project |
| 20 | +from models import Role |
| 21 | +from models import Tag |
| 22 | +from models import User |
| 23 | + |
| 24 | +fake = Faker() |
| 25 | + |
| 26 | + |
| 27 | +def generate_fake_data(app): |
| 28 | + """Populate the database with a rich set of example data covering all models.""" |
| 29 | + with app.app_context(): |
| 30 | + db.create_all() |
| 31 | + db.create_all() |
| 32 | + # ----- Roles ----- |
| 33 | + if Role.query.count() == 0: |
| 34 | + for name in ("admin", "editor", "user"): |
| 35 | + db.session.add(Role(name=name)) |
| 36 | + db.session.commit() |
| 37 | + |
| 38 | + roles = Role.query.all() |
| 39 | + |
| 40 | + # ----- Categories ----- |
| 41 | + while Category.query.count() < 5: |
| 42 | + db.session.add( |
| 43 | + Category(name=fake.unique.word(), description=fake.sentence()) |
| 44 | + ) |
| 45 | + db.session.commit() |
| 46 | + categories = Category.query.all() |
| 47 | + |
| 48 | + # ----- Tags ----- |
| 49 | + while Tag.query.count() < 10: |
| 50 | + db.session.add(Tag(name=fake.unique.word())) |
| 51 | + db.session.commit() |
| 52 | + tags = Tag.query.all() |
| 53 | + |
| 54 | + # ----- Products ----- |
| 55 | + while Product.query.count() < 20: |
| 56 | + db.session.add( |
| 57 | + Product( |
| 58 | + name=fake.unique.word().title(), |
| 59 | + description=fake.text(max_nb_chars=200), |
| 60 | + price=round( |
| 61 | + fake.pyfloat( |
| 62 | + left_digits=3, |
| 63 | + right_digits=2, |
| 64 | + positive=True, |
| 65 | + min_value=5, |
| 66 | + max_value=200, |
| 67 | + ), |
| 68 | + 2, |
| 69 | + ), |
| 70 | + stock=randint(0, 100), |
| 71 | + data={ |
| 72 | + "color": fake.color_name(), |
| 73 | + "size": choice(["S", "M", "L", "XL"]), |
| 74 | + }, |
| 75 | + ) |
| 76 | + ) |
| 77 | + db.session.commit() |
| 78 | + products = Product.query.all() |
| 79 | + |
| 80 | + # ----- Users ----- |
| 81 | + while User.query.count() < 50: |
| 82 | + user = User( |
| 83 | + email=fake.unique.email(), |
| 84 | + name=fake.name(), |
| 85 | + age=randint(18, 65), |
| 86 | + active=choice([True, False]), |
| 87 | + preferences={"newsletter": choice([True, False])}, |
| 88 | + balance=round( |
| 89 | + fake.pyfloat(left_digits=3, right_digits=2, positive=True), 2 |
| 90 | + ), |
| 91 | + last_login=fake.date_time_this_year(), |
| 92 | + ) |
| 93 | + # assign 1-3 random roles |
| 94 | + user.roles.extend( |
| 95 | + fake.random_elements(elements=roles, length=randint(1, 3), unique=True) |
| 96 | + ) |
| 97 | + db.session.add(user) |
| 98 | + db.session.commit() |
| 99 | + |
| 100 | + users = User.query.all() |
| 101 | + |
| 102 | + # ----- Addresses & Profiles ----- |
| 103 | + for user in users: |
| 104 | + if not user.addresses: |
| 105 | + addr = Address( |
| 106 | + street=fake.street_address(), |
| 107 | + city=fake.city(), |
| 108 | + state=fake.state(), |
| 109 | + postal_code=fake.postcode(), |
| 110 | + country=fake.country(), |
| 111 | + is_primary=True, |
| 112 | + user_id=user.id, |
| 113 | + ) |
| 114 | + db.session.add(addr) |
| 115 | + if not user.profile: |
| 116 | + profile = Profile(bio=fake.sentence(), avatar=None, user_id=user.id) |
| 117 | + db.session.add(profile) |
| 118 | + db.session.commit() |
| 119 | + |
| 120 | + # ----- Posts ----- |
| 121 | + while Post.query.count() < 100: |
| 122 | + author = choice(users) |
| 123 | + post = Post( |
| 124 | + author_id=author.id, |
| 125 | + category=choice(categories), |
| 126 | + title=fake.sentence(), |
| 127 | + body=fake.text(max_nb_chars=800), |
| 128 | + ) |
| 129 | + post.tags = fake.random_elements( |
| 130 | + elements=tags, length=randint(1, 4), unique=True |
| 131 | + ) |
| 132 | + db.session.add(post) |
| 133 | + db.session.commit() |
| 134 | + |
| 135 | + posts = Post.query.all() |
| 136 | + |
| 137 | + # ----- Comments ----- |
| 138 | + while Comment.query.count() < 300: |
| 139 | + db.session.add( |
| 140 | + Comment( |
| 141 | + post_id=choice(posts).id, |
| 142 | + user_id=choice(users).id, |
| 143 | + body=fake.sentence(), |
| 144 | + ) |
| 145 | + ) |
| 146 | + db.session.commit() |
| 147 | + |
| 148 | + # ----- Orders, Items, Payments ----- |
| 149 | + while Order.query.count() < 200: |
| 150 | + user = choice(users) |
| 151 | + order = Order( |
| 152 | + user_id=user.id, |
| 153 | + status=choice(list(OrderStatus)), |
| 154 | + total_price=0, # will update after adding items |
| 155 | + ) |
| 156 | + db.session.add(order) |
| 157 | + db.session.commit() |
| 158 | + |
| 159 | + orders = Order.query.all() |
| 160 | + |
| 161 | + for order in orders: |
| 162 | + if not order.items: |
| 163 | + chosen_products = fake.random_elements( |
| 164 | + elements=products, length=randint(1, 5), unique=True |
| 165 | + ) |
| 166 | + total = 0 |
| 167 | + for prod in chosen_products: |
| 168 | + qty = randint(1, 3) |
| 169 | + total += prod.price * qty |
| 170 | + db.session.add( |
| 171 | + OrderItem( |
| 172 | + order_id=order.id, |
| 173 | + product_id=prod.id, |
| 174 | + quantity=qty, |
| 175 | + price=prod.price, |
| 176 | + ) |
| 177 | + ) |
| 178 | + order.total_price = round(total, 2) |
| 179 | + db.session.add( |
| 180 | + Payment( |
| 181 | + order_id=order.id, |
| 182 | + amount=order.total_price, |
| 183 | + paid_at=fake.date_time_this_year(), |
| 184 | + method=choice(list(PaymentMethod)), |
| 185 | + ) |
| 186 | + ) |
| 187 | + db.session.commit() |
| 188 | + |
| 189 | + # ----- Departments ----- |
| 190 | + while Department.query.count() < 5: |
| 191 | + db.session.add( |
| 192 | + Department( |
| 193 | + name=fake.unique.company(), |
| 194 | + budget=round( |
| 195 | + fake.pyfloat( |
| 196 | + left_digits=7, |
| 197 | + right_digits=2, |
| 198 | + positive=True, |
| 199 | + min_value=10000, |
| 200 | + max_value=1000000, |
| 201 | + ), |
| 202 | + 2, |
| 203 | + ), |
| 204 | + ) |
| 205 | + ) |
| 206 | + db.session.commit() |
| 207 | + departments = Department.query.all() |
| 208 | + |
| 209 | + # ----- Projects ----- |
| 210 | + while Project.query.count() < 10: |
| 211 | + db.session.add( |
| 212 | + Project( |
| 213 | + name=fake.unique.catch_phrase(), |
| 214 | + deadline=fake.date_this_year(after_today=True), |
| 215 | + description=fake.text(max_nb_chars=200), |
| 216 | + budget=round( |
| 217 | + fake.pyfloat( |
| 218 | + left_digits=6, |
| 219 | + right_digits=2, |
| 220 | + positive=True, |
| 221 | + min_value=5000, |
| 222 | + max_value=500000, |
| 223 | + ), |
| 224 | + 2, |
| 225 | + ), |
| 226 | + ) |
| 227 | + ) |
| 228 | + db.session.commit() |
| 229 | + projects = Project.query.all() |
| 230 | + |
| 231 | + # ----- Employees ----- |
| 232 | + while Employee.query.count() < 100: |
| 233 | + dept = choice(departments) |
| 234 | + emp = Employee( |
| 235 | + department_id=dept.id, |
| 236 | + first_name=fake.first_name(), |
| 237 | + last_name=fake.last_name(), |
| 238 | + salary=round( |
| 239 | + fake.pyfloat( |
| 240 | + left_digits=6, |
| 241 | + right_digits=2, |
| 242 | + positive=True, |
| 243 | + min_value=30000, |
| 244 | + max_value=150000, |
| 245 | + ), |
| 246 | + 2, |
| 247 | + ), |
| 248 | + hire_date=fake.date_between(start_date="-10y", end_date="today"), |
| 249 | + shift_start=fake.time_object(), |
| 250 | + is_full_time=choice([True, False]), |
| 251 | + rating=round( |
| 252 | + fake.pyfloat( |
| 253 | + left_digits=1, |
| 254 | + right_digits=2, |
| 255 | + positive=True, |
| 256 | + min_value=1, |
| 257 | + max_value=5, |
| 258 | + ), |
| 259 | + 2, |
| 260 | + ), |
| 261 | + ) |
| 262 | + db.session.add(emp) |
| 263 | + db.session.commit() |
| 264 | + |
| 265 | + employees = Employee.query.all() |
| 266 | + |
| 267 | + for emp in employees: |
| 268 | + if not emp.projects: |
| 269 | + emp.projects = fake.random_elements( |
| 270 | + elements=projects, length=randint(1, 4), unique=True |
| 271 | + ) |
| 272 | + db.session.commit() |
0 commit comments