Merge pull request #878 from bornhack/fix_none_unpacking_when_finding_tickets

Ticket finding fix
This commit is contained in:
Thomas Steen Rasmussen 2021-08-19 17:50:07 +02:00 committed by GitHub
commit 1ed70d0a07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View file

@ -1,14 +1,22 @@
import logging import logging
from itertools import chain from itertools import chain
from typing import Optional
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q
from django.utils import timezone from django.utils import timezone
from django.views.generic import ListView, TemplateView from django.views.generic import ListView, TemplateView
from camps.mixins import CampViewMixin from camps.mixins import CampViewMixin
from shop.models import Order, OrderProductRelation from shop.models import Order, OrderProductRelation
from tickets.models import DiscountTicket, ShopTicket, SponsorTicket, TicketType from tickets.models import (
DiscountTicket,
ShopTicket,
SponsorTicket,
TicketType,
TicketTypeUnion,
)
from ..mixins import InfoTeamPermissionMixin from ..mixins import InfoTeamPermissionMixin
@ -49,15 +57,12 @@ class TicketCheckinView(CampViewMixin, InfoTeamPermissionMixin, ListView):
return list(chain(shoptickets, sponsortickets, discounttickets)) return list(chain(shoptickets, sponsortickets, discounttickets))
def _ticket_getter_by_token(token): def _ticket_getter_by_token(token) -> Optional[TicketTypeUnion]:
for ticket_class in [ShopTicket, SponsorTicket, DiscountTicket]: for ticket_class in [ShopTicket, SponsorTicket, DiscountTicket]:
try: try:
return ticket_class.objects.get(token=token), False return ticket_class.objects.get(Q(token=token) | Q(badge_token=token))
except ticket_class.DoesNotExist: except ticket_class.DoesNotExist:
try: continue
return ticket_class.objects.get(badge_token=token), True
except ticket_class.DoesNotExist:
pass
def _ticket_getter_by_pk(pk): def _ticket_getter_by_pk(pk):
@ -88,11 +93,11 @@ class ScanTicketsView(
# Slice to get rid of the first character which is a '#' # Slice to get rid of the first character which is a '#'
ticket_token = self.request.POST.get("ticket_token")[1:] ticket_token = self.request.POST.get("ticket_token")[1:]
ticket, is_badge = _ticket_getter_by_token(ticket_token) ticket: Optional[TicketTypeUnion] = _ticket_getter_by_token(ticket_token)
if ticket: if ticket:
context["ticket"] = ticket context["ticket"] = ticket
context["is_badge"] = is_badge context["is_badge"] = ticket_token == ticket.badge_token
else: else:
messages.warning(self.request, "Ticket not found!") messages.warning(self.request, "Ticket not found!")

View file

@ -222,3 +222,6 @@ class ShopTicket(BaseTicket):
@property @property
def order(self): def order(self):
return self.opr.order return self.opr.order
TicketTypeUnion = Union[ShopTicket, SponsorTicket, DiscountTicket]