Skip to content

Commit

Permalink
Creating increase / decrease quantity views functions :
Browse files Browse the repository at this point in the history
** 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
  • Loading branch information
RawanAbuleil committed Jan 19, 2023
1 parent d7e3128 commit a984014
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 114 deletions.
1 change: 1 addition & 0 deletions buy_together/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
path('products/', include('product.urls')),
path('suppliers/', include('supplier.urls')),
path('supplier_product/', include('supplier_product.urls', namespace='supplier_product')),

]
51 changes: 19 additions & 32 deletions supplier_product/test_views.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions supplier_product/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

app_name = 'supplier_product'
urlpatterns = [
path('increase_quantity_by_one/<int:id>/', views.increase_quantity, name='increase_quantity'),
path('decrease_quantity_by_one/<int:id>/', views.decrease_quantity, name='decrease_quantity'),
path('increase_quantity/<int:id>/<int:n>/', views.increase_quantity, name='increase_quantity'),
path('decrease_quantity/<int:id>/<int:n>/', views.decrease_quantity, name='decrease_quantity'),
]
26 changes: 16 additions & 10 deletions supplier_product/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

70 changes: 0 additions & 70 deletions templates/supplier/suppliers.html

This file was deleted.

0 comments on commit a984014

Please sign in to comment.