Skip to content

Commit

Permalink
Merge pull request #1889 from unicef/feature/cluster_api
Browse files Browse the repository at this point in the history
[ch7900] Dashboard: filter by cluster + cluster API
  • Loading branch information
robertavram authored Oct 25, 2018
2 parents ffaa6a2 + d7225c5 commit 99ccc4e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/etools/applications/partners/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ def frs_qs(self):
def maps_qs(self):
qs = self.get_queryset().prefetch_related('flat_locations').distinct().annotate(
results=StringConcat("result_links__cp_output__name", separator="|", distinct=True),
clusters=StringConcat("result_links__ll_results__applied_indicators__cluster_indicator_title",
clusters=StringConcat("result_links__ll_results__applied_indicators__cluster_name",
separator="|", distinct=True),
)
return qs
Expand Down
2 changes: 1 addition & 1 deletion src/etools/applications/partners/views/interventions_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class InterventionListMapView(QueryStringFilterMixin, ListCreateAPIView):
('grants', 'frs__fr_items__grant_number__in'),
('unicef_focal_points', 'unicef_focal_points__in'),
('interventions', 'pk__in'),
('clusters', 'result_links__ll_results__applied_indicators__cluster_indicator_title__icontains'),
('clusters', 'result_links__ll_results__applied_indicators__cluster_name__icontains'),
)

def get_queryset(self):
Expand Down
7 changes: 7 additions & 0 deletions src/etools/applications/reports/serializers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ class Meta:
fields = '__all__'


class ClusterSerializer(serializers.ModelSerializer):

class Meta:
model = AppliedIndicator
fields = 'cluster_name',


class LowerResultSimpleCUSerializer(serializers.ModelSerializer):

code = serializers.CharField(read_only=True)
Expand Down
52 changes: 50 additions & 2 deletions src/etools/applications/reports/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import datetime
from operator import itemgetter

from django.urls import reverse
from django.test import SimpleTestCase

from django.urls import reverse

from rest_framework import status
from tablib.core import Dataset
Expand Down Expand Up @@ -861,6 +860,55 @@ def test_csv_flat_export_api(self):
self.assertEqual(len(dataset[0]), 31)


class TestClusterListAPIView(BaseTenantTestCase):
@classmethod
def setUpTestData(cls):
cls.unicef_staff = UserFactory(is_staff=True)
cls.intervention = InterventionFactory()
cls.result_link = InterventionResultLinkFactory()
cls.lower_result = LowerResultFactory(
name="LL Name",
result_link=cls.result_link,
)
cls.indicator = IndicatorBlueprintFactory()
AppliedIndicatorFactory(
context_code="CC321",
indicator=cls.indicator,
lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link),
cluster_name='ABC',
)
AppliedIndicatorFactory(
context_code="CC321",
indicator=cls.indicator,
lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link),
cluster_name='XYZ',
)
AppliedIndicatorFactory(
context_code="CC321",
indicator=cls.indicator,
lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link),
cluster_name='XYZ',
)
AppliedIndicatorFactory(
context_code="CC321",
indicator=cls.indicator,
lower_result=LowerResultFactory(name="LL Name", result_link=cls.result_link),
)
cls.url = reverse("reports:cluster")

def test_get(self):
response = self.forced_auth_req(
"get",
self.url,
user=self.unicef_staff,
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 2)
clusters = [item['cluster_name'] for item in response.data]
clusters.sort()
self.assertEquals(['ABC', 'XYZ'], clusters)


class TestSpecialReportingRequirementListCreateView(BaseTenantTestCase):
@classmethod
def setUpTestData(cls):
Expand Down
7 changes: 6 additions & 1 deletion src/etools/applications/reports/urls_v2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.conf.urls import url, include
from django.conf.urls import include, url

from rest_framework import routers

from etools.applications.reports.views.v1 import CountryProgrammeListView, CountryProgrammeRetrieveView, SectionViewSet
from etools.applications.reports.views.v2 import (
AppliedIndicatorListAPIView,
AppliedIndicatorLocationExportView,
ClusterListAPIView,
DisaggregationListCreateView,
DisaggregationRetrieveUpdateView,
LowerResultsDeleteView,
Expand All @@ -28,6 +30,9 @@
url(r'^applied-indicators/intervention/$',
view=AppliedIndicatorLocationExportView.as_view(http_method_names=['get']),
name='intervention-applied-indicator'),
url(r'^clusters/$',
view=ClusterListAPIView.as_view(http_method_names=['get']),
name='cluster'),
url(r'^lower_results/$',
view=LowerResultsListAPIView.as_view(http_method_names=['get']),
name='lower-results'),
Expand Down
13 changes: 13 additions & 0 deletions src/etools/applications/reports/views/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from etools.applications.reports.serializers.v1 import IndicatorSerializer
from etools.applications.reports.serializers.v2 import (
AppliedIndicatorSerializer,
ClusterSerializer,
DisaggregationSerializer,
LowerResultSerializer,
MinimalOutputListSerializer,
Expand Down Expand Up @@ -388,6 +389,18 @@ def get_indicators(self, intervention):
return qs


class ClusterListAPIView(ListAPIView):
"""Returns a list of Clusters"""
model = AppliedIndicator
serializer_class = ClusterSerializer
renderer_classes = (
JSONRenderer,
CSVRenderer,
CSVFlatRenderer,
)
queryset = AppliedIndicator.objects.filter(cluster_name__isnull=False).values('cluster_name').distinct()


class SpecialReportingRequirementListCreateView(ListCreateAPIView):
serializer_class = SpecialReportingRequirementSerializer
permission_classes = (PartnershipManagerPermission, )
Expand Down

0 comments on commit 99ccc4e

Please sign in to comment.