def run(self): from grocery_store import app, db with app.app_context(): users = get_users() goods = get_goods() stores = get_stores() for user in users: db.session.add(User(**user)) db.session.commit() for good in goods: db.session.add(Good(**good)) db.session.commit() for store in stores: db.session.add(Store(**store)) db.session.commit() logging.info("Data added to database successfully")
def run(self): from grocery_store import app, db with app.app_context(): users = User.query.all() goods = Good.query.all() stores = Store.query.all() for user in users: number_of_orders = randint(1, 5) for _ in range(number_of_orders): number_of_goods = randint(1, 10) order = Order() order_lines = [OrderLine(good=good) for good in sample(goods, number_of_goods)] order.order_lines = order_lines order.user = user order.store = choice(stores) db.session.add(order) db.session.commit() logging.info("Added successfully.")
def get_goods(): with open(GOODS_DIR, "r") as f: reader = csv.DictReader(f) goods = [good for good in reader] return goods def get_stores(): with open(STORES_DIR, "r") as f: reader = csv.DictReader(f) stores = [store for store in reader] return stores with app.app_context(): if not database_exists(db.engine.url): logging.info(f'Database "{Config.DB_NAME}" does not exists') create_database(db.engine.url) logging.info(f'Creating database "{Config.DB_NAME}"...') logging.info("Database successfully created") db.create_all() logging.info(f'Database "{Config.DB_NAME}" exists') logging.info("Connection to database...") logging.info("Connection succeeded!") with app.app_context(): users = get_users() goods = get_goods() stores = get_stores() for user in users:
def run(self): from grocery_store import app, db with app.app_context(): users = User.query.all() goods = Good.query.all() stores = Store.query.all()