示例#1
0
    def test_form_invalid_decimal_price(self):
        """Test an invalid form with a decimal with three decimal places"""
        form = AdjustMenuForm(
            data={
                'id': 100,
                'name': "taco",
                'price': 10.555,
                'description': "nice good food",
                'course': "main",
                'category': "tacos",
                'allergy': "uyhg",
                'calories': 99,
                'image': "imgur.com/sef32",
                'vegetarian': False,
                'vegan': False,
                'meat': False,
                'stock': 5,
                'cost': 10
            })

        self.assertFalse(form.is_valid())
示例#2
0
    def test_form_invalid_cost_too_large(self):
        """Test invalid form with a cost that is too large"""
        form = AdjustMenuForm(
            data={
                'id': 100,
                'name': "taco",
                'price': 6,
                'description': "nice good food",
                'course': "main",
                'category': "tacos",
                'allergy': "uyhg",
                'calories': 99,
                'image': "imgur.com/sef32",
                'vegetarian': False,
                'vegan': False,
                'meat': False,
                'stock': 5,
                'cost': 100 * 10000000000000000000000
            })

        self.assertFalse(form.is_valid())
示例#3
0
    def test_form_valid(self):
        """Test form validation is valid"""
        form = AdjustMenuForm(
            data={
                'id': 100,
                'name': "taco",
                'price': 10,
                'description': "nice good food",
                'course': "main",
                'category': "tacos",
                'allergy': "uyhg",
                'calories': 99,
                'image': "imgur.com/sef32",
                'vegetarian': False,
                'vegan': False,
                'meat': False,
                'stock': 5,
                'cost': 10
            })

        self.assertTrue(form.is_valid())
示例#4
0
    def test_form_invalid_long_name(self):
        """Test an invalid form with a name that's over the character count"""
        form = AdjustMenuForm(
            data={
                'id': 100,
                'name': "a" * 10000,
                'price': 10,
                'description': "nice good food",
                'course': "main",
                'category': "tacos",
                'allergy': "uyhg",
                'calories': 99,
                'image': "imgur.com/sef32",
                'vegetarian': False,
                'vegan': False,
                'meat': False,
                'stock': 5,
                'cost': 10
            })

        self.assertFalse(form.is_valid())
示例#5
0
def adjust_menu(request):
    """Return the menu in formatted HTML and update the table based on inputs by the manager."""
    if request.method == "POST":

        form_name = request.POST['menu_name']
        form_price = request.POST['menu_price']
        form_description = request.POST['menu_description']
        form_course = request.POST['menu_course']
        form_category = request.POST['menu_category']
        form_allergy = request.POST['menu_allergy']
        form_calories = request.POST['menu_calories']
        form_image = request.POST['menu_image'],
        form_vegetarian = request.POST['menu_vegetarian']
        form_vegan = request.POST['menu_vegan']
        form_meat = request.POST['menu_meat']
        form_stock = request.POST['menu_stock']
        form_cost = request.POST['menu_cost']

        # VALIDATION: check if form inputs are valid then send it to database
        form = AdjustMenuForm(
            data={
                'name': form_name,
                'price': form_price,
                'description': form_description,
                'course': form_course,
                'category': form_category,
                'allergy': form_allergy,
                'calories': form_calories,
                'image': form_image,
                'vegetarian': form_vegetarian,
                'vegan': form_vegan,
                'meat': form_meat,
                'stock': form_stock,
                'cost': form_cost
            })

        # if the confirm change button was pressed, check form for validation and update menu
        if form.is_valid():
            if 'confirm' in request.POST:
                print("item changed")
                Menu.objects.filter(pk=request.POST['menu_id']).update(
                    name=form_name,
                    price=form_price,
                    description=form_description,
                    course=form_course,
                    category=form_category,
                    allergy=form_allergy,
                    calories=form_calories,
                    image=form_image,
                    vegetarian=form_vegetarian,
                    vegan=form_vegan,
                    meat=form_meat,
                    stock=form_stock)

            # if the delete button was pressed, remove the item from menu
            elif 'delete' in request.POST:
                Menu.objects.filter(pk=request.POST['menu_id']).delete()

            elif 'add_item' in request.POST:
                Menu.objects.create(name=form_name,
                                    price=form_price,
                                    description=form_description,
                                    course=form_course,
                                    category=form_category,
                                    allergy=form_allergy,
                                    calories=form_calories,
                                    image=form_image,
                                    vegetarian=form_vegetarian,
                                    vegan=form_vegan,
                                    meat=form_meat,
                                    stock=form_stock)

    context = {"menu": Menu.objects.all()}
    return render(request, 'manager/managermenu.html', context)