Exemplo n.º 1
0
def clone(id):
    cart = Cart.query.get_or_404(id)
    foods = cart.foods.all()

    # clone cart
    cart_copy = Cart()
    cart_copy.user = current_user

    for food in foods:
        food_copy = FoodItem(name=food.name,
                             img_url=food.img_url,
                             nf_calories=food.nf_calories,
                             nf_total_fat=food.nf_total_fat,
                             nf_saturated_fat=food.nf_saturated_fat,
                             nf_cholesterol=food.nf_cholesterol,
                             nf_sodium=food.nf_sodium,
                             nf_total_carbohydrate=food.nf_total_carbohydrate,
                             nf_dietary_fiber=food.nf_dietary_fiber,
                             nf_sugars=food.nf_sugars,
                             nf_protein=food.nf_protein,
                             serving_unit=food.serving_unit,
                             serving_qty=food.serving_qty)
        food_copy.cart = cart_copy
        db.session.add(food_copy)

    cart_copy.update_nutrients()
    db.session.add(cart_copy)
    db.session.commit()

    flash('Cart Has Been Cloned And Added To Your Carts')
    return redirect(url_for('carts.cart', id=cart_copy.id))
Exemplo n.º 2
0
def add_food():
    form = AddFoodToCartForm()

    # form processing
    cart_id_tuple = []
    carts = current_user.carts.order_by(Cart.id.asc()).all()
    for i in range(len(carts)):
        cart_id_tuple.append((str(carts[i].id), i + 1))
    form.cart_id.choices = cart_id_tuple

    if form.validate_on_submit():
        # get cart
        cart = Cart.query.get_or_404(int(form.cart_id.data))

        # create food and add to cart
        food_info = session.get('food_info')
        food = FoodItem(
            name=food_info['food_name'],
            img_url=food_info['photo']['thumb'],
            nf_calories=food_info['nf_calories'],
            nf_total_fat=food_info['nf_total_fat'],
            nf_saturated_fat=food_info['nf_saturated_fat'],
            nf_cholesterol=food_info['nf_cholesterol'],
            nf_sodium=food_info['nf_sodium'],
            nf_total_carbohydrate=food_info['nf_total_carbohydrate'],
            nf_dietary_fiber=food_info['nf_dietary_fiber'],
            nf_sugars=food_info['nf_sugars'],
            nf_protein=food_info['nf_protein'],
            serving_unit=session.get('serving_unit'),
            serving_qty=session.get('serving_qty'))
        food.cart = cart

        # update cart nutrients too
        cart.update_nutrients()

        db.session.add(food)
        db.session.commit()

        return redirect(url_for('carts.list', username=current_user.username))

    return render_template('foods/add_food.html', form=form)
Exemplo n.º 3
0
    def test_foods_delete_food(self):
        user = User(email='*****@*****.**', username='******', password='******')
        user2 = User(email='*****@*****.**', username='******', password='******')
        cart = Cart()
        cart2 = Cart()
        food1 = FoodItem(name='food1',
                         img_url="",
                         nf_calories=Decimal(1),
                         nf_total_fat=Decimal(2),
                         nf_cholesterol=Decimal(3),
                         nf_saturated_fat=Decimal(4),
                         nf_sodium=Decimal(5),
                         nf_total_carbohydrate=Decimal(6),
                         nf_dietary_fiber=Decimal(7),
                         nf_sugars=Decimal(8),
                         nf_protein=Decimal(9),
                         serving_qty=Decimal(1),
                         serving_unit="Serving")

        food2 = FoodItem(name='food2',
                         img_url="",
                         nf_calories=Decimal(1),
                         nf_total_fat=Decimal(2),
                         nf_cholesterol=Decimal(3),
                         nf_saturated_fat=Decimal(4),
                         nf_sodium=Decimal(5),
                         nf_total_carbohydrate=Decimal(6),
                         nf_dietary_fiber=Decimal(7),
                         nf_sugars=Decimal(8),
                         nf_protein=Decimal(9),
                         serving_qty=Decimal(1),
                         serving_unit="Serving")
        food1.cart = cart
        food2.cart = cart2
        cart.user = user
        cart2.user = user2
        db.session.add_all([user, user2, cart, cart2, food1, food2])
        db.session.commit()

        with self.client:
            self.client.post(url_for('auth.login'),
                             data={
                                 'email': '*****@*****.**',
                                 'username': '******',
                                 'password': '******'
                             })

            # delete food
            self.client.get(url_for('foods.delete_food', id=1))
            cart = current_user.carts.first()
            self.assertTrue(len(cart.foods.all()) == 0)

            # 404 error if food does not exist
            response1 = self.client.get(url_for('foods.delete_food', id=100))
            self.assertTrue(response1.status_code == 404)

            # 403 error if food does not belong to user
            response2 = self.client.get(url_for('foods.delete_food', id=2))
            self.assertTrue(response2.status_code == 403)
