Skip to content

Commit 8e01a65

Browse files
committed
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 *** no changes in the suppliers html file
1 parent 0bb94ee commit 8e01a65

File tree

5 files changed

+43
-59
lines changed

5 files changed

+43
-59
lines changed

buy_together/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@
2525
path('client/', include('client.urls')),
2626
path('ordered_product/', include('ordered_product.urls')),
2727
path('supplier_product/', include('supplier_product.urls', namespace='supplier_product')),
28+
2829
]

supplier_product/test_views.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
1-
from django.test import TestCase, Client
1+
from django.test import Client
22
from django.urls import reverse
3-
from .models import SupplierProduct, Product, Supplier
4-
from django.contrib.auth.models import User
3+
from .models import SupplierProduct
54
import pytest
65

76

87
@pytest.mark.django_db()
9-
class IncreaseQuantityTest(TestCase):
10-
def setUp(self):
11-
# Create a test client
12-
self.client = Client()
13-
# Create a test user
14-
self.user = User.objects.create_user(username='testuser', password='testpassword')
15-
self.supplier = Supplier.objects.create(supplier_account=self.user)
16-
self.product = Product.objects.create(product_name='product1')
17-
self.supplier_product = SupplierProduct.objects.create(
18-
supplier_product_id=1,
19-
qr_code=self.product,
20-
user_name=self.supplier,
21-
price=100,
22-
quantity=10
23-
)
8+
def test_increase_quantity(saved_supplier_product0):
9+
supplier_product = saved_supplier_product0
10+
client = Client()
11+
n = 3
12+
response = client.get(
13+
reverse('supplier_product:increase_quantity', kwargs={'id': supplier_product.supplier_product_id, 'n': n}))
14+
updated_product = SupplierProduct.objects.get(supplier_product_id=supplier_product.supplier_product_id)
15+
assert updated_product.quantity == supplier_product.quantity + n
16+
assert response.status_code == 302
2417

25-
def increase_quantity_by_one(self):
26-
response = self.client.get(
27-
reverse(
28-
'supplier_product:increase_quantity_by_one', kwargs={'id': self.supplier_product.supplier_product_id}))
29-
updated_product = SupplierProduct.objects.get(supplier_product_id=self.supplier_product.supplier_product_id)
30-
self.assertEqual(updated_product.quantity, self.supplier_product.quantity + 1)
31-
self.assertEqual(response.status_code, 302)
3218

33-
def decrease_quantity_by_one(self):
34-
response = self.client.get(
35-
reverse(
36-
'supplier_product:decrease_quantity_by_one', kwargs={'id': self.supplier_product.supplier_product_id}))
37-
updated_product = SupplierProduct.objects.get(supplier_product_id=self.supplier_product.supplier_product_id)
38-
self.assertEqual(updated_product.quantity, self.supplier_product.quantity - 1)
39-
self.assertEqual(response.status_code, 302)
19+
@pytest.mark.django_db()
20+
def test_decrease_quantity(saved_supplier_product0):
21+
supplier_product = saved_supplier_product0
22+
client = Client()
23+
n = 3
24+
response = client.get(
25+
reverse('supplier_product:decrease_quantity', kwargs={'id': supplier_product.supplier_product_id, 'n': n}))
26+
updated_product = SupplierProduct.objects.get(supplier_product_id=supplier_product.supplier_product_id)
27+
assert updated_product.quantity == supplier_product.quantity - n
28+
assert response.status_code == 302

supplier_product/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
app_name = 'supplier_product'
66
urlpatterns = [
7-
path('increase_quantity_by_one/<int:id>/', views.increase_quantity, name='increase_quantity'),
8-
path('decrease_quantity_by_one/<int:id>/', views.decrease_quantity, name='decrease_quantity'),
7+
path('increase_quantity/<int:id>/<int:n>/', views.increase_quantity, name='increase_quantity'),
8+
path('decrease_quantity/<int:id>/<int:n>/', views.decrease_quantity, name='decrease_quantity'),
99
]

supplier_product/views.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,35 @@
55
from django.db.models import F
66

77

8-
def increase_quantity_by_one(request, id):
9-
SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')+1)
10-
product = SupplierProduct.objects.get(supplier_product_id=id)
8+
def increase_quantity(request, id, n):
9+
SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')+n)
10+
try:
11+
product = SupplierProduct.objects.get(supplier_product_id=id)
12+
except SupplierProduct.DoesNotExist:
13+
raise HttpResponse("Product does not exist")
1114
context = {'product': product}
1215
try:
1316
return redirect(request.META.get('HTTP_REFERER', '/'), context)
14-
except TemplateDoesNotExist as e:
15-
print(f'Template not found: {e}')
17+
except TemplateDoesNotExist:
1618
return HttpResponse("Template not found")
1719

1820

19-
def decrease_quantity_by_one(request, id):
20-
product = SupplierProduct.objects.get(supplier_product_id=id)
21-
if product.quantity > 0:
22-
SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')-1)
21+
def decrease_quantity(request, id, n):
22+
try:
23+
product = SupplierProduct.objects.get(supplier_product_id=id)
24+
except SupplierProduct.DoesNotExist:
25+
raise HttpResponse("Product does not exist")
26+
context = {'product': product}
27+
if product.quantity > n:
28+
SupplierProduct.objects.filter(supplier_product_id=id).update(quantity=F('quantity')-n)
2329
else:
2430
try:
2531
context = {'product': product}
2632
return redirect(request.META.get('HTTP_REFERER', '/'), context)
27-
except TemplateDoesNotExist as e:
28-
print(f'Template not found: {e}')
33+
except TemplateDoesNotExist:
2934
return HttpResponse("Template not found")
3035
try:
3136
context = {'product': product}
3237
return redirect(request.META.get('HTTP_REFERER', '/'), context)
33-
except TemplateDoesNotExist as e:
34-
print(f'Template not found: {e}')
38+
except TemplateDoesNotExist:
3539
return HttpResponse("Template not found")

templates/supplier/suppliers.html

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<body>
1414
{% include 'buy_together_app/help_parts/nav.html' %}
15+
1516
<div class="container">
1617
<div class="container-A d-flex justify-content-center">
1718
<dov> here will be something</p>
@@ -28,7 +29,6 @@
2829
<th scope="col">Product name</th>
2930
<th scope="col">Quantity</th>
3031
<th scope="col">Price</th>
31-
<th scope="col">Actions</th>
3232
</tr>
3333
</thead>
3434
<tbody>
@@ -37,17 +37,7 @@
3737
<td>{{sup_product.qr_code.product_name}}</td>
3838
<td>{{sup_product.quantity}}</td>
3939
<td>{{sup_product.price}}</td>
40-
<td><form action="{% url 'supplier_product:increase_quantity_by_one' id=sup_product.supplier_product_id %}">
41-
<button type="submit" class="btn btn-primary"> + </button>
42-
</form>
43-
<form action="{% url 'supplier_product:decrease_quantity_by_one' id=sup_product.supplier_product_id %}">
44-
<button type="submit" class="btn btn-danger"> - </button>
45-
</form>
46-
</td>
47-
4840
</tr>
49-
50-
5141
{% endfor %}
5242
</tbody>
5343
</table>
@@ -64,7 +54,7 @@
6454
</form>
6555
</div>
6656

67-
</div>
57+
</div>
6858
</body>
6959

7060
</html>

0 commit comments

Comments
 (0)