Skip to content

Commit e2352f2

Browse files
yftacherzogRawanAbuleil
authored andcommitted
Merge pull request #95 from LiorSilberman/supplier_deli
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.
2 parents d2c4c72 + c0d5542 commit e2352f2

File tree

8 files changed

+213
-25
lines changed

8 files changed

+213
-25
lines changed

buy_together/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
path('products/', include('product.urls')),
2424
path('suppliers/', include('supplier.urls')),
2525
path('client/', include('client.urls')),
26-
path('ordered_product/', include('ordered_product.urls'))
26+
path('ordered_product/', include('ordered_product.urls')),
27+
path('supplier_product/', include('supplier_product.urls', namespace='supplier_product')),
2728
]

static/css/suppliers.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ body{
99
}
1010

1111
.container-A{
12-
background-color: rgb(229, 127, 2);
12+
background-color: rgb(34, 214, 234);
1313
height: 500px;
14-
width:20%
14+
overflow-y: scroll;
15+
position: relative;
16+
width: 25%;
1517
}
1618

1719
.container-B{background-color: brown;

supplier/tests/test_supplier_page.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from django.contrib.auth.models import User
44
from supplier.models import Supplier
55
from supplier.views import action_display_all_products, get_supplier
6-
from supplier.views import get_product_by_name_and_supplier, action_search_product
6+
from supplier.views import get_product_by_name_and_supplier, action_search_product, show_deliveries
77
from django.test import RequestFactory
88
from supplier import views
99
from supplier_product.models import SupplierProduct
1010
from product.models import Product
11+
from delivery_location.models import DeliveryLocation
1112

1213

1314
@pytest.fixture
@@ -130,40 +131,82 @@ def test_get_product_return_none(request_searched_product_mock,
130131
@pytest.mark.django_db()
131132
def test_searched_product_with_data(get_request_with_data,
132133
render_mock,
133-
saved_supplier_product0):
134-
response = action_search_product(get_request_with_data, saved_supplier_product0)
134+
saved_supplier_product0, saved_supplier0):
135+
136+
context = show_deliveries(saved_supplier0)
137+
response = action_search_product(get_request_with_data, saved_supplier_product0, context)
135138
assert response == render_mock.return_value
136139

137-
data = {'supplier_products': saved_supplier_product0}
140+
# data = {'supplier_products': saved_supplier_product0}
141+
context['supplier_products'] = saved_supplier_product0
138142
assert render_mock.call_args_list == [
139-
call(get_request_with_data, 'supplier/suppliers.html', data)
143+
call(get_request_with_data, 'supplier/suppliers.html', context)
140144
]
141145

142146

143147
@pytest.mark.django_db()
144148
def test_searched_product_without_data(get_request_with_data,
145149
render_mock,
146-
messages_mock):
147-
response = action_search_product(get_request_with_data, None)
150+
messages_mock, saved_supplier0):
151+
context = show_deliveries(saved_supplier0)
152+
response = action_search_product(get_request_with_data, None, context)
148153
assert response == render_mock.return_value
149154
messages_mock.info.assert_called_with(
150155
get_request_with_data, 'Product Not Found'
151156
)
152157
assert render_mock.call_args_list == [
153-
call(get_request_with_data, 'supplier/suppliers.html')
158+
call(get_request_with_data, 'supplier/suppliers.html', context)
154159
]
155160

156161

157162
@pytest.mark.django_db()
158163
def test_action_display_all_products(get_request,
159164
saved_supplier0,
160-
render_mock,
161-
saved_supplier_product0,
162-
saved_supplier_product1):
163-
response = action_display_all_products(get_request, saved_supplier0)
165+
render_mock):
166+
context = show_deliveries(saved_supplier0)
167+
response = action_display_all_products(get_request, saved_supplier0, context)
164168
assert response == render_mock.return_value
165169