Exemplo n.º 4
0
  def test_decimal_fields(self):
    name = 'food1'
    nf_calories = Decimal(1.00)
    nf_total_fat = Decimal(2.00)
    nf_cholesterol = Decimal(3.00)
    nf_saturated_fat = Decimal(4.00)
    nf_sodium = Decimal(5.00)
    nf_total_carbohydrate = Decimal(6.00)
    nf_dietary_fiber = Decimal(7.00)
    nf_sugars = Decimal(8.00)
    nf_protein = Decimal(9.00)
    serving_qty = Decimal(10.00)
    serving_unit='serving'

    food1 = FoodItem(name=name,
    img_url="",
    nf_calories=nf_calories,
    nf_total_fat=nf_total_fat,
    nf_cholesterol=nf_cholesterol,
    nf_saturated_fat=nf_saturated_fat,
    nf_sodium=nf_sodium,
    nf_total_carbohydrate=nf_total_carbohydrate,
    nf_dietary_fiber=nf_dietary_fiber,
    nf_sugars=nf_sugars,
    nf_protein=nf_protein,
    serving_qty=serving_qty,
    serving_unit=serving_unit
    )

    # check type decimal
    self.assertTrue(type(food1.nf_calories) == Decimal)
    self.assertTrue(type(food1.nf_total_fat) == Decimal)
    self.assertTrue(type(food1.nf_cholesterol) == Decimal)
    self.assertTrue(type(food1.nf_saturated_fat) == Decimal)
    self.assertTrue(type(food1.nf_sodium) == Decimal)
    self.assertTrue(type(food1.nf_total_carbohydrate) == Decimal)
    self.assertTrue(type(food1.nf_dietary_fiber) == Decimal)
    self.assertTrue(type(food1.nf_sugars) == Decimal)
    self.assertTrue(type(food1.nf_protein) == Decimal)
    self.assertTrue(type(food1.serving_qty) == Decimal)

    # check nutrient values
    self.assertTrue(food1.nf_calories == Decimal(1))
    self.assertTrue(food1.nf_total_fat == Decimal(2))
    self.assertTrue(food1.nf_cholesterol == Decimal(3))
    self.assertTrue(food1.nf_saturated_fat == Decimal(4))
    self.assertTrue(food1.nf_sodium == Decimal(5))
    self.assertTrue(food1.nf_total_carbohydrate == Decimal(6))
    self.assertTrue(food1.nf_dietary_fiber == Decimal(7))
    self.assertTrue(food1.nf_sugars == Decimal(8))
    self.assertTrue(food1.nf_protein == Decimal(9))
Exemplo n.º 5
0
  def test_relationships(self):
    food1 = FoodItem(name='food1',
    img_url="",
    nf_calories=Decimal(1),
    nf_total_fat=Decimal(2),
    nf_cholesterol=Decimal(3),
    nf_saturated_fat=Decimal(4),
    nf_sodium=Decimal(5),
    nf_total_carbohydrate=Decimal(6),
    nf_dietary_fiber=Decimal(7),
    nf_sugars=Decimal(8),
    nf_protein=Decimal(9))

    food2 = FoodItem(name='food2',
    img_url="",
    nf_calories=Decimal(11),
    nf_total_fat=Decimal(12),
    nf_cholesterol=Decimal(13),
    nf_saturated_fat=Decimal(14),
    nf_sodium=Decimal(15),
    nf_total_carbohydrate=Decimal(16),
    nf_dietary_fiber=Decimal(17),
    nf_sugars=Decimal(18),
    nf_protein=Decimal(19))

    cart1 = Cart()
    cart2 = Cart()

    user1 = User(email="*****@*****.**",username="******",password="******")
    user2 = User(email="*****@*****.**",username="******",password="******")

    food1.cart = cart1
    cart1.user = user1
    food2.cart = cart2
    cart2.user = user2

    # check relationships
    self.assertTrue(food1.cart == cart1)
    self.assertTrue(cart1.user == user1)
    self.assertTrue(food2.cart == cart2)
    self.assertTrue(cart2.user == user2)

    # test relationships are showed in __repr__
    self.assertTrue(food1.__repr__() == 'food1 in Cart of <User one>')
    self.assertTrue(cart1.__repr__() == 'Cart of <User one>')
    self.assertTrue(user1.__repr__() == '<User one>')
