Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #849 from trowik/order-fix
Browse files Browse the repository at this point in the history
fix(subscription): fix parser and notifications for orders
  • Loading branch information
David Vogt authored Feb 10, 2022
2 parents 5041daa + 0deaafa commit f835108
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
12 changes: 4 additions & 8 deletions timed/subscription/notify_admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime

from dateutil import parser
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, get_connection
from django.template.loader import get_template, render_to_string
Expand All @@ -14,13 +13,10 @@ def prepare_and_send_email(project, order_duration):

customer = project.customer

duration = parser.parse(order_duration)
hours_added = datetime.timedelta(
days=duration.day,
hours=duration.hour,
minutes=duration.minute,
seconds=duration.second,
)
duration = order_duration.split(":")
hours = int(duration[0])
minutes = int(duration[1])
hours_added = datetime.timedelta(hours=hours, minutes=minutes)

hours_total = hours_added
if project.estimated_time is not None:
Expand Down
56 changes: 52 additions & 4 deletions timed/subscription/tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ def test_order_confirm(
status.HTTP_400_BAD_REQUEST,
),
(True, False, False, False, 1, timedelta(hours=1), status.HTTP_201_CREATED),
(False, True, False, True, 1, timedelta(hours=10), status.HTTP_201_CREATED),
(False, True, False, False, 1, timedelta(hours=24), status.HTTP_201_CREATED),
(False, False, True, True, 1, timedelta(hours=50), status.HTTP_201_CREATED),
(False, False, True, False, 1, timedelta(hours=100), status.HTTP_201_CREATED),
(False, True, False, True, 0, timedelta(hours=10), status.HTTP_201_CREATED),
(False, True, False, False, 0, timedelta(hours=24), status.HTTP_201_CREATED),
(False, False, True, True, 0, timedelta(hours=50), status.HTTP_201_CREATED),
(False, False, True, False, 0, timedelta(hours=100), status.HTTP_201_CREATED),
(False, False, False, True, 0, timedelta(hours=200), status.HTTP_403_FORBIDDEN),
(False, False, False, False, 0, None, status.HTTP_403_FORBIDDEN),
],
Expand Down Expand Up @@ -206,6 +206,54 @@ def test_order_create(
assert url in mail.alternatives[0][0]


@pytest.mark.parametrize(
"duration, expected, status_code",
[
("00:30:00", "0:30:00", status.HTTP_201_CREATED),
("30:00:00", "1 day, 6:00:00", status.HTTP_201_CREATED),
("30:30:00", "1 day, 6:30:00", status.HTTP_201_CREATED),
("-00:30:00", "-0:30:00", status.HTTP_400_BAD_REQUEST),
("-30:00:00", "-1 day, 6:00:00", status.HTTP_400_BAD_REQUEST),
("-30:30:00", "-1 day, 6:30:00", status.HTTP_400_BAD_REQUEST),
],
)
def test_order_create_duration(
auth_client, mailoutbox, duration, expected, status_code
):
user = auth_client.user
project = ProjectFactory.create(estimated_time=timedelta(hours=1))
CustomerAssigneeFactory.create(
user=user, is_customer=True, customer=project.customer
)

data = {
"data": {
"type": "subscription-orders",
"id": None,
"attributes": {
"acknowledged": False,
"duration": duration,
},
"relationships": {
"project": {
"data": {"type": "subscription-projects", "id": project.id}
},
},
}
}

url = reverse("subscription-order-list")

response = auth_client.post(url, data)
assert response.status_code == status_code

if status_code == status.HTTP_201_CREATED:
assert len(mailoutbox) == 1

mail = mailoutbox[0]
assert expected in mail.body


@pytest.mark.parametrize(
"is_customer, is_accountant, is_superuser, acknowledged, expected",
[
Expand Down
9 changes: 8 additions & 1 deletion timed/subscription/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ def create(self, request, *args, **kwargs):
project = Project.objects.get(id=request.data.get("project")["id"])
order_duration = request.data.get("duration")

notify_admin.prepare_and_send_email(project, order_duration)
# only send notification emails if order was created by a customer
# don't allow customers to create orders with negative duration
if not (request.user.is_accountant or request.user.is_superuser):
if "-" in request.data.get("duration"):
raise ValidationError(
"Customer can not create orders with negative duration!"
)
notify_admin.prepare_and_send_email(project, order_duration)
return super().create(request, *args, **kwargs)

@decorators.action(
Expand Down

0 comments on commit f835108

Please sign in to comment.