Repository: dotkom/onlineweb4

Issue:
Users on the waiting list are not informed when they are moved to the attendance list
They should be notified by e-mail or else they probably won't know that they can attend. 



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
@@ -48,6 +48,12 @@ class AttendanceEventAdmin(admin.ModelAdmin):
 class AttendeeAdmin(admin.ModelAdmin):
     model = Attendee
     list_display = ('user', 'event', 'paid')
+    actions = None
+
+    def delete_model(self, request, obj):
+        event = obj.event.event
+        event.notify_waiting_list(host=request.META['HTTP_HOST'], unattended_user=obj.user)
+        obj.delete()
 
 
 class CompanyEventAdmin(admin.ModelAdmin):
@@ -88,6 +94,16 @@ class EventAdmin(admin.ModelAdmin):
     def save_model(self, request, obj, form, change):
         if not change:  # created
             obj.author = request.user
+        else:
+            # If attendance max capacity changed we will notify users that they are now on the attend list
+            old_event = Event.objects.get(id=obj.id)
+            if obj.is_attendance_event() and old_event.wait_list:
+                diff_capacity = obj.attendance_event.max_capacity - old_event.attendance_event.max_capacity
+                if diff_capacity > 0:
+                    if diff_capacity > len(old_event.wait_list):
+                        diff_capacity = len(old_event.wait_list)
+                    # Using old_event because max_capacity has already been changed in obj
+                    old_event.notify_waiting_list(host=request.META['HTTP_HOST'], extra_capacity=diff_capacity)
         obj.save()
 
     def save_formset(self, request, form, formset, change):
diff --git a/apps/events/models.py b/apps/events/models.py
--- a/apps/events/models.py
+++ b/apps/events/models.py
@@ -4,12 +4,12 @@
 
 from django.conf import settings
 from django.contrib.auth.models import Group
+from django.core.mail import send_mail
 from django.core.urlresolvers import reverse
 from django.db import models
 from django.template.defaultfilters import slugify
 from django.utils import timezone
 from django.utils.translation import ugettext as _
-
 from filebrowser.fields import FileBrowseField
 import watson
 
@@ -184,6 +184,33 @@ def what_place_is_user_on_wait_list(self, user):
                             return list(waitlist).index(attendee_object) + 1
         return 0
 
+    def notify_waiting_list(self, host, unattended_user=None, extra_capacity=1):
+        if not self.is_attendance_event():
+            return
+        # Notify next user on waiting list
+        wait_list = self.wait_list
+        if wait_list:
+            # Checking if user is on the wait list
+            on_wait_list = False
+            if unattended_user:
+                for waiting_user in wait_list:
+                    if waiting_user.user == unattended_user:
+                        on_wait_list = True
+                        break
+            if not on_wait_list:
+                # Send mail to first user on waiting list
+                attendees = wait_list[:extra_capacity]
+                email_message = _(u"""
+Du har stått på venteliste for arrangementet "%s" og har nå fått plass.
+Det kreves ingen ekstra handling fra deg med mindre du vil melde deg av.
+
+For mer info:
+http://%s%s
+""") % (self.title, host, self.get_absolute_url())
+                for attendee in attendees:
+                    send_mail(_(u'Du har fått plass på et arrangement'), email_message,
+                              settings.DEFAULT_FROM_EMAIL, [attendee.user.get_email().email])
+
     def feedback_mail(self):
         if self.event_type == 1 or self.event_type == 4: #sosialt/utflukt
             return settings.EMAIL_ARRKOM
diff --git a/apps/events/views.py b/apps/events/views.py
--- a/apps/events/views.py
+++ b/apps/events/views.py
@@ -127,7 +127,8 @@ def unattendEvent(request, event_id):
     if attendance_event.unattend_deadline < timezone.now():
         messages.error(request, _(u"Avmeldingsfristen for dette arrangementet har utløpt."))
         return redirect(event)
-    
+
+    event.notify_waiting_list(host=request.META['HTTP_HOST'], unattended_user=request.user)
     Attendee.objects.get(event=attendance_event, user=request.user).delete()
 
     messages.success(request, _(u"Du ble meldt av arrangementet."))


Test patch: