views.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import random
  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. # Try to create the event. If the url is already taken, try again.
  20. n = 0
  21. while n<10:
  22. try:
  23. event = Event.objects.create(name=data['event_name'])
  24. for date in data['event_date']:
  25. event.date_set.create(date=date)
  26. event.save()
  27. return HttpResponseRedirect(f'/{event.url}')
  28. except IntegrityError:
  29. n += 1
  30. form.add_error(None, 'Could not create event. Please try again.')
  31. else:
  32. form = CreateEventForm()
  33. return render(request, 'zitap/create-event.html', {'form': form})
  34. def event(request, url):
  35. try:
  36. event = Event.objects.get(url=url)
  37. except Event.DoesNotExist:
  38. return render(request, 'zitap/event-not-found.html')
  39. # Check if the user is logged in
  40. if 'participant_id' in request.session:
  41. participant = Participant.objects.get(id=request.session['participant_id'])
  42. login_form = None
  43. update_form = UpdateSlotsForm(initial={'slots': participant.slots})
  44. else:
  45. login_form = LoginForm()
  46. update_form = None
  47. return render(
  48. request,
  49. 'zitap/event.html',
  50. {'event': event, 'grid': slots2grid(event), 'login_form': login_form, 'update_form': update_form}
  51. )
  52. def login(request, url):
  53. try:
  54. event = Event.objects.get(url=url)
  55. except Event.DoesNotExist:
  56. return render(request, 'zitap/event-not-found.html')
  57. if request.method != 'POST':
  58. return HttpResponseNotAllowed(['POST'])
  59. form = LoginForm(request.POST)
  60. if form.is_valid():
  61. data = form.cleaned_data
  62. user = authenticate(request, username=data['username'], password=data.get('password'))
  63. if user is None:
  64. try:
  65. user = User.objects.create_user(data['username'], password=data.get('password'))
  66. except IntegrityError:
  67. form.add_error('username', 'Username already exists')
  68. return render(
  69. request,
  70. 'zitap/event.html',
  71. {'event': event, 'grid': slots2grid(event), 'login_form': form, 'update_form': None}
  72. )
  73. auth_login(request, user)
  74. participant, created = Participant.objects.get_or_create(event=event, user=user)
  75. participant.slots = random.getrandbits(get_slot_count(event)).to_bytes(get_slot_count(event) // 8, 'big')
  76. participant.save()
  77. request.session['participant_id'] = participant.id
  78. return HttpResponseRedirect(f'/{event.url}')
  79. def logout(request, url):
  80. try:
  81. event = Event.objects.get(url=url)
  82. except Event.DoesNotExist:
  83. return render(request, 'zitap/event-not-found.html')
  84. if 'participant_id' in request.session:
  85. del request.session['participant_id']
  86. return HttpResponseRedirect(f'/{event.url}')
  87. def update_slots(request, url):
  88. try:
  89. event = Event.objects.get(url=url)
  90. except Event.DoesNotExist:
  91. return render(request, 'zitap/event-not-found.html')
  92. if request.method != 'POST':
  93. return HttpResponseNotAllowed(['POST'])
  94. form = UpdateSlotsForm(request.POST)
  95. if form.is_valid():
  96. data = form.cleaned_data
  97. participant = Participant.objects.get(id=request.session['participant_id'])
  98. participant.slots = string2slots(data['slots'])
  99. participant.save()
  100. return HttpResponseRedirect(f'/{event.url}')
  101. def slots_api(request, url):
  102. """
  103. REST JSON API for slots of all participants of an event
  104. Slots are represented as a string of 0s and 1s where 0 means the slot is available and 1 means the slot is taken.
  105. 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.
  106. """
  107. try:
  108. event = Event.objects.get(url=url)
  109. slot_count = get_slot_count(event)
  110. # Check if the user is logged in and wants to update their slots
  111. if 'participant_id' in request.session and request.method == 'POST':
  112. participant = Participant.objects.get(id=request.session['participant_id'])
  113. form = UpdateSlotsForm(request.POST)
  114. if form.is_valid():
  115. data = form.cleaned_data
  116. participant.slots = string2slots(data['slots'])
  117. participant.save()
  118. # Get the slots of each participant
  119. data = {}
  120. participants = event.participant_set.all()
  121. for participant in participants:
  122. data[participant.name] = slots2string(participant, slot_count)
  123. return JsonResponse(data)
  124. except Event.DoesNotExist:
  125. return HttpResponseNotFound()