Files
secure-online-shop/app/db/seed_demo.py
T

62 lines
2.4 KiB
Python

from __future__ import annotations
import logging
from decimal import Decimal
from sqlalchemy import func, select
from sqlalchemy.orm import Session
from app.models.product import Product
logger = logging.getLogger("app.db")
PRODUCT_TEMPLATES = (
("Aurora Headphones", "Wireless headphones with active noise reduction", "189.90", 42),
("Nimbus Laptop", "Thin laptop for study, work, and secure online payments", "1299.00", 18),
("Pulse Smartwatch", "Fitness smartwatch with long battery life", "249.50", 55),
("Volt Power Bank", "Compact 20000 mAh power bank with fast charging", "79.99", 90),
("Axis Keyboard", "Mechanical keyboard with quiet tactile switches", "139.00", 36),
("Orbit Backpack", "Water-resistant backpack with laptop compartment", "89.90", 64),
("Brew Coffee Maker", "Programmable drip coffee maker for home offices", "119.95", 31),
("Frame Desk Lamp", "Adjustable LED lamp with warm and cold light modes", "54.40", 83),
("Focus Webcam", "Full HD webcam with privacy shutter", "74.99", 71),
("Studio Speaker", "Bluetooth speaker with balanced stereo sound", "159.00", 27),
("Terra Sneakers", "Lightweight everyday sneakers with cushioned soles", "109.90", 49),
("Slate Tablet", "Portable tablet for browsing, media, and field work", "399.00", 22),
)
def seed_demo_products(db: Session, target_count: int) -> None:
"""Populate the catalog with deterministic demo products for MVP deployment."""
if target_count <= 0:
return
existing_count = db.scalar(select(func.count(Product.id))) or 0
products_to_create = target_count - existing_count
if products_to_create <= 0:
logger.info("Demo product seed skipped existing_count=%s", existing_count)
return
products: list[Product] = []
for offset in range(products_to_create):
sequence = existing_count + offset + 1
name, description, price, base_stock = PRODUCT_TEMPLATES[offset % len(PRODUCT_TEMPLATES)]
batch = (offset // len(PRODUCT_TEMPLATES)) + 1
products.append(
Product(
name=f"{name} {batch:02d}",
description=description,
price=Decimal(price),
stock=base_stock + (sequence % 17),
is_active=True,
)
)
db.add_all(products)
db.commit()
logger.info(
"Demo product seed completed created=%s target_count=%s",
len(products),
target_count,
)