Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket scan #135

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions insalan/tickets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ def test_get(self) -> None:
response.json(),
{
"user": "user1",
"identity": "",
"token": "00000000-0000-0000-0000-000000000001",
"status": "VA",
"tournament": "Tournament",
"team": None,
},
)

Expand Down
2 changes: 2 additions & 0 deletions insalan/tickets/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
path("scan/<str:token>", views.scan, name="scan"),
path("qrcode/<str:token>", views.qrcode, name="qrcode"),
path("generate/<str:token>", views.generate_pdf, name="generate"),
path("pay", views.pay, name="pay"),
path("unpaid", views.unpaid, name="unpaid"),
]
125 changes: 125 additions & 0 deletions insalan/tickets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser

from insalan.tournament.models import Team, Player, Substitute, Manager, PaymentStatus
from insalan.user.models import User
from .models import Ticket, TicketManager
from insalan.mailer import MailManager
from insalan.settings import EMAIL_AUTH

@api_view(["GET"])
@permission_classes([IsAdminUser])
Expand All @@ -42,11 +45,29 @@ def get(request: HttpRequest, id: str, token: str) -> JsonResponse:
return JsonResponse({'err': _("Ticket non trouvé")},
status=status.HTTP_404_NOT_FOUND)

# Get the team name for the user
player = Player.objects.filter(user=user, team__tournament=ticket.tournament)
if player.exists():
team = player.first().team.name
else:
substitute = Substitute.objects.filter(user=user, team__tournament=ticket.tournament)
if substitute.exists():
team = substitute.first().team.name
else:
manager = Manager.objects.filter(user=user, team__tournament=ticket.tournament)
if manager.exists():
team = manager.first().team.name
else:
team = None

return JsonResponse(
{
"user": user.username,
"identity": user.get_full_name(),
"token": ticket.token,
"status": ticket.status,
"tournament": ticket.tournament.name,
"team": team,
}
)

Expand Down Expand Up @@ -121,3 +142,107 @@ def generate_pdf(request: HttpRequest, token: str) -> JsonResponse:

pdf = TicketManager.generate_ticket_pdf(ticket)
return HttpResponse(pdf, content_type='application/pdf')

@api_view(["POST"])
@permission_classes([IsAdminUser])
def pay(request: HttpRequest) -> JsonResponse:
"""
This view is used to :
- Mark as paid a registration from it's type and id
- Create a ticket for the user that paid
- Send an email to the user with the ticket
"""
data = request.data
print(data)
if 'type' not in data or 'id' not in data:
return JsonResponse({'err': _("Type ou id manquant")},
status=status.HTTP_400_BAD_REQUEST)

if data['type'] == 'player':
KwikKill marked this conversation as resolved.
Show resolved Hide resolved
try:
player = Player.objects.get(id=data['id'])
except Player.DoesNotExist:
return JsonResponse({'err': _("Inscription non trouvée")},
status=status.HTTP_404_NOT_FOUND)
if player.payment_status == PaymentStatus.PAID:
return JsonResponse({'err': _("Inscription déjà payée")},
status=status.HTTP_400_BAD_REQUEST)
player.payment_status = PaymentStatus.PAID
player.save()
ticket = Ticket.objects.create(user=player.user, tournament=player.team.tournament)
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_ticket_mail(player.user, ticket)
return JsonResponse({'success': True})
elif data['type'] == 'substitute':
try:
substitute = Substitute.objects.get(id=data['id'])
except Substitute.DoesNotExist:
return JsonResponse({'err': _("Remplaçant⋅e non trouvé⋅e")},
status=status.HTTP_404_NOT_FOUND)
if substitute.payment_status == PaymentStatus.PAID:
return JsonResponse({'err': _("Remplaçant⋅e déjà payé⋅e")},
status=status.HTTP_400_BAD_REQUEST)
substitute.payment_status = PaymentStatus.PAID
substitute.save()
ticket = Ticket.objects.create(user=substitute.user, tournament=substitute.team.tournament)
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_ticket_mail(substitute.user, ticket)
return JsonResponse({'success': True})
elif data['type'] == 'manager':
try:
manager = Manager.objects.get(id=data['id'])
except Manager.DoesNotExist:
return JsonResponse({'err': _("Manager non trouvé")},
status=status.HTTP_404_NOT_FOUND)
if manager.payment_status == PaymentStatus.PAID:
return JsonResponse({'err': _("Manager déjà payé")},
status=status.HTTP_400_BAD_REQUEST)
manager.payment_status = PaymentStatus.PAID
manager.save()
ticket = Ticket.objects.create(user=manager.user, tournament=manager.team.tournament)
MailManager.get_mailer(EMAIL_AUTH["contact"]["from"]).send_ticket_mail(manager.user, ticket)
return JsonResponse({'success': True})
else:
return JsonResponse({'err': _("Type invalide")},
status=status.HTTP_400_BAD_REQUEST)

@api_view(["GET"])
@permission_classes([IsAdminUser])
def unpaid(request: HttpRequest) -> JsonResponse:
"""
This view is used to get all the unpaid registrations
"""
# Get all the registrations that are not paid
players = Player.objects.filter(
team__validated=True,
team__tournament__event__ongoing=True
).exclude(payment_status=PaymentStatus.PAID)
substitutes = Substitute.objects.filter(
team__validated=True,
team__tournament__event__ongoing=True
).exclude(payment_status=PaymentStatus.PAID)
managers = Manager.objects.filter(
team__validated=True,
team__tournament__event__ongoing=True
).exclude(payment_status=PaymentStatus.PAID)

return JsonResponse([
{
'id': reg.id,
'type': 'player',
'user': reg.user.username,
'team': reg.team.name,
} for reg in players
] + [
{
'id': reg.id,
'type': 'substitute',
'user': reg.user.username,
'team': reg.team.name,
} for reg in substitutes
] + [
{
'id': reg.id,
'type': 'manager',
'user': reg.user.username,
'team': reg.team.name,
} for reg in managers
], safe=False)
Loading