From a98401466c6056825ebec1c4049b50205c44c5cf Mon Sep 17 00:00:00 2001 From: RawanAbuleil Date: Tue, 17 Jan 2023 18:40:43 +0200 Subject: [PATCH] Creating increase / decrease quantity views functions : ** this code depends on suppliers.html code which hasn't been merged yes once the code is merged i will change it ** two functions has been created : which increase and decrease the quantity of a product by it's supplier *** modifie the code so it increase / decrease the quantity by "n" not one --- buy_together/urls.py | 1 + supplier_product/test_views.py | 51 +++++++++------------- supplier_product/urls.py | 4 +- supplier_product/views.py | 26 +++++++----- templates/supplier/suppliers.html | 70 ------------------------------- 5 files changed, 38 insertions(+), 114 deletions(-) delete mode 100644 templates/supplier/suppliers.html diff --git a/buy_together/urls.py b/buy_together/urls.py index f5615e8..f41f061 100644 --- a/buy_together/urls.py +++ b/buy_together/urls.py @@ -23,4 +23,5 @@ path('products/', include('product.urls')), path('suppliers/', include('supplier.urls')), path('supplier_product/', include('supplier_product.urls', namespace='supplier_product')), + ] diff --git a/supplier_product/test_views.py b/supplier_product/test_views.py index 36b0384..78aa8c6 100644 --- a/supplier_product/test_views.py +++ b/supplier_product/test_views.py @@ -1,39 +1,26 @@ -from django.test import TestCase, Client +from django.test import Client from django.urls import reverse -from .models import SupplierProduct, Product, Supplier -from django.contrib.auth.models import User +from .models import SupplierProduct import pytest @pytest.mark.django_db() -class IncreaseQuantityTest(TestCase): - def setUp(self): - # Create a test client - self.client = Client() - # Create a test user - self.user = User.objects.create_user(username='testuser', password='testpassword') - self.supplier = Supplier.objects.create(supplier_account=self.user) - self.product = Product.objects.create(product_name='product1') - self.supplier_product = SupplierProduct.objects.create( - supplier_product_id=1, - qr_code=self.product, - user_name=self.supplier, - price=100, - quantity=10 - ) +def test_increase_quantity(saved_supplier_product0): + supplier_product = saved_supplier_product0 + client = Client() + n=3 + response = client.get(reverse('supplier_product:increase_quantity', kwargs={'id': supplier_product.supplier_product_id, 'n': n})) + updated_product = SupplierProduct.objects.get(supplier_product_id=supplier_product.supplier_product_id) + assert updated_product.quantity == supplier_product.quantity + n + assert response.status_code == 302 - def increase_quantity_by_one(self): - response = self.client.get( - reverse( - 'supplier_product:increase_quantity_by_one', kwargs={'id': self.supplier_product.supplier_product_id})) - updated_product = SupplierProduct.objects.get(supplier_product_id=self.supplier_product.supplier_product_id) - self.assertEqual(updated_product.quantity, self.supplier_product.quantity + 1) - self.assertEqual(response.status_code, 302) +@pytest.mark.django_db() +def test_decrease_quantity(saved_supplier_product0): + supplier_product = saved_supplier_product0 + client = Client() + n = 3 + response = client.get(reverse('supplier_product:decrease_quantity', kwargs={'id': supplier_product.supplier_product_id, 'n': n})) + updated_product = SupplierProduct.objects.get(supplier_product_id=supplier_product.supplier_product_id) + assert updated_product.quantity == supplier_product.quantity - n + assert response.status_code == 302 - def decrease_quantity_by_one(self): - response = self.client.get( - reverse( - 'supplier_product:decrease_quantity_by_one', kwargs={'id': self.supplier_product.supplier_product_id})) - updated_product = SupplierProduct.objects.get(supplier_product_id=self.supplier_product.supplier_product_id) - self.assertEqual(updated_product.quantity, self.supplier_product.quantity - 1) - self.assertEqual(response.status_code, 302) diff --git a/supplier_product/urls.py b/supplier_product/urls.py index f505160..4015a40 100644 --- a/supplier_product/urls.py +++ b/supplier_product/urls.py @@ -4,6 +4,6 @@ app_name = 'supplier_product' urlpatterns = [ - path('increase_quantity_by_one//', views.increase_quantity, name='increase_quantity'), - path('decrease_quantity_by_one//', views.decrease_quantity, name='decrease_quantity'), + path('increase_quantity///', views.increase_quantity, name='increase_quantity'), + path('decrease_quantity///', views.decrease_quantity, name='decrease_quantity'), ] diff --git a/supplier_product/views.py b/supplier_product/views.py index a382a95..78eb66d 100644 --- a/supplier_product/views.py +++ b/supplier_product/views.py @@ -5,31 +5,37 @@ from django.db.models import F -def increase_quantity_by_one(request, id): - SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')+1) - product = SupplierProduct.objects.get(supplier_product_id=id) +def increase_quantity(request, id, n): + SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')+n) + try: + product = SupplierProduct.objects.get(supplier_product_id=id) + except SupplierProduct.DoesNotExist: + # Handle the exception here, such as returning a 404 error or redirecting to another page + raise HttpResponse("Product does not exist") context = {'product': product} try: return redirect(request.META.get('HTTP_REFERER', '/'), context) except TemplateDoesNotExist as e: - print(f'Template not found: {e}') return HttpResponse("Template not found") -def decrease_quantity_by_one(request, id): - product = SupplierProduct.objects.get(supplier_product_id=id) - if product.quantity > 0: - SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')-1) +def decrease_quantity(request, id, n): + try: + product = SupplierProduct.objects.get(supplier_product_id=id) + except SupplierProduct.DoesNotExist: + raise HttpResponse("Product does not exist") + context = {'product': product} + if product.quantity > n: + SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')-n) else: try: context = {'product': product} return redirect(request.META.get('HTTP_REFERER', '/'), context) except TemplateDoesNotExist as e: - print(f'Template not found: {e}') return HttpResponse("Template not found") try: context = {'product': product} return redirect(request.META.get('HTTP_REFERER', '/'), context) except TemplateDoesNotExist as e: - print(f'Template not found: {e}') return HttpResponse("Template not found") + diff --git a/templates/supplier/suppliers.html b/templates/supplier/suppliers.html deleted file mode 100644 index 535a201..0000000 --- a/templates/supplier/suppliers.html +++ /dev/null @@ -1,70 +0,0 @@ -{% load static %} - - - - - Suppliers - - - - - - - - {% include 'buy_together_app/help_parts/nav.html' %} -
-
- here will be something

-
-
- -
-
-

Your items:

-
- - - - - - - - - - - {% for sup_product in supplier_products %} - - - - - - - - - - {% endfor %} - -
Product nameQuantityPriceActions
{{sup_product.qr_code.product_name}}{{sup_product.quantity}}{{sup_product.price}}
- -
-
- -
-
-
- {% for message in messages %} -

{{message}}

- {% endfor %} -
-
- - -
-
- -
- - - \ No newline at end of file