Repository: akvo/akvo-rsr

Issue:
Show permission error in PE when copy/import fails



Discussion:

Resolving patch:
diff --git a/akvo/rsr/admin.py b/akvo/rsr/admin.py
--- a/akvo/rsr/admin.py
+++ b/akvo/rsr/admin.py
@@ -1232,6 +1232,14 @@ def get_queryset(self, request):
 class NarrativeReportAdmin(admin.ModelAdmin):
     list_display = (u'project', u'category', u'published',)
 
+    def get_queryset(self, request):
+        if request.user.is_admin or request.user.is_superuser:
+            return super(NarrativeReportAdmin, self).get_queryset(request)
+
+        employments = request.user.approved_employments(['Admins', 'M&E Managers'])
+        projects = employments.organisations().all_projects()
+        return self.model.objects.filter(project__in=projects)
+
 admin.site.register(apps.get_model('rsr', 'narrativereport'), NarrativeReportAdmin)
 
 
@@ -1241,6 +1249,14 @@ class ProjectCommentAdmin(admin.ModelAdmin):
     search_fields = ('project__id', 'project__title', 'user__first_name', 'user__last_name',)
     readonly_fields = ('created_at', 'last_modified_at')
 
+    def get_queryset(self, request):
+        if request.user.is_admin or request.user.is_superuser:
+            return super(ProjectCommentAdmin, self).get_queryset(request)
+
+        employments = request.user.approved_employments(['Admins', 'M&E Managers', 'Project Editors'])
+        projects = employments.organisations().all_projects()
+        return self.model.objects.filter(project__in=projects)
+
 admin.site.register(apps.get_model('rsr', 'projectcomment'), ProjectCommentAdmin)
 
 
@@ -1254,7 +1270,7 @@ class ProjectUpdateAdmin(TimestampsAdminDisplayMixin, AdminVideoMixin, admin.Mod
 
     list_display = ('title', 'project', 'user', 'text', 'language', 'event_date', 'created_at', 'img',)
     list_filter = ('created_at', 'project', )
-    search_fields = ('project__id', 'project__title', 'user__first_name', 'user__last_name',)
+    search_fields = ('title', 'id', 'project__id', 'project__title', 'user__first_name', 'user__last_name',)
     inlines = (ProjectUpdateLocationInline,)
     # created_at and last_modified_at MUST be readonly since they have the auto_now/_add attributes
     readonly_fields = ('created_at', 'last_modified_at')
@@ -1281,6 +1297,11 @@ def __init__(self, model, admin_site):
         self.formfield_overrides = {ImageField: {'widget': widgets.AdminFileWidget}, }
         super(ProjectUpdateAdmin, self).__init__(model, admin_site)
 
+    def get_queryset(self, request):
+        if request.user.is_admin or request.user.is_superuser:
+            return super(ProjectUpdateAdmin, self).get_queryset(request)
+        return self.model.objects.none()
+
 
 admin.site.register(apps.get_model('rsr', 'projectupdate'), ProjectUpdateAdmin)
 
@@ -1506,6 +1527,13 @@ class IatiExportAdmin(admin.ModelAdmin):
     exclude = ('projects', )
     inlines = (IatiActivityExportInline, )
 
+    def get_queryset(self, request):
+        if request.user.is_admin or request.user.is_superuser:
+            return super(IatiExportAdmin, self).get_queryset(request)
+
+        employments = request.user.approved_employments(['Admins', 'Project Editors'])
+        return self.model.objects.filter(reporting_organisation_id__in=employments.organisations())
+
 admin.site.register(apps.get_model('rsr', 'IatiExport'), IatiExportAdmin)
 
 
@@ -1527,6 +1555,11 @@ class ValidationSetAdmin(admin.ModelAdmin):
     fields = ('name', 'description')
     inlines = (ValidationInline, )
 
+    def get_queryset(self, request):
+        if request.user.is_admin or request.user.is_superuser:
+            return super(ValidationSetAdmin, self).get_queryset(request)
+        return self.model.objects.none()
+
 admin.site.register(apps.get_model('rsr', 'ProjectEditorValidationSet'), ValidationSetAdmin)
 
 
@@ -1546,6 +1579,15 @@ class IndicatorPeriodDataAdmin(admin.ModelAdmin):
     readonly_fields = ('created_at', 'last_modified_at')
     inlines = (IndicatorPeriodDataCommentInline, )
 
+    def get_queryset(self, request):
+        if request.user.is_admin or request.user.is_superuser:
+            return super(IndicatorPeriodDataAdmin, self).get_queryset(request)
+
+        employments = request.user.approved_employments(['Admins', 'M&E Managers'])
+        projects = employments.organisations().all_projects()
+        return self.model.objects.filter(period__indicator__result__project__in=projects)
+
+
 admin.site.register(apps.get_model('rsr', 'IndicatorPeriodData'), IndicatorPeriodDataAdmin)
 
 
diff --git a/akvo/rsr/eutf_signals.py b/akvo/rsr/eutf_signals.py
--- a/akvo/rsr/eutf_signals.py
+++ b/akvo/rsr/eutf_signals.py
@@ -54,5 +54,11 @@ def set_eutf_as_reporting_organisation(project):
         defaults=dict(organisation=eutf)
     )
     if not created and partnership.organisation != eutf:
+        old_reporting_organisation = partnership.organisation
         partnership.organisation = eutf
         partnership.save(update_fields=['organisation'])
+        Partnership.objects.create(
+            project=project,
+            organisation=old_reporting_organisation,
+            iati_organisation_role=Partnership.IATI_IMPLEMENTING_PARTNER,
+        )


Test patch:
diff --git a/akvo/rsr/tests/test_eutf_signals.py b/akvo/rsr/tests/test_eutf_signals.py
--- a/akvo/rsr/tests/test_eutf_signals.py
+++ b/akvo/rsr/tests/test_eutf_signals.py
@@ -14,6 +14,7 @@ class EUTFSignalsTestCase(BaseTestCase):
     def setUp(self):
         super(EUTFSignalsTestCase, self).setUp()
         self.eutf = self.create_organisation('EUTF')
+        self.delegation = self.create_organisation('EUTF Delegation')
         self.root_project = self.create_project('Root Project')
         ProjectHierarchy.objects.create(
             root_project=self.root_project, organisation=self.eutf, max_depth=2)
@@ -32,6 +33,27 @@ def test_adding_project_to_hierarchy_sets_reporting_org(self):
             project.reporting_partner.iati_organisation_role,
             Partnership.IATI_REPORTING_ORGANISATION)
 
+    def test_existing_reporting_org_made_implementing_partner(self):
+        # Given
+        project = self.create_project('New Project')
+        Partnership.objects.create(
+            project=project,
+            organisation=self.delegation,
+            iati_organisation_role=Partnership.IATI_REPORTING_ORGANISATION,
+        )
+
+        # When
+        with self.settings(EUTF_ROOT_PROJECT=self.root_project.id, EUTF_ORG_ID=self.eutf.id):
+            self.make_parent(self.root_project, project)
+
+        # Then
+        self.assertEqual(project.reporting_partner.organisation, self.eutf)
+        self.assertEqual(
+            project.reporting_partner.iati_organisation_role,
+            Partnership.IATI_REPORTING_ORGANISATION)
+        implementing_partnership = project.partnerships.get(organisation=self.delegation)
+        self.assertEqual(implementing_partnership.iati_organisation_role, Partnership.IATI_IMPLEMENTING_PARTNER)
+
     def test_adding_project_outside_hierarchy_does_not_set_reporting_org(self):
         # Given
         project = self.create_project('New Project')