Exemplo n.º 6
0
def shopping_list():
    form = AddFoodForm()
    user = User.query.filter_by(username=current_user.username).first()
    shopping_list = user.shopping_list.first()
    if request.method == "PUT":
        list_item = FoodItem.query.filter_by(id=request.args.get('item_id')).first()
        db.session.delete(list_item)
        db.session.commit()
        return '200'
    elif request.method == "POST" and form.validate_on_submit():
        item_name = form.name.data
        item_type = form.food_type.data
        list_id = shopping_list.id
        food_item = FoodItem(name=item_name, food_type=item_type, shopping_list_id=list_id)
        db.session.add(food_item)
        db.session.commit()
        return jsonify({"item_id": food_item.id, "item_name": food_item.name})
    else:
        return render_template('shopping_list.html', title='Shopping List', form=form, shopping_list=shopping_list.items.all())
Exemplo n.º 7
0
  def test_update_nutrients(self):
    food1 = FoodItem(name='food1',
    img_url="",
    nf_calories=Decimal(1),
    nf_total_fat=Decimal(2),
    nf_cholesterol=Decimal(3),
    nf_saturated_fat=Decimal(4),
    nf_sodium=Decimal(5),
    nf_total_carbohydrate=Decimal(6),
    nf_dietary_fiber=Decimal(7),
    nf_sugars=Decimal(8),
    nf_protein=Decimal(9))

    food2 = FoodItem(name='food2',
    img_url="",
    nf_calories=Decimal(11),
    nf_total_fat=Decimal(12),
    nf_cholesterol=Decimal(13),
    nf_saturated_fat=Decimal(14),
    nf_sodium=Decimal(15),
    nf_total_carbohydrate=Decimal(16),
    nf_dietary_fiber=Decimal(17),
    nf_sugars=Decimal(18),
    nf_protein=Decimal(19))

    cart2 = Cart()
    food1.cart = cart2
    food2.cart = cart2

    cart2.update_nutrients()
    self.assertTrue(cart2.nf_calories == Decimal(12))
    self.assertTrue(cart2.nf_total_fat == Decimal(14))
    self.assertTrue(cart2.nf_cholesterol == Decimal(16))
    self.assertTrue(cart2.nf_saturated_fat == Decimal(18))
    self.assertTrue(cart2.nf_sodium == Decimal(20))
    self.assertTrue(cart2.nf_total_carbohydrate == Decimal(22))
    self.assertTrue(cart2.nf_dietary_fiber == Decimal(24))
    self.assertTrue(cart2.nf_sugars == Decimal(26))
    self.assertTrue(cart2.nf_protein == Decimal(28))
Exemplo n.º 8
0
    def setUp(self):
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.client = self.app.test_client(use_cookies=True)

        user = User(email="*****@*****.**", username="******", password="******")

        # Add carts to user
        cart1 = Cart()
        cart2 = Cart()
        cart3 = Cart()
        cart4 = Cart()
        cart5 = Cart()
        cart1.user = user
        cart2.user = user
        cart3.user = user
        cart4.user = user
        cart5.user = user

        # Add foods to cart1 and cart2
        food1 = FoodItem(name='food1',
                         img_url="",
                         nf_calories=Decimal(1),
                         nf_total_fat=Decimal(2),
                         nf_cholesterol=Decimal(3),
                         nf_saturated_fat=Decimal(4),
                         nf_sodium=Decimal(5),
                         nf_total_carbohydrate=Decimal(6),
                         nf_dietary_fiber=Decimal(7),
                         nf_sugars=Decimal(8),
                         nf_protein=Decimal(9),
                         serving_qty=Decimal(1),
                         serving_unit="Serving")

        food2 = FoodItem(name='food2',
                         img_url="",
                         nf_calories=Decimal(11),
                         nf_total_fat=Decimal(12),
                         nf_cholesterol=Decimal(13),
                         nf_saturated_fat=Decimal(14),
                         nf_sodium=Decimal(15),
                         nf_total_carbohydrate=Decimal(16),
                         nf_dietary_fiber=Decimal(17),
                         nf_sugars=Decimal(18),
                         nf_protein=Decimal(19),
                         serving_qty=Decimal(1),
                         serving_unit="Serving")

        food1.cart = cart1
        food2.cart = cart1
        cart1.update_nutrients()
        cart2.update_nutrients()

        user2 = User(email='*****@*****.**', username='******', password='******')
        cart6 = Cart()
        cart6.user = user2

        db.session.add_all([
            user, user2, cart1, cart2, cart3, cart4, cart5, cart6, food1, food2
        ])
        db.session.commit()
