Skip to content

Commit

Permalink
implement API to check if user paid for event or not (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcduong authored and odkhang committed Sep 19, 2024
1 parent af60dca commit fa42d17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/pretix/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@
url(r"^upload$", upload.UploadView.as_view(), name="upload"),
url(r"^me$", user.MeView.as_view(), name="user.me"),
url(r"^version$", version.VersionView.as_view(), name="version"),
url(r"(?P<organizer>[^/]+)/(?P<event>[^/]+)/customer/(?P<customer_id>[^/]+)/ticket-check", event.CustomerOrderCheckView.as_view()),
]
38 changes: 37 additions & 1 deletion src/pretix/api/views/event.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import django_filters
from django.db import transaction
from django.db.models import ProtectedError, Q
from django.http import JsonResponse
from django.utils.timezone import now
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from django_scopes import scopes_disabled
Expand All @@ -15,7 +16,7 @@
)
from pretix.api.views import ConditionalListView
from pretix.base.models import (
CartPosition, Device, Event, TaxRule, TeamAPIToken,
CartPosition, Device, Event, TaxRule, TeamAPIToken, Organizer, Customer, Order,
)
from pretix.base.models.event import SubEvent
from pretix.base.settings import SETTINGS_AFFECTING_CSS
Expand Down Expand Up @@ -402,3 +403,38 @@ def patch(self, request, *wargs, **kwargs):
'request': request
})
return Response(s.data)


class CustomerOrderCheckView(views.View):

def get(self, request, *args, **kwargs):
if (not kwargs.get("customer_id")
or not kwargs.get("event")
or not kwargs.get("organizer")):
return Response(status=400, data={"error": "Missing parameters."})

try:
organizer = Organizer.objects.get(slug=kwargs["organizer"])
event = Event.objects.get(slug=kwargs["event"], organizer=organizer)
customer = Customer.objects.get(identifier=kwargs["customer_id"], organizer=organizer)

except Organizer.DoesNotExist:
return JsonResponse(status=404, data={"error": "Organizer not found."})
except Event.DoesNotExist:
return JsonResponse(status=404, data={"error": "Event not found."})
except Customer.DoesNotExist:
return JsonResponse(status=404, data={"error": "Customer not found."})

# Get all orders of customer which belong to this event
order_list = (Order.objects.filter(Q(event=event)
& (Q(customer=customer) | Q(
email__iexact=customer.email))).select_related('event').order_by('-datetime'))

if not order_list:
return JsonResponse(status=404, data={"error": "Customer has no orders for this event."})

for order in order_list:
if order.status == 'p':
return JsonResponse(status=200, data={"message": "Customer has paid orders."})

return JsonResponse(status=400, data={"message": "Customer did not paid orders."})

0 comments on commit fa42d17

Please sign in to comment.