views.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import datetime
  2. from django.db import IntegrityError
  3. from django.http import HttpResponseNotFound, HttpResponseRedirect, JsonResponse, HttpResponseNotAllowed
  4. from django.shortcuts import render
  5. from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
  6. from django.contrib.auth.models import User
  7. from .forms import CreateEventForm, LoginForm, UpdateSlotsForm
  8. from .models import Event, Participant, Date
  9. from .helpers import slots2string, string2slots, get_slot_count, slots2grid
  10. def index(request):
  11. return render(request, 'zitap/index.html')
  12. def about(request):
  13. return render(request, 'zitap/about.html')
  14. def create_event(request):
  15. if request.method == 'POST':
  16. form = CreateEventForm(request.POST)
  17. if form.is_valid():
  18. data = form.cleaned_data
  19. print(data)
  20. event = Event.objects.create(name=data['event_name'])
  21. for date in data['event_date']:
  22. event.date_set.create(date=date)
  23. event.save()
  24. return HttpResponseRedirect(f'/{event.url}')
  25. else:
  26. form = CreateEventForm()
  27. return render(request, 'zitap/create-event.html', {'form': form})
  28. def event(request, url):
  29. try:
  30. event = Event.objects.get(url=url)
  31. except Event.DoesNotExist:
  32. return render(request, 'zitap/event-not-found.html')
  33. # Check if the user is logged in
  34. if 'participant_id' in request.session:
  35. participant = Participant.objects.get(id=request.session['participant_id'])
  36. login_form = None
  37. update_form = UpdateSlotsForm(initial={'slots': participant.slots})
  38. else:
  39. login_form = LoginForm()
  40. update_form = None
  41. return render(
  42. request,
  43. 'zitap/event.html',
  44. {'event': event, 'grid': slots2grid(event), 'login_form': login_form, 'update_form': update_form}
  45. )
  46. def login(request, url):
  47. try:
  48. event = Event.objects.get(url=url)
  49. except Event.DoesNotExist:
  50. return render(request, 'zitap/event-not-found.html')
  51. if request.method != 'POST':
  52. return HttpResponseNotAllowed(['POST'])
  53. form = LoginForm(request.POST)
  54. if form.is_valid():
  55. data = form.cleaned_data
  56. user = authenticate(request, username=data['username'], password=data.get('password'))
  57. if user is None:
  58. try:
  59. user = User.objects.create_user(data['username'], password=data.get('password'))
  60. except IntegrityError:
  61. form.add_error('username', 'Username already exists')
  62. return render(
  63. request,
  64. 'zitap/event.html',
  65. {'event': event, 'grid': slots2grid(event), 'login_form': form, 'update_form': None}
  66. )
  67. auth_login(request, user)
  68. participant, created = Participant.objects.get_or_create(event=event, user=user)
  69. request.session['participant_id'] = participant.id
  70. return HttpResponseRedirect(f'/{event.url}')
  71. def logout(request, url):
  72. try:
  73. event = Event.objects.get(url=url)
  74. except Event.DoesNotExist:
  75. return render(request, 'zitap/event-not-found.html')
  76. if request.method != 'POST':
  77. return HttpResponseNotAllowed(['POST'])
  78. if 'participant_id' in request.session:
  79. del request.session['participant_id']
  80. return HttpResponseRedirect(f'/{event.url}')
  81. def update_slots(request, url):
  82. try:
  83. event = Event.objects.get(url=url)
  84. except Event.DoesNotExist:
  85. return render(request, 'zitap/event-not-found.html')
  86. if request.method != 'POST':
  87. return HttpResponseNotAllowed(['POST'])
  88. form = UpdateSlotsForm(request.POST)
  89. if form.is_valid():
  90. data = form.cleaned_data
  91. participant = Participant.objects.get(id=request.session['participant_id'])
  92. participant.slots = string2slots(data['slots'])
  93. participant.save()
  94. return HttpResponseRedirect(f'/{event.url}')
  95. def slots_api(request, url):
  96. """
  97. REST JSON API for slots of all participants of an event
  98. Slots are represented as a string of 0s and 1s where 0 means the slot is available and 1 means the slot is taken.
  99. The string begins with the first slot of the first day of the event and ends with the last slot of the last day of the event.
  100. """
  101. try:
  102. event = Event.objects.get(url=url)
  103. slot_count = get_slot_count(event)
  104. # Check if the user is logged in and wants to update their slots
  105. if 'participant_id' in request.session and request.method == 'POST':
  106. participant = Participant.objects.get(id=request.session['participant_id'])
  107. form = UpdateSlotsForm(request.POST)
  108. if form.is_valid():
  109. data = form.cleaned_data
  110. participant.slots = string2slots(data['slots'])
  111. participant.save()
  112. # Get the slots of each participant
  113. data = {}
  114. participants = event.participant_set.all()
  115. for participant in participants:
  116. data[participant.name] = slots2string(participant, slot_count)
  117. return JsonResponse(data)
  118. except Event.DoesNotExist:
  119. return HttpResponseNotFound()