subDesTagesMitExtraKaese 2 лет назад
Родитель
Сommit
43a0b205f7
1 измененных файлов с 12 добавлено и 7 удалено
  1. 12 7
      zitap/views.py

+ 12 - 7
zitap/views.py

@@ -60,14 +60,17 @@ def create_event(request):
 @vary_on_cookie
 @vary_on_cookie
 def event(request, url):
 def event(request, url):
     try:
     try:
-        event = Event.objects.prefetch_related('date_set', 'participant_set', 'participant_set__user').get(url=url)
+        participants = list(Participant.objects.prefetch_related('user', 'event', 'event__date_set').filter(event__url=url))
+        event = participants[0].event
     except Event.DoesNotExist:
     except Event.DoesNotExist:
         return render(request, 'zitap/event-not-found.html')
         return render(request, 'zitap/event-not-found.html')
     
     
     # Check if the user is logged in
     # Check if the user is logged in
     if 'user_id' in request.session:
     if 'user_id' in request.session:
-        participant = event.participant_set.filter(user_id=request.session['user_id']).first()
-        if participant is None:
+        for participant in participants:
+            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, created = Participant.objects.get_or_create(user_id=request.session['user_id'], event=event)
         login_form = None
         login_form = None
         update_form = UpdateSlotsForm(initial={'slots': slots2string(participant, get_slot_count(event))}, participant=participant)
         update_form = UpdateSlotsForm(initial={'slots': slots2string(participant, get_slot_count(event))}, participant=participant)
@@ -77,7 +80,7 @@ def event(request, url):
     return render(
     return render(
         request, 
         request, 
         'zitap/event.html', 
         'zitap/event.html', 
-        {'event': event, 'grid': slots2grid(event, list(event.participant_set.all()), False), 'login_form': login_form, 'update_form': update_form}
+        {'event': event, 'grid': slots2grid(event, participants, False), 'login_form': login_form, 'update_form': update_form}
     )
     )
 
 
 def login(request, url):
 def login(request, url):
@@ -164,12 +167,15 @@ def slots_api(request, url):
     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.
     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:
     try:
-        event = Event.objects.get(url=url)
+        participants = Participant.objects.select_related('event', 'user').filter(event__url=url)
+        event = participants.first().event
         slot_count = get_slot_count(event)
         slot_count = get_slot_count(event)
 
 
         # Check if the user is logged in and wants to update their slots
         # Check if the user is logged in and wants to update their slots
         if 'user_id' in request.session and request.method == 'POST':
         if 'user_id' in request.session and request.method == 'POST':
-            participant = Participant.objects.get(user_id=request.session['user_id'], event=event)
+            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)
             form = UpdateSlotsForm(request.POST, participant=participant)
             if form.is_valid():
             if form.is_valid():
                 data = form.cleaned_data
                 data = form.cleaned_data
@@ -178,7 +184,6 @@ def slots_api(request, url):
 
 
         # Get the slots of each participant
         # Get the slots of each participant
         data = {}
         data = {}
-        participants = event.participant_set.all()
         for participant in participants:
         for participant in participants:
             data[participant.user.username] = slots2string(participant, slot_count)
             data[participant.user.username] = slots2string(participant, slot_count)