Skip to content

Commit

Permalink
Merge pull request #3611 from unicef/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
robertavram authored Dec 15, 2023
2 parents e9132e9 + 80189ae commit 4d3dec4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/etools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = __version__ = '11.1.1'
VERSION = __version__ = '11.1.6'
NAME = 'eTools'
1 change: 0 additions & 1 deletion src/etools/applications/organizations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def relationship_types(self):
not self.partner.hidden:
_list.append('partner')
if hasattr(self, 'auditorfirm') and \
self.auditorfirm.purchase_orders.filter(engagement__isnull=False).exists() and \
not self.auditorfirm.hidden:
_list.append('audit')
if hasattr(self, 'tpmpartner') and \
Expand Down
1 change: 1 addition & 0 deletions src/etools/applications/partners/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ class AgreementAdmin(
'partner_manager',
'signed_by',
'terms_acknowledged_by',
'authorized_officers',
)
fieldsets = (
(_('Agreement Details'), {
Expand Down
1 change: 1 addition & 0 deletions src/etools/applications/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ class RealmAdmin(RestrictedEditAdminMixin, SnapshotModelAdmin):
search_fields = ('user__email', 'user__first_name', 'user__last_name', 'country__name',
'organization__name', 'organization__vendor_number', 'group__name')
autocomplete_fields = ('country', 'group')
list_filter = ('country', 'group')

inlines = (ActivityInline, )

Expand Down
15 changes: 15 additions & 0 deletions src/etools/applications/users/filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.db import models

from django_filters import rest_framework as filters
from rest_framework.filters import BaseFilterBackend

from etools.applications.organizations.models import Organization


class UserRoleFilter(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
Expand All @@ -26,3 +29,15 @@ def filter_queryset(self, request, queryset, view):
filters |= models.Q(is_active=True, has_active_realm=False)
return queryset.filter(filters)
return queryset


class OrganizationFilter(filters.FilterSet):
organization_id = filters.NumberFilter()
organization_type = filters.ChoiceFilter(choices=(('audit', 'audit'), ('partner', 'partner'), ('tpm', 'tpm')))

class Meta:
model = Organization
fields = ['organization_id', 'organization_type']

def filter_queryset(self, queryset):
return queryset
23 changes: 22 additions & 1 deletion src/etools/applications/users/tests/test_views_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

from etools.applications.action_points.models import PME
from etools.applications.audit.models import Auditor, UNICEFAuditFocalPoint, UNICEFUser
from etools.applications.audit.tests.factories import AuditFocalPointUserFactory, AuditorUserFactory, EngagementFactory
from etools.applications.audit.tests.factories import (
AuditFocalPointUserFactory,
AuditorUserFactory,
AuditPartnerFactory,
EngagementFactory,
)
from etools.applications.core.tests.cases import BaseTenantTestCase
from etools.applications.organizations.tests.factories import OrganizationFactory
from etools.applications.partners.permissions import PARTNERSHIP_MANAGER_GROUP, UNICEF_USER
Expand Down Expand Up @@ -132,6 +137,22 @@ def test_get_auditor_firms(self):
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]['id'], engagement.agreement.auditor_firm.organization.id)

def test_get_auditor_firms_with_org_id(self):
PartnerFactory(organization=OrganizationFactory())
EngagementFactory()
audit_firm = AuditPartnerFactory()

with self.assertNumQueries(3):
response = self.forced_auth_req(
"get",
self.url,
data={"organization_type": "audit", "organization_id": audit_firm.organization.pk},
user=self.partnership_manager,
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]['id'], audit_firm.organization.id)

def test_get_tpm_firms(self):
PartnerFactory(organization=OrganizationFactory())
EngagementFactory()
Expand Down
13 changes: 11 additions & 2 deletions src/etools/applications/users/views_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from etools.applications.partners.permissions import user_group_permission
from etools.applications.partners.views.v3 import PMPBaseViewMixin
from etools.applications.users import views as v1, views_v2 as v2
from etools.applications.users.filters import UserRoleFilter, UserStatusFilter
from etools.applications.users.filters import OrganizationFilter, UserRoleFilter, UserStatusFilter
from etools.applications.users.mixins import (
AUDIT_ACTIVE_GROUPS,
GroupEditPermissionMixin,
Expand Down Expand Up @@ -224,6 +224,8 @@ class OrganizationListView(ListAPIView):
model = Organization
serializer_class = SimpleOrganizationSerializer
permission_classes = (IsAuthenticated, IsUNICEFUser)
filter_backends = (DjangoFilterBackend,)
filterset_class = OrganizationFilter

def get_queryset(self):
queryset = Organization.objects.all() \
Expand All @@ -241,8 +243,15 @@ def get_queryset(self):
auditorfirm__hidden=False),
"tpm": dict(tpmpartner__countries=connection.tenant, tpmpartner__hidden=False)
}
organization_type = self.request.query_params.get('organization_type', 'partner')
# Audit firms without audits are included when organization_id is present
# so that staff members can be added in AMP (ch35468)
if organization_type == 'audit' and 'organization_id' in self.request.query_params:
organization_type_filter['audit'] = dict(auditorfirm__hidden=False,
id__in=[int(self.request.query_params['organization_id'])])

return queryset\
.filter(**organization_type_filter[self.request.query_params.get('organization_type', 'partner')])\
.filter(**organization_type_filter[organization_type])\
.distinct()


Expand Down

0 comments on commit 4d3dec4

Please sign in to comment.