Luke Plant avatar Luke Plant committed ed98f14

Switched to use new messages framework

Comments (0)

Files changed (7)

cciw/officers/admin.py

 from cciw.middleware import threadlocals
 from cciw.officers.fields import ExplicitBooleanField
 from cciw.officers.models import Application, Reference, Invitation, ReferenceForm
-from cciw.officers import widgets
+from cciw.officers import widgets, email
 from cciw.utils.views import close_window_response
 
 class ApplicationAdminModelForm(forms.ModelForm):
         resp = super(ApplicationAdmin, self).response_change(request, new_object)
         return self._redirect(request, resp)
 
+    def save_model(self, request, obj, form, change):
+        super(ApplicationAdmin, self).save_model(request, obj, form, change)
+        email.send_application_emails(request, obj)
+
 class ReferenceAdmin(admin.ModelAdmin):
     search_fields = ['application__officer__first_name', 'application__officer__last_name']
 

cciw/officers/email.py

 from cciw.officers.email_utils import send_mail_with_attachments, formatted_email
 from cciw.officers.references import reference_form_to_text
 from django.conf import settings
+from django.contrib import messages
 from django.core.mail import send_mail
 from django.core.urlresolvers import reverse
 from django.utils.hashcompat import sha_constructor
     return filter(lambda x: x is not None,
                   map(formatted_email, leaders))
 
-def send_application_emails(application):
+def send_application_emails(request, application):
     if not application.finished:
         return
 
 
     # If an admin user corrected an application, we don't send the user a copy
     # (usually they just get the year of the camp wrong(!))
-    user = threadlocals.get_current_user()
+    user = request.user
     if len(leader_emails) > 0:
-        user.message_set.create(message="The completed application form has been sent to the leaders via email.")
+        messages.info(request, "The completed application form has been sent to the leaders via email.")
 
     if user == application.officer:
         send_officer_email(application.officer, application, application_text, rtf_attachment)
-        user.message_set.create(message="A copy of the application form has been sent to you via email.")
+        messages.info(request, "A copy of the application form has been sent to you via email.")
 
         if application.officer.email != application.address_email:
             send_email_change_emails(user, application)

cciw/officers/hooks.py

-# Hooks for various events
-from cciw.officers import signals
-from cciw.officers.email import send_application_emails
-
-send_application_emails_w = lambda sender, **kwargs: send_application_emails(sender)
-signals.application_saved.connect(send_application_emails_w)

cciw/officers/models.py

 from django.http import HttpResponseForbidden
 
 from cciw.cciwmain.models import Camp
-from cciw.officers import signals
 from django.contrib.auth.models import User
 import datetime
 from cciw.officers.fields import YyyyMmField, AddressField, ExplicitBooleanField, required_field
             self._references_cache = retval
             return retval
 
-    def save(self):
-        super(Application, self).save()
-        signals.application_saved.send(sender=self)
-
     def __unicode__(self):
         if self.camp is not None:
             return u"Application from %s, %d, camp %d" % (self.full_name, self.camp.year, self.camp.number)
         ordering = ('-camp__year', 'officer__first_name', 'officer__last_name')
         unique_together = (('officer', 'camp'),)
 
-# Ensure hooks get set up
-import cciw.officers.hooks
 import cciw.officers.admin

cciw/officers/signals.py

-from django.dispatch import Signal
-
-application_saved = Signal() # Sent after an Application object is saved

cciw/officers/views.py

 from django.contrib.admin.views.decorators import staff_member_required
 from django.contrib.auth.decorators import user_passes_test
 from django.contrib.auth.models import User
+from django.contrib import messages
 from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
 from django.core.urlresolvers import reverse
 from django.db import models
                                    msg, settings.SERVER_EMAIL,
                                    [formatted_email(request.user)],
                                    attachments=[rtf_attachment])
-        request.user.message_set.create(message="Email sent.")
+        messages.info(request, "Email sent.")
 
         # Redirect back where we came from
         return HttpResponseRedirect(request.POST.get('to', '/officers/'))
     'cciw.officers',
     'cciw.tagging',
     'cciw.utils',
+    'django.contrib.messages',
 )
 
 if DEBUG:
     "django.core.context_processors.i18n",
     "cciw.cciwmain.common.standard_processor",
     "django.core.context_processors.request",
+    "django.contrib.messages.context_processors.messages",
 )
 
 #####  EMAIL  #######
 #    (DEVBOX,     "lukeplant_me_uk.django.middleware.validator.ValidatorMiddleware"),
     (True,       'django.middleware.csrf.CsrfViewMiddleware'),
     (True,       "django.contrib.sessions.middleware.SessionMiddleware"),
+    (True,       "django.contrib.messages.middleware.MessageMiddleware"),
     (True,       "django.contrib.auth.middleware.AuthenticationMiddleware"),
     (True,       "django.middleware.common.CommonMiddleware"),
     (True,       "cciw.middleware.threadlocals.ThreadLocals"),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.