Repository: kartoza/prj.app

Issue:
Certification sign up : Update email content to moderator
The email sent and cc’d to moderators should include a link to the orgs page for easy review

see: https://github.com/kartoza/prj.app/blob/8885b4e60f7c90de8e00fb2f4ed411948c7045bd/django_project/certification/views/certifying_organisation.py#L486


Discussion:

Resolving patch:
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
@@ -266,22 +266,23 @@ def get_context_data(self, **kwargs):
             context['user_can_create'] = user_can_create
             context['user_can_delete'] = user_can_delete
 
+        checklist_questions = Checklist.objects.filter(
+            project=context['the_project'],
+            target=REVIEWER,
+            active=True
+        ).prefetch_related(
+            Prefetch(
+                'organisationchecklist_set',
+                queryset=OrganisationChecklist.objects.filter(
+                    organisation=certifying_organisation
+                ))
+        )
         context['available_checklist'] = ChecklistSerializer(
-            Checklist.objects.filter(
-                project=context['the_project'],
-                target=REVIEWER,
-                active=True
-            ).prefetch_related(
-                Prefetch(
-                    'organisationchecklist_set',
-                    queryset=OrganisationChecklist.objects.filter(
-                        organisation=certifying_organisation
-                    ))
-            ), many=True).data
+            checklist_questions, many=True).data
 
         context['submitted_checklist'] = OrganisationChecklist.objects.filter(
             organisation=certifying_organisation,
-            checklist__active=True
+            checklist__in=checklist_questions
         )
         context['checked_checklist'] = context['submitted_checklist'].filter(
             checked=True
@@ -523,6 +524,7 @@ def form_valid(self, form):
                     'project_name': self.project.name,
                     'site': site,
                     'project_slug': self.project_slug,
+                    'org': self.object.slug,
                     'organisation_name': self.object.name,
                     'organisation_country': self.object.country.name,
                 }
@@ -538,8 +540,8 @@ def form_valid(self, form):
                     u'Country: {organisation_country}\n'
                     u'You may review and approve the organisation by '
                     u'following this link:\n'
-                    u'{site}/en/{project_slug}/pending-certifyingorganisation/'
-                    u'list/\n\n'
+                    u'{site}/en/{project_slug}/certifyingorganisation/{org}/'
+                    u'\n\n'
                     u'Sincerely,\n\n\n\n\n'
                     u'------------------------------------------------------\n'
                     u'This is an auto-generated email from the system.'


Test patch:
diff --git a/django_project/certification/tests/model_factories.py b/django_project/certification/tests/model_factories.py
--- a/django_project/certification/tests/model_factories.py
+++ b/django_project/certification/tests/model_factories.py
@@ -16,7 +16,7 @@
     CourseAttendee,
     Status,
     CertifyingOrganisationCertificate,
-    Checklist,
+    Checklist, OrganisationChecklist,
 )
 from core.model_factories import UserF
 from base.tests.model_factories import ProjectF
@@ -179,3 +179,13 @@ class Meta:
 
     question = factory.sequence(lambda n: u'Test question %s' % n)
     project = factory.SubFactory(ProjectF)
+
+
+class OrganisationChecklistF(factory.django.DjangoModelFactory):
+    class Meta:
+        model = OrganisationChecklist
+
+    checklist = factory.SubFactory(ChecklistF)
+    organisation = factory.SubFactory(CertifyingOrganisationF)
+    checklist_question = factory.sequence(
+        lambda n: 'Question %s' % n)
diff --git a/django_project/certification/tests/views/test_certifying_organisation_views.py b/django_project/certification/tests/views/test_certifying_organisation_views.py
--- a/django_project/certification/tests/views/test_certifying_organisation_views.py
+++ b/django_project/certification/tests/views/test_certifying_organisation_views.py
@@ -1,6 +1,7 @@
 # coding=utf-8
 import logging
 from io import StringIO
+from django.core import mail
 from django.core.management import call_command
 from django.test import TestCase, override_settings
 from django.test.client import Client
@@ -13,7 +14,7 @@
     ProjectF,
     UserF,
     CertifyingOrganisationF,
-    StatusF, ChecklistF
+    StatusF, ChecklistF, OrganisationChecklistF
 )
 
 
@@ -97,8 +98,9 @@ def test_create_view_post(self):
             target=ORGANIZATION_OWNER,
             active=True
         )
+        org_slug = 'org_name'
         post_data = {
-            'name': 'org_name',
+            'name': org_slug,
             'organisation_email': 'org@test.com',
             'address': 'city',
             'country': 'ID',
@@ -120,6 +122,9 @@ def test_create_view_post(self):
                 checked=True
             ).exists()
         )
+        self.assertIn(
+            f'/en/{self.project.slug}/certifyingorganisation/{org_slug}/',
+            mail.outbox[0].body)
 
     @override_settings(VALID_DOMAIN=['testserver', ])
     def test_detail_view_pending(self):
@@ -134,11 +139,29 @@ def test_detail_view_pending(self):
             target=REVIEWER,
             active=True
         )
+        checklist_owner = ChecklistF.create(
+            project=self.project,
+            target=ORGANIZATION_OWNER,
+            active=True
+        )
+        OrganisationChecklistF.create(
+            checklist=self.checklist,
+            organisation=pending_certifying_organisation,
+            checked=True
+        )
+        OrganisationChecklistF.create(
+            checklist=checklist_owner,
+            organisation=pending_certifying_organisation,
+            checked=True
+        )
         response = client.get(reverse('certifyingorganisation-detail', kwargs={
             'project_slug': self.project.slug,
             'slug': pending_certifying_organisation.slug
         }))
-        self.assertTrue(len(response.context_data['available_checklist']) > 0)
+        self.assertEqual(
+            len(response.context_data['available_checklist']), 1)
+        self.assertEqual(
+            len(response.context_data['submitted_checklist']), 1)
         self.assertEqual(response.status_code, 200)
 
     @override_settings(VALID_DOMAIN=['testserver', ])