Beispiel #1
0
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!'})
Beispiel #2
0
 def post(self):
     action = cgi.escape(self.request.get('action'))
     
     if action == 'add':
         fooditem = FoodItem()
         fooditem.name = cgi.escape(self.request.get('fooditem_name'))
         fooditem.kcals = int(cgi.escape(self.request.get('fooditem_kcals')))
         fooditem.put()
     
     elif action == 'delete':
         key_name = self.request.get('fooditemkey')
         if len(key_name) == 0: 
             return;
             
         fooditem = db.get(db.Key(key_name))
         fooditem.delete()
     
     self.redirect('/foodupdater')
Beispiel #3
0
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!'})
Beispiel #4
0
 def get(self):
     fooditems = FoodItem.all().fetch(15)
     template_values = { 'fooditems': fooditems }
     
     path = os.path.join(os.path.dirname(__file__), 'foodupdater.html')
     self.response.out.write(template.render(path, template_values))
        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:
Beispiel #6
0
def food_search_within_restaurant(rid):
    searchterm = request.args['query']
    searchterm = "%"+searchterm.replace(" ", "%")+"%"
    fs= FoodSearch(rid, searchterm)
    results = fs.search_db()
    return jsonify([FoodItem.get_food_item_by_id(x).serializable() for x in results])
Beispiel #7
0
def get_fooditem(id):
    return jsonify(FoodItem.get_food_item_by_id(id).serializable())
Beispiel #8
0
 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