166170
data = set(SupplierProduct.objects.filter(user_name=saved_supplier0))
171+
context['supplier_products'] = data
167172
assert render_mock.call_args_list == [
168-
call(get_request, 'supplier/suppliers.html', {'supplier_products': data})
173+
call(get_request, 'supplier/suppliers.html', context)
169174
]
175+
176+
177+
@pytest.mark.django_db()
178+
def test_supplier_deliveries(client, delivery_location0):
179+
delivery_location0.add_delivery_location()
180+
client.force_login(user=delivery_location0.user_name.supplier_account)
181+
response = client.get('/suppliers/')
182+
assert list(response.context['supplier_deliveries']) == [delivery_location0]
183+
184+
185+
@pytest.mark.django_db()
186+
def test_create_delivery(client, saved_supplier0):
187+
client.force_login(user=saved_supplier0.supplier_account)
188+
response = client.get('/suppliers/?location=tel+aviv&date=2023-01-18&add=Add')
189+
assert len(DeliveryLocation.objects.filter(user_name=saved_supplier0)) != 0
190+
assert response.status_code == 302
191+
assert client.get('/suppliers/').status_code == 200
192+
193+
194+
@pytest.mark.django_db()
195+
def test_show_deliveries_with_no_deliveries(client, saved_supplier0):
196+
client.force_login(user=saved_supplier0.supplier_account)
197+
response = client.get('/suppliers/')
198+
assert len(DeliveryLocation.objects.filter(user_name=saved_supplier0)) == 0
199+
assert response.status_code == 200
200+
assert len(list(response.context['supplier_deliveries'])) == 0
201+
assert response.context['message'] == "You don't have any deliveries yet"
202+
203+
204+
@pytest.mark.django_db()
205+
def test_remove_delivery(client, saved_supplier0):
206+
client.force_login(user=saved_supplier0.supplier_account)
207+
client.get('/suppliers/?location=tel+aviv&date=2023-01-18&add=Add')
208+
assert len(DeliveryLocation.objects.filter(user_name=saved_supplier0)) != 0
209+
response = client.get('/suppliers/?location=tel+aviv&date=2023-01-18&delete=Delete')
210+
assert len(DeliveryLocation.objects.filter(user_name=saved_supplier0)) == 0
211+
assert response.status_code == 302
212+
assert client.get('/suppliers/').status_code == 200

supplier/views.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from django.shortcuts import render
22
from supplier.models import Supplier
3+
from delivery_location.models import DeliveryLocation
34
from django.contrib.auth.decorators import user_passes_test
45
from supplier_product.models import SupplierProduct
56
from product.models import Product
67
from django.contrib import messages
8+
from django.shortcuts import redirect
79

810

911
def get_supplier(user):
@@ -24,23 +26,61 @@ def get_product_by_name_and_supplier(request, supplier):
2426
return None
2527

2628

27-
def action_search_product(request, supplier_product):
29+
def action_search_product(request, supplier_product, context):
2830
if supplier_product:
29-
return render(request, 'supplier/suppliers.html', {'supplier_products': supplier_product})
31+
context['supplier_products'] = supplier_product
32+
return render(request, 'supplier/suppliers.html', context)
3033
else:
3134
messages.info(request, 'Product Not Found')
32-
return render(request, 'supplier/suppliers.html')
35+
return render(request, 'supplier/suppliers.html', context)
3336

3437

35-
def action_display_all_products(request, supplier):
38+
def action_display_all_products(request, supplier, context):
3639
data = set(SupplierProduct.objects.filter(user_name=supplier))
37-
return render(request, 'supplier/suppliers.html', {'supplier_products': data})
40+
context['supplier_products'] = data
41+
return render(request, 'supplier/suppliers.html', context)
3842

3943

