def create_item(): data = request.get_json() new_item = FoodItem(name=data['name'], calories=data['calories'], protein=data['protein'], fat=data['fat'], carbs=data['carbs']) print(new_item) print(data) db.session.add(new_item) db.session.commit() return jsonify({'message': 'New item added!'})
def create_plan(current_user): data = request.get_json() # jeśli user dodaje swoje itemy - najpierw wrzucam je do (jego profilu?) bazy #TODO model CustomFoodItem n:1 User if data.get('custom_items', ''): for custom_item in data['custom_items']: new_item = FoodItem(name=custom_item['name'], calories=custom_item['calories'], protein=custom_item['protein'], fat=custom_item['fat'], carbs=custom_item['carbs']) print("New item ADDED: ",new_item) db.session.add(new_item) # najpierw dodaję plan i przypisuję go do current_usera new_plan = DietPlan(name=data['name']) db.session.add(new_plan) db.session.commit() assign_plan = DietPlanUser(user_id=current_user.id, diet_plan_id=new_plan.id) #dodawanie do planu itemów, z naszej bazy, które podał user if data.get('our_items', ''): for item in data['our_items']: assign_item = DietPlanFoodItem(food_item_id = item['food_item_id'], diet_plan_id = new_plan.id, meal_time = item['meal_time'], weekday = item['weekday'], food_item_weight = item['food_item_weight'], food_item_pieces = item.get('food_item_pieces', None) ) db.session.add(assign_item) print(assign_item) print("DODAJĘ PLAN: ",new_plan) db.session.add(assign_plan) db.session.commit() return jsonify({'message': 'New plan added!'})
print('Grocery UPC database already downloaded.') print('Opening the data file...') # Import the data from the spreadsheet book = xlrd.open_workbook(UPC_XLSX_NAME) sheet = book.sheets()[0] print('Importing the data...') for r in range( 1, sheet.nrows): # Skip the first row, as it's just column names # row = sheet.row(r) item_id = int(sheet.cell(r, 0).value) upc12 = int(sheet.cell(r, 2).value) name = sheet.cell(r, 4).value.replace('"', '') item = FoodItem(item_id, name, None, None, None, None) fia.save_item(item) if r % 100 == 0: print('Successfully saved row', r) print('Grocery UPC data successfully imported.') # CSV method: # with open('static/Grocery_UPC_Database.csv', newline='', encoding='utf-8') as csvfile: # try: # reader = csv.reader(csvfile, delimiter=',', quotechar='|') # for row in reader: # # if reader.line_num == 652: # # stop = True # item_id = row[0] # line_count += 1 # if reader.line_num == 1:
def __parse_food_item(row): """ Internal method for parsing the results of a database query and saving it into a Location object """ item = FoodItem(row['item_id'], row['name'], row['aisle'], row['category'], row['description'], row['image_url'], row['id']) return item