示例#1
0
    def test_delete_food(self):
        # create a new food to be deleted, then delete
        insert_food = Food(
            food="Chicken",
            protein=5,
            carbs=1,
            fat=1,
            calories=1
        )

        insert_food.insert()
        # record number of foods before delete
        number_food = len(Food.query.all())

        # delete new food
        res = self.client().post(
            f'/food/{insert_food.id}/delete',
            headers=get_headers(self.admin))

        # record number of questions after delete
        number_food_new = len(Food.query.all())

        self.assertEqual(res.status_code, 200)
        self.assertEqual(number_food_new, number_food - 1)
        self.assert_template_used('/pages/foods.html')
示例#2
0
 def add_menu_item():
     date_now = datetime.now()
     obj = Food(request.form['title'], date_now)
     try:
         Food.insert(obj)
     except SQLAlchemyError as exc:
         error = str(exc.__dict__['orig'])
         return jsonify({"error": "Oops! %s" % error})
     else:
         return jsonify(
             {"name": "Menu item successfully added - Reloading"})
示例#3
0
    def new_food(payload):
        foods = Food.query.all()
        form = NewFood()
        current_foods = Food.query.all()
        if db.session.query(
                exists().where(Food.food == request.form['food'])).scalar():
            flash('Looks like that food is already in our database.')

            return render_template('forms/new_food.html', form=form)

        new_food = Food(food=request.form['food'].lower(),
                        protein=request.form['protein'],
                        carbs=request.form['carbs'],
                        fat=request.form['fats'],
                        calories=request.form['calories'])

        new_food.insert()

        current_foods = Food.query.all()
        paged_foods = paginate_foods(request, current_foods)
        print(paged_foods)
        flash(request.form['food'] + ' has been successfully added!')
        return render_template('/pages/foods.html', foods=paged_foods)