4044
@user_passes_test(get_supplier, login_url='Not allowed')
4145
def suppliers_page(request):
4246
supplier = get_supplier(request.user)
47+
context = show_deliveries(supplier)
48+
49+
if 'add' in request.GET:
50+
location = request.GET['location'].capitalize()
51+
date = request.GET['date']
52+
create_delivery(supplier, location, date)
53+
return redirect('/suppliers/')
54+
55+
if 'delete' in request.GET:
56+
location = request.GET['location']
57+
date = request.GET['date']
58+
remove_delivery(supplier, location, date)
59+
return redirect('/suppliers/')
60+
4361
if 'searched_product' in request.GET:
44-
return action_search_product(request, get_product_by_name_and_supplier(request, supplier))
62+
return action_search_product(request, get_product_by_name_and_supplier(request, supplier), context)
63+
64+
return action_display_all_products(request, supplier, context)
65+
66+
67+
def show_deliveries(supplier):
68+
supplier_deliveries = DeliveryLocation.objects.filter(user_name=supplier)
69+
message = ''
70+
if len(supplier_deliveries) == 0:
71+
message = "You don't have any deliveries yet"
72+
return {'message': message, 'supplier_deliveries': supplier_deliveries}
73+
74+
75+
def create_delivery(supplier, location, date):
76+
delivery = DeliveryLocation.objects.filter(user_name=supplier, location__icontains=location, date=date).first()
77+
if delivery is not None:
78+
return
79+
80+
DeliveryLocation(user_name=supplier, location=location, date=date).add_delivery_location()
81+
4582

46-
return action_display_all_products(request, supplier)
83+
def remove_delivery(supplier, location, date):
84+
delivery = DeliveryLocation.objects.filter(user_name=supplier, location__icontains=location, date=date).first()
85+
if delivery is not None:
86+
delivery.remove_delivery_location()

supplier_product/test_views.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from django.test import Client
2+
from django.urls import reverse
3+
from .models import SupplierProduct
4+
import pytest
5+
6+
7+
@pytest.mark.django_db()
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
17+
18+
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.urls import path
2+
from . import views
3+
4+
5+
app_name = 'supplier_product'
6+
urlpatterns = [
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'),
9+
]

supplier_product/views.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from django.shortcuts import redirect
2+
from supplier_product.models import SupplierProduct
3+
from django.template.exceptions import TemplateDoesNotExist
4+
from django.http import HttpResponse
5+
from django.db.models import F
6+
7+
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")
14+
context = {'product': product}
15+
try:
16+
return redirect(request.META.get('HTTP_REFERER', '/'), context)
17+
except TemplateDoesNotExist:
18+
return HttpResponse("Template not found")
19+
20+
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)
29+
else:
30+
try:
31+
context = {'product': product}
32+
return redirect(request.META.get('HTTP_REFERER', '/'), context)
33+
except TemplateDoesNotExist:
34+
return HttpResponse("Template not found")
35+
try:
36+
context = {'product': product}
37+
return redirect(request.META.get('HTTP_REFERER', '/'), context)
38+
except TemplateDoesNotExist:
39+
return HttpResponse("Template not found")

templates/supplier/suppliers.html

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,29 @@
1414
{% include 'buy_together_app/help_parts/nav.html' %}
1515

1616
<div class="container">
17-
<div class="container-A d-flex justify-content-center">
18-
<dov> here will be something</p>
17+
<div class="container-A justify-content-center" style="background-color: rgb(34, 214, 234);;">
18+
<p style="font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; margin-left: 60px">
19+
{% if not supplier_deliveries %}
20+
<p style="color: #000; font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; left: 10px; position: relative; left: 8px;">{{message}}</p>
21+
{% else %}
22+
<ul style="font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;">
23+
{% for delivery in supplier_deliveries %}
24+
<li>
25+
Location: {{delivery.location}}<br>
26+
Date: {{delivery.date}}<br><br>
27+
</li>
28+
{% endfor %}
29+
</ul>
30+
{% endif %}
31+
</p>
32+
<form method = "get" style="font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; margin-left: 30px;">
33+
<input id="enter_location" type="text" placeholder="Enter location" required name="location"><br>
34+
<input id="add_date" type="date" placeholder="Add date" required name="date">
35+
<p>
36+
<input type="submit" value="Add" name="add" style="color: green;">
37+
<input type="submit" value="Delete" name="delete" style="color: red;">
38+
</p>
39+
</form>
1940
</div>
2041
<div class="container-B">
2142

@@ -53,7 +74,12 @@
5374
</button>
5475
</form>
5576
</div>
77+
78+
<div class="container_A d-flex justify-content-center">
79+
<p> here will be something</p>
80+
</div> -->
5681

82+
</div>
5783
</div>
5884
</body>
5985

0 commit comments

Comments
 (0)