def new_category(): if request.method == "GET": return render_template("new_category.html") elif request.method == "POST": category = Category(None, request.form["name"]) category.create() return redirect("/categories")
def new_category(): if request.method == "GET": return render_template("new_category.html") elif request.method == "POST": category = Category(None, request.form["name"]) category.create() logging.info('%s with id: %s added new categoty %s named %s', User.find_by_id(session['USERNAME']), session['USERNAME'], category.id, category.name) return redirect("/categories")
def creation(): """ create all the tables by calling static method from these tables six tables including two association tables: - CategoryProduct and StoreProduct .""" with Connection.get_instance() as cursor: sql = """ CREATE DATABASE IF NOT EXISTS openfoodbase CHARACTER SET utf8; """ cursor.execute(sql) Category.create() Product.create() Store.create() CategoryProduct.create() StoreProduct.create() Historic.create()
def data_init(): """Function that creates the different tables """ # Create Category table (SQL REQUEST) categorie = Category() product = Product() favorite = Favorite() tables = { 'Category': categorie.create(), 'Product': product.create(), 'Favorite': favorite.create() } # Create all tables of database for table_name in tables: table_description = tables[table_name] try: print(f"Création de la table {table_name} : ", end='') my_cursor.execute(table_description) except mysql.connector.Error as err: if err.errno == errorcode.ER_TABLE_EXISTS_ERROR: print("existe déjà.") else: print(err.msg) else: print("OK") # Insert 7 categories in my category table(manually) categorie.insert_data() # The categories of my table category_index = list() for item in data_categories: category_index.append(item[1]) # browse categories and insert data for index in enumerate(category_index): # Upload json file from api data_for_insert = upload_data(index[1], 1) # Insert data in Product table size = len(data_for_insert['products']) print(f'{index[1]} - produits: {size}') # The loop that inserts the data into my tables for i in range(size): try: store = data_for_insert['products'][i]['stores'] name = data_for_insert['products'][i]['product_name'] grade = data_for_insert['products'][i][ 'nutrition_grades_tags'][0] url = data_for_insert['products'][i]['url'] id_category = index[0] + 1 except (KeyError, TypeError): continue finally: product.insert_data(name, url, grade, id_category, store)