Repository: dotkom/onlineweb4

Issue:
Events-overview "Attending" missing actual attending
Browsing [ow4/events](https://online.ntnu.no/events/) lists all (future) events (by default).
Under the short introduction to an event, there's a list of information. (see inline quote) 
Number of attending in this list does not actually tell you how many are attending, and therefore not if there are available spots.

> Påmeldte: / 220
> Sted: Frimurerlogen
> Påmelding åpner 28.08.14 12.00



Discussion:

Resolving patch:
diff --git a/apps/events/admin.py b/apps/events/admin.py
--- a/apps/events/admin.py
+++ b/apps/events/admin.py
@@ -172,11 +172,11 @@ def _max_capacity(self, obj):
     _max_capacity.short_description = _("Arrangementets maks-kapasitet")
 
     def save_model(self, request, obj, form, change):
-        if change:
-            number_of_free_seats = obj.attendance_event.max_capacity - obj.attendance_event.number_of_attendees
-            if number_of_free_seats < obj.seats:
-                obj.seats = number_of_free_seats
-                self.message_user(request, _("Du har valgt et antall reserverte plasser som overskrider antallet ledige plasser for dette arrangementet. Antallet ble automatisk justert til %d (alle ledige plasser).") % number_of_free_seats, messages.WARNING)
+        attendance_event = AttendanceEvent.objects.get(pk=obj.attendance_event.event)
+        number_of_free_seats = attendance_event.max_capacity - attendance_event.number_of_attendees
+        if number_of_free_seats < obj.seats:
+            obj.seats = number_of_free_seats
+            self.message_user(request, _(u"Du har valgt et antall reserverte plasser som overskrider antallet ledige plasser for dette arrangementet. Antallet ble automatisk justert til %d (alle ledige plasser).") % number_of_free_seats, messages.WARNING)
         obj.save()
             
     
diff --git a/apps/events/models.py b/apps/events/models.py
--- a/apps/events/models.py
+++ b/apps/events/models.py
@@ -424,12 +424,14 @@ def reservees_qs(self):
     @property
     def number_of_attendees(self):
         """ Count of all attendees not in waiting list """
-        return self.attendees_qs.count()
+        # We need to use len() instead of .count() here, because of the prefetched event archive
+        return len(self.attendees_qs)
 
     @property
     def number_on_waitlist(self):
         """ Count of all attendees on waiting list """
-        return self.waitlist_qs.count()
+        # We need to use len() instead of .count() here, because of the prefetched event archive
+        return len(self.waitlist_qs)
 
     @property
     def number_of_reserved_seats(self):
diff --git a/apps/events/views.py b/apps/events/views.py
--- a/apps/events/views.py
+++ b/apps/events/views.py
@@ -178,8 +178,9 @@ def _search_indexed(request, query, filters):
         kwargs['attendance_event__attendees__user'] = request.user
 
     events = Event.objects.filter(**kwargs).order_by('event_start').prefetch_related(
-            'attendance_event', 'attendance_event__attendees')
-
+            'attendance_event', 'attendance_event__attendees', 'attendance_event__reserved_seats',
+            'attendance_event__reserved_seats__reservees')
+    
     if query:
         for result in watson.search(query, models=(events,)):
             results.append(result.object)


Test patch: