|
@@ -59,10 +59,9 @@ def create_event(request):
|
|
|
@cache_page(60 * 15)
|
|
|
@vary_on_cookie
|
|
|
def event(request, url):
|
|
|
- try:
|
|
|
- participants = list(Participant.objects.prefetch_related('user', 'event', 'event__date_set').filter(event__url=url))
|
|
|
- event = participants[0].event
|
|
|
- except Event.DoesNotExist:
|
|
|
+ event = Event.objects.filter(url=url).prefetch_related('date_set', 'participant_set', 'participant_set__user').first()
|
|
|
+ participants = list(event.participant_set.all()) if event else []
|
|
|
+ if not event:
|
|
|
return render(request, 'zitap/event-not-found.html')
|
|
|
|
|
|
# Check if the user is logged in
|
|
@@ -71,9 +70,10 @@ def event(request, url):
|
|
|
if participant.user_id == request.session['user_id']:
|
|
|
break
|
|
|
else:
|
|
|
- participant, created = Participant.objects.get_or_create(user_id=request.session['user_id'], event=event)
|
|
|
+ participant = None
|
|
|
login_form = None
|
|
|
- update_form = UpdateSlotsForm(initial={'slots': slots2string(participant, get_slot_count(event))}, participant=participant)
|
|
|
+ n_slots = get_slot_count(event)
|
|
|
+ update_form = UpdateSlotsForm(initial={'slots': slots2string(participant, n_slots)}, event=event, n_slots=n_slots, participant=participant)
|
|
|
else:
|
|
|
login_form = LoginForm()
|
|
|
update_form = None
|
|
@@ -131,12 +131,18 @@ def update_slots(request, url):
|
|
|
if request.method != 'POST':
|
|
|
return HttpResponseNotAllowed(['POST'])
|
|
|
|
|
|
- participant = Participant.objects.get(user_id=request.session['user_id'], event=event)
|
|
|
- form = UpdateSlotsForm(request.POST, participant=participant)
|
|
|
+ slot_count = get_slot_count(event)
|
|
|
+ participant = event.participant_set.filter(user_id=request.session['user_id'], event=event).first()
|
|
|
+ form = UpdateSlotsForm(request.POST, event=event, n_slots=slot_count, participant=participant)
|
|
|
if form.is_valid():
|
|
|
data = form.cleaned_data
|
|
|
- participant.slots = string2slots(data['slots'], get_slot_count(event))
|
|
|
- participant.save()
|
|
|
+ if data['slots'] == '0' * slot_count and participant:
|
|
|
+ participant.delete()
|
|
|
+ elif not participant:
|
|
|
+ participant = Participant.objects.create(user_id=request.session['user_id'], event=event, slots=string2slots(data['slots'], slot_count))
|
|
|
+ else:
|
|
|
+ participant.slots = string2slots(data['slots'], slot_count)
|
|
|
+ participant.save()
|
|
|
return HttpResponseRedirect(f'/{event.url}')
|
|
|
|
|
|
def event_api(request, url):
|
|
@@ -166,28 +172,37 @@ def slots_api(request, url):
|
|
|
Slots are represented as a string of 0s and 1s where 0 means the slot is available and 1 means the slot is taken.
|
|
|
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.
|
|
|
"""
|
|
|
- try:
|
|
|
- participants = Participant.objects.select_related('event', 'user').filter(event__url=url)
|
|
|
- event = participants.first().event
|
|
|
- slot_count = get_slot_count(event)
|
|
|
+ event = Event.objects.filter(url=url).prefetch_related('date_set', 'participant_set', 'participant_set__user').first()
|
|
|
+ participants = list(event.participant_set.all()) if event else []
|
|
|
+ if not event:
|
|
|
+ return HttpResponseNotFound()
|
|
|
|
|
|
- # Check if the user is logged in and wants to update their slots
|
|
|
- if 'user_id' in request.session and request.method == 'POST':
|
|
|
- participant = participants.filter(user_id=request.session['user_id']).first()
|
|
|
- if participant is None:
|
|
|
- participant = Participant.objects.get_or_create(user_id=request.session['user_id'], event=event)
|
|
|
- form = UpdateSlotsForm(request.POST, participant=participant)
|
|
|
- if form.is_valid():
|
|
|
- data = form.cleaned_data
|
|
|
- participant.slots = string2slots(data['slots'], slot_count)
|
|
|
- participant.save()
|
|
|
+ slot_count = get_slot_count(event)
|
|
|
|
|
|
- # Get the slots of each participant
|
|
|
- data = {}
|
|
|
+ # Check if the user is logged in and wants to update their slots
|
|
|
+ if 'user_id' in request.session and request.method == 'POST':
|
|
|
for participant in participants:
|
|
|
- data[participant.user.username] = slots2string(participant, slot_count)
|
|
|
-
|
|
|
- return JsonResponse(data)
|
|
|
+ if participant.user_id == request.session['user_id']:
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ participant = None
|
|
|
+
|
|
|
+ slot_count = get_slot_count(event)
|
|
|
+ form = UpdateSlotsForm(request.POST, event=event, n_slots=slot_count, participant=participant)
|
|
|
+ if form.is_valid():
|
|
|
+ data = form.cleaned_data
|
|
|
+ if data['slots'] == '0' * slot_count and participant:
|
|
|
+ participant.delete()
|
|
|
+ elif not participant:
|
|
|
+ participant = Participant.objects.create(user_id=request.session['user_id'], event=event, slots=string2slots(data['slots'], slot_count))
|
|
|
+ else:
|
|
|
+ participant.slots = string2slots(data['slots'], slot_count)
|
|
|
+ participant.save()
|
|
|
|
|
|
- except Event.DoesNotExist:
|
|
|
- return HttpResponseNotFound()
|
|
|
+ # Get the slots of each participant
|
|
|
+ data = {}
|
|
|
+ for participant in participants:
|
|
|
+ data[participant.user.username] = slots2string(participant, slot_count)
|
|
|
+
|
|
|
+ return JsonResponse(data)
|
|
|
+
|