Exemplo n.º 9
0
def manage_submenu(submenu_name, submenu_hash):
    submenu = Submenu.query.filter_by(submenu_hash=submenu_hash).first()
    foods = FoodItem.query.filter_by(submenu_id=submenu.id).order_by(
        FoodItem.name.asc()).all()
    company = Company.query.filter_by(id=submenu.company_id).first()
    if current_user.is_authenticated:
        if not current_user.is_company_admin(company):
            return redirect(url_for('index'))
    edit_submenu_form = AddSubMenuForm()
    if edit_submenu_form.submit_submenu_form.data:
        if edit_submenu_form.validate_on_submit():
            submenu.name = edit_submenu_form.input_submenu.data,
            submenu.description = edit_submenu_form.submenu_description.data,
            db.session.commit()
            return redirect(
                url_for('manage_submenu',
                        submenu_name=submenu.name,
                        submenu_hash=submenu.submenu_hash))
    elif request.method == 'GET':
        edit_submenu_form.input_submenu.data = submenu.name
        edit_submenu_form.submenu_description.data = submenu.description
    food_item_form = AddFoodItemForm()
    if food_item_form.submit_food_item_form.data:
        if food_item_form.validate_on_submit():
            food_item = FoodItem(
                name=food_item_form.food_item_name.data,
                price=food_item_form.food_item_price.data,
                description=food_item_form.food_item_description.data,
                company_id=submenu.company_id,
                submenu_id=submenu.id)
            for_hash = food_item.name + str(food_item.id) + str(
                submenu.company_id) + str(food_item.price)
            food_item.set_food_item_hash(for_hash)
            if food_item_form.food_item_pic.data:
                pic_query = FoodItem.query.filter_by(
                    cover_pic=str(food_item_form.food_item_pic.data)).first()
                if pic_query is None:
                    food_item_pic_filename = photos.save(
                        food_item_form.food_item_pic.data)
                    food_item.cover_pic = photos.url(food_item_pic_filename)
                else:
                    flash(
                        'Duplicate cover pic name, please choose a different(specific) name for your cover pic.'
                    )
                    return redirect(
                        url_for('manage_submenu',
                                submenu_name=submenu.name,
                                submenu_hash=submenu.submenu_hash))
            db.session.add(food_item)
            db.session.commit()
            return redirect(
                url_for('manage_submenu',
                        submenu_name=submenu.name,
                        submenu_hash=submenu.submenu_hash))
    edit_food_item_form = EditFoodItemForm()
    food_item_list = [(f.id, f.name) for f in foods]
    edit_food_item_form.edit_food_item_name.choices = food_item_list
    if edit_food_item_form.validate_on_submit():
        edit_food_item = FoodItem.query.filter_by(
            id=edit_food_item_form.edit_food_item_name.data).first()
        edit_food_item.description = edit_food_item_form.edit_item_description.data
        if edit_food_item_form.edit_food_item_pic.data:
            edit_pic_query = FoodItem.query.filter_by(cover_pic=str(
                edit_food_item_form.edit_food_item_pic.data)).first()
            if edit_pic_query is None:
                food_item_pic_filename = photos.save(
                    edit_food_item_form.edit_food_item_pic.data)
                edit_food_item.cover_pic = photos.url(food_item_pic_filename)
            else:
                flash(
                    'Duplicate cover pic name, please choose a different(specific) name for your cover pic.'
                )
                return redirect(
                    url_for('manage_submenu',
                            submenu_name=submenu.name,
                            submenu_hash=submenu.submenu_hash))
        db.session.commit()
        return redirect(
            url_for('manage_submenu',
                    submenu_name=submenu.name,
                    submenu_hash=submenu.submenu_hash))
    return render_template('submenu.html',
                           submenu=submenu,
                           edit_submenu_form=edit_submenu_form,
                           food_item_form=food_item_form,
                           foods=foods,
                           edit_food_item_form=edit_food_item_form)