Example #1
0
 def post(self):
     temp_list_goods = request.json
     for item in temp_list_goods:
         temp = Good(**item)
         db.session.add(temp)
     db.session.commit()
     return f"Numbers of items created {len(temp_list_goods)}"
Example #2
0
 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")
Example #3
0
 def post(self):
     good = Good(**request.json)
     db.session.add(good)
     db.session.commit()
     return f"Successfully added a new good {good}"
Example #4
0
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:
        db.session.add(User(**user))
    for good in goods:
        db.session.add(Good(**good))
    for store in stores:
        db.session.add(Store(**store))
    db.session.commit()
    logging.info("Data added to database successfully")