From e4bd680fe2a86cd7b580ff50422051de034a25d2 Mon Sep 17 00:00:00 2001 From: Laetitia Getti Date: Mon, 31 Jul 2023 12:24:48 +0200 Subject: [PATCH] simplifying code --- .../templates/order/grouped_order_detail.html | 16 ++++++------ .../order/grouped_order_overview.html | 4 +-- .../order/templates/order/order_detail.html | 2 +- .../test_views/test_views_grouped_order.py | 26 ++++++++++--------- .../tests/test_views/test_views_order.py | 4 +-- la_chariotte/order/urls.py | 2 +- la_chariotte/order/views/__init__.py | 2 +- la_chariotte/order/views/order.py | 26 ++++++++----------- pyproject.toml | 2 +- 9 files changed, 41 insertions(+), 43 deletions(-) diff --git a/la_chariotte/order/templates/order/grouped_order_detail.html b/la_chariotte/order/templates/order/grouped_order_detail.html index 48e66ca..0632781 100644 --- a/la_chariotte/order/templates/order/grouped_order_detail.html +++ b/la_chariotte/order/templates/order/grouped_order_detail.html @@ -138,15 +138,15 @@

-

+

-

+

-

+

-

+

@@ -170,13 +170,13 @@ function findTotal(){ var inputs = document.getElementsByTagName("input"); total_price = 0; - for (x = 0 ; x < inputs.length ; x++){ - input_name = inputs[x].getAttribute("name"); + for(let input of inputs){ + input_name = input.getAttribute("name"); if(input_name.indexOf("quantity_")==0){ // get the quantities inputs only item_id = input_name.split("_")[1]; prices = {{ prices_dict | safe }}; - total_price += prices[item_id] * inputs[x].value; - console.log(prices[item_id] * inputs[x].value); + total_price += prices[item_id] * input.value; + console.log(prices[item_id] * input.value); } } console.log(total_price); diff --git a/la_chariotte/order/templates/order/grouped_order_overview.html b/la_chariotte/order/templates/order/grouped_order_overview.html index 320267a..6f11842 100644 --- a/la_chariotte/order/templates/order/grouped_order_overview.html +++ b/la_chariotte/order/templates/order/grouped_order_overview.html @@ -182,7 +182,7 @@

{% for item in order.ordered_items.all %} - {{ item.nb }} x {{ item.item.name }} + {{ item.nb }} × {{ item.item.name }}
{% endfor %}
@@ -214,7 +214,7 @@
{% for item in order.ordered_items.all %} - {{ item.nb }} x {{ item.item.name }} + {{ item.nb }} × {{ item.item.name }}
{% endfor %}
diff --git a/la_chariotte/order/templates/order/order_detail.html b/la_chariotte/order/templates/order/order_detail.html index 7628e0f..84cb558 100644 --- a/la_chariotte/order/templates/order/order_detail.html +++ b/la_chariotte/order/templates/order/order_detail.html @@ -15,7 +15,7 @@

Votre commande

    {% for item in order.ordered_items.all %} -
  • {{ item.nb }} x {{ item.item }} : {{ item.get_price }} €
  • +
  • {{ item.nb }} × {{ item.item }} : {{ item.get_price }} €
  • {% endfor %}

Prix total de la commande : {{ order.price }} € diff --git a/la_chariotte/order/tests/test_views/test_views_grouped_order.py b/la_chariotte/order/tests/test_views/test_views_grouped_order.py index ad1eed7..e6e6bed 100644 --- a/la_chariotte/order/tests/test_views/test_views_grouped_order.py +++ b/la_chariotte/order/tests/test_views/test_views_grouped_order.py @@ -258,14 +258,15 @@ class TestGroupedOrderDetailView: ) item.refresh_from_db() item2.refresh_from_db() - assert models.OrderAuthor.objects.first().first_name == "Prénom" - assert models.OrderAuthor.objects.first().email == "test@mail.fr" - assert models.OrderAuthor.objects.first().phone == "0645632569" - assert models.Order.objects.first().note == "note test" - assert models.Order.objects.first().created_date.date() == timezone.now().date() + author = models.OrderAuthor.objects.first() + order = models.Order.objects.first() + assert author.first_name == "Prénom" + assert author.email == "test@mail.fr" + assert author.phone == "0645632569" + assert order.note == "note test" + assert order.created_date.date() == timezone.now().date() assert item.ordered_nb == 4 assert item2.ordered_nb == 1 - order = models.Order.objects.first() assert order.ordered_items.count() == 2 assert order.articles_nb == 5 assert order.price == 9 @@ -321,12 +322,13 @@ class TestGroupedOrderDetailView: response.context["error_message"] == "Veuillez commander au moins un produit" ) - assert response.context["first_name"] == "Prénom test" - assert "Prénom test" in response.content.decode() - assert "Nom test" in response.content.decode() - assert "0645632569" in response.content.decode() - assert "test@mail.fr" in response.content.decode() - assert "test note" in response.content.decode() + assert response.context["author"].first_name == "Prénom test" + content = response.content.decode() + assert "Prénom test" in content + assert "Nom test" in content + assert "0645632569" in content + assert "test@mail.fr" in content + assert "test note" in content assert not models.Order.objects.first() assert not models.OrderAuthor.objects.first() diff --git a/la_chariotte/order/tests/test_views/test_views_order.py b/la_chariotte/order/tests/test_views/test_views_order.py index 0bde19a..38e4d38 100644 --- a/la_chariotte/order/tests/test_views/test_views_order.py +++ b/la_chariotte/order/tests/test_views/test_views_order.py @@ -15,7 +15,7 @@ pytestmark = pytest.mark.django_db class TestOrder: - def test_order_deadline_passed(self, client, other_user): + def test_order_deadline_passed__raise_errors(self, client, other_user): """A user orders when the deadline is passed. They get a 403 error""" grouped_order = create_grouped_order( days_before_delivery_date=5, @@ -47,7 +47,7 @@ class TestOrder: ) assert response.status_code == 403 - def test_order_deadline_passed__orga(self, client_log): + def test_order_deadline_passed__still_works_for_orga(self, client_log): """The orga user orders when the deadline is passed, but the delivery is to come. It works""" grouped_order = create_grouped_order( diff --git a/la_chariotte/order/urls.py b/la_chariotte/order/urls.py index 44c6971..dc1706e 100644 --- a/la_chariotte/order/urls.py +++ b/la_chariotte/order/urls.py @@ -13,7 +13,7 @@ urlpatterns = [ views.GroupedOrderOverview.as_view(), name="grouped_order_overview", ), - path("/commander/", views.order, name="order"), + path("/commander/", views.place_order, name="order"), path( "//confirmation/", views.OrderDetailView.as_view(), diff --git a/la_chariotte/order/views/__init__.py b/la_chariotte/order/views/__init__.py index 482d6b8..0307e86 100644 --- a/la_chariotte/order/views/__init__.py +++ b/la_chariotte/order/views/__init__.py @@ -7,6 +7,6 @@ from .grouped_order import (DownloadGroupedOrderSheetView, GroupedOrderSheetView, GroupedOrderUpdateView, IndexView) from .item import ItemCreateView, ItemDeleteView -from .order import OrderDeleteView, OrderDetailView, order +from .order import OrderDeleteView, OrderDetailView, place_order # fmt: on diff --git a/la_chariotte/order/views/order.py b/la_chariotte/order/views/order.py index 5debfda..0833f22 100644 --- a/la_chariotte/order/views/order.py +++ b/la_chariotte/order/views/order.py @@ -7,17 +7,19 @@ from django.views import generic from ..models import GroupedOrder, Order, OrderAuthor, OrderedItem -def order(request, grouped_order_id): +def place_order(request, grouped_order_id): """Creates an AnonymousUser, and an Order for this GroupedOrder, with related OrderedItems""" grouped_order = get_object_or_404(GroupedOrder, pk=grouped_order_id) # check if the grouped order is ongoing or if the user is allowed to order - if request.user != grouped_order.orga: - if not grouped_order.is_ongoing(): - return http.HttpResponseForbidden() - else: - if not grouped_order.is_to_be_delivered(): - return http.HttpResponseForbidden() + user_is_orga = request.user == grouped_order.orga + is_to_be_delivered = grouped_order.is_to_be_delivered() + is_ongoing = grouped_order.is_ongoing() + + access_allowed = is_ongoing or (user_is_orga and is_to_be_delivered) + + if not access_allowed: + return http.HttpResponseForbidden() # get a dict with quantity_{{item_id}}:[ {{ quantity on phone }},{{ quantity on desktop }} ] orders_dict = { @@ -69,11 +71,8 @@ def order(request, grouped_order_id): { "grouped_order": grouped_order, "error_message": error_message, - "first_name": author.first_name, - "last_name": author.last_name, - "phone": author.phone, - "email": author.email, "note": order.note, + "author": author, }, ) @@ -90,11 +89,8 @@ def order(request, grouped_order_id): { "grouped_order": grouped_order, "error_message": error_message, - "first_name": author.first_name, - "last_name": author.last_name, - "phone": author.phone, - "email": author.email, "note": order.note, + "author": author, }, ) diff --git a/pyproject.toml b/pyproject.toml index cfa75ec..42e4aca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ dev = [ [tool.pytest.ini_options] DJANGO_SETTINGS_MODULE = "la_chariotte.settings" -addopts = "--isort --black --reuse-db --cov-report xml --cov-report term-missing --cov=la_chariotte -p no:warnings" +addopts = "-x --ff --isort --black --reuse-db --cov-report xml --cov-report term-missing --cov=la_chariotte -p no:warnings" isort_ignore = ["*migrations/*.py"] [tool.isort]