Repository: kartoza/prj.app

Issue:
Project admins should be emailed a notification when an organization needs approval
When a user creates a new organization, there should be an email sent to the project admin(s) indicating it needs approval.

It would be also nice to show a message when an admin user logs in to the site to say there are some orgs etc. pending approval. Look at using Django messaging for this.


Discussion:

Resolving patch:
diff --git a/django_project/base/views/project.py b/django_project/base/views/project.py
--- a/django_project/base/views/project.py
+++ b/django_project/base/views/project.py
@@ -21,6 +21,7 @@
 from ..models import Project
 from ..forms import ProjectForm
 from vota.models import Committee, Ballot
+from certification.models import CertifyingOrganisation
 from django.conf import settings
 
 logger = logging.getLogger(__name__)
@@ -83,6 +84,23 @@ def get_context_data(self, **kwargs):
         context['num_projects'] = self.get_queryset().count()
         context[
             'PROJECT_VERSION_LIST_SIZE'] = settings.PROJECT_VERSION_LIST_SIZE
+        if self.request.user.is_authenticated():
+            project = Project.objects.filter(owner=self.request.user)
+            pending_organisation = CertifyingOrganisation.objects.filter(
+                project=project, approved=False
+            )
+            context['num_project_with_pending'] = 0
+            if pending_organisation:
+                context['project_with_pending'] = []
+
+                for organisation in pending_organisation:
+                    if organisation.project not in \
+                            context['project_with_pending']:
+                        context['project_with_pending'].append(
+                            organisation.project)
+                        context['num_project_with_pending'] += 1
+                context['message'] = \
+                    'You have a pending organisation approval. '
         return context
 
     def get_queryset(self):
diff --git a/django_project/certification/views/certifying_organisation.py b/django_project/certification/views/certifying_organisation.py
--- a/django_project/certification/views/certifying_organisation.py
+++ b/django_project/certification/views/certifying_organisation.py
@@ -1,5 +1,7 @@
 # coding=utf-8
 from base.models import Project
+from django.contrib import messages
+from django.core.mail import send_mail
 from django.core.urlresolvers import reverse
 from django.shortcuts import get_object_or_404
 from django.http import HttpResponse
@@ -341,8 +343,31 @@ def get_queryset(self):
         return qs
 
 
+class CustomSuccessMessageMixin(object):
+    """
+    Adds a success message and extra tags on successful form submission.
+    """
+    success_message = ''
+    message_extra_tags = ''
+
+    def form_valid(self, form):
+        response = super(CustomSuccessMessageMixin, self).form_valid(form)
+        success_message = self.get_success_message(form.cleaned_data)
+        message_extra_tags = self.get_extra_tags(form.cleaned_data)
+        if success_message:
+            messages.success(self.request, success_message, message_extra_tags)
+        return response
+
+    def get_success_message(self, cleaned_data):
+        return self.success_message % cleaned_data
+
+    def get_extra_tags(self, cleaned_data):
+        return self.message_extra_tags % cleaned_data
+
+
 # noinspection PyAttributeOutsideInit
 class CertifyingOrganisationCreateView(
+        CustomSuccessMessageMixin,
         LoginRequiredMixin,
         CertifyingOrganisationMixin,
         CreateView):
@@ -350,6 +375,9 @@ class CertifyingOrganisationCreateView(
 
     context_object_name = 'certifyingorganisation'
     template_name = 'certifying_organisation/create.html'
+    success_message = 'Your organisation is successfully registered. '\
+                      'It is now waiting for an approval.'
+    message_extra_tags = 'organisation_submitted'
 
     def get_success_url(self):
         """Define the redirect URL.
@@ -395,6 +423,27 @@ def form_valid(self, form):
 
         try:
             super(CertifyingOrganisationCreateView, self).form_valid(form)
+            site = self.request.get_host()
+
+            send_mail(
+                'Projecta - New Pending Organisation Approval',
+                'Dear %s %s,\n\n' % (
+                    self.project.owner.first_name,
+                    self.project.owner.last_name) +
+                'You have a new organisation registered to your project: %s.\n'
+                % self.project.name +
+                'You may review and approve the organisation by following this'
+                ' link:\n'
+                '%s/en/%s/pending-certifyingorganisation/list/\n\n'
+                % (site, self.project_slug) +
+                'Sincerely,\n\n\n\n\n'
+                '----------------------------------------------------------\n'
+                'This is an auto-generated email from the system.'
+                ' Please do not reply to this email.',
+                self.project.owner.email,
+                [self.project.owner.email],
+                fail_silently=False,
+            )
             return HttpResponseRedirect(self.get_success_url())
         except IntegrityError:
             return ValidationError(
diff --git a/django_project/core/settings/base.py b/django_project/core/settings/base.py
--- a/django_project/core/settings/base.py
+++ b/django_project/core/settings/base.py
@@ -106,6 +106,7 @@
 
                 # `allauth` needs this from django
                 'django.template.context_processors.request',
+                'django.contrib.messages.context_processors.messages',
             ],
         },
     },


Test patch: