Skip to content

Commit

Permalink
Fix InterventionPartnershipDashView query
Browse files Browse the repository at this point in the history
  • Loading branch information
reinbach committed Apr 5, 2018
1 parent a7cdbc6 commit 0e8e341
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
4 changes: 2 additions & 2 deletions EquiTrack/partners/serializers/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def get_disbursement_percent(self, obj):
return "%.1f" % percent

def get_days_last_pv(self, obj):
return obj.days_since_last_pv.days if obj.days_since_last_pv else None
return "" # obj.days_since_last_pv.days if obj.days_since_last_pv else None

def get_last_pv_date(self, obj):
return obj.last_pv_date
return "" # obj.last_pv_date

def get_offices_names(self, obj):
return ",".join(o.name for o in obj.offices.all())
Expand Down
66 changes: 50 additions & 16 deletions EquiTrack/partners/views/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.generics import (
ListCreateAPIView
)
from rest_framework.response import Response

from partners.models import (
Intervention,
Expand All @@ -27,21 +28,7 @@ class InterventionPartnershipDashView(ListCreateAPIView):
renderer_classes = (r.JSONRenderer, PartnershipDashCSVRenderer)

def get_queryset(self):
today = datetime.now()
delta = ExpressionWrapper(today - F('last_pv_date'), output_field=DurationField())

qs = Intervention.objects.exclude(status=Intervention.DRAFT)

qs = qs \
.annotate(last_pv_date=Max(Case(When(travel_activities__travel_type=TravelType.PROGRAMME_MONITORING,
travel_activities__travels__traveler=F(
'travel_activities__primary_traveler'),
travel_activities__travels__status=Travel.COMPLETED,
then=F('travel_activities__date')),
output_field=DateTimeField())))

qs = qs.annotate(days_since_last_pv=delta)
qs = qs.annotate(
qs = Intervention.objects.exclude(status=Intervention.DRAFT).annotate(
Max("frs__end_date"),
Min("frs__start_date"),
Sum("frs__total_amt"),
Expand All @@ -56,13 +43,60 @@ def get_queryset(self):
)
return qs.order_by('agreement__partner__name')

def append_last_pv_date(self, serializer):
delta = ExpressionWrapper(
datetime.now() - F('last_pv_date'),
output_field=DurationField()
)
qs = Intervention.objects.exclude(
status=Intervention.DRAFT
).annotate(
last_pv_date=Max(
Case(
When(
travel_activities__travel_type=TravelType.PROGRAMME_MONITORING,
travel_activities__travels__traveler=F(
'travel_activities__primary_traveler'
),
travel_activities__travels__status=Travel.COMPLETED,
then=F('travel_activities__date')
),
output_field=DateTimeField()
)
)
)
qs = qs.annotate(days_since_last_pv=delta)
pv_dates = {}
for i in qs:
pv_dates[str(i.pk)] = {
"last_pv_date": i.last_pv_date,
"days_last_pv": i.days_since_last_pv.days if i.days_since_last_pv else None,
}
# Add last_pv_date
for d in serializer.data:
pk = d["intervention_id"]
d["last_pv_date"] = pv_dates[pk]["last_pv_date"]
d["days_last_pv"] = pv_dates[pk]["days_last_pv"]
return serializer

def list(self, request):
"""
Checks for format query parameter
:returns: JSON or CSV file
"""
query_params = self.request.query_params
response = super(InterventionPartnershipDashView, self).list(request)
queryset = self.filter_queryset(self.get_queryset())

page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
self.append_last_pv_date(serializer)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(queryset, many=True)
self.append_last_pv_date(serializer)

response = Response(serializer.data)
if "format" in query_params.keys():
if query_params.get("format") == 'csv':
response['Content-Disposition'] = "attachment;filename=partnership-dash.csv"
Expand Down

0 comments on commit 0e8e341

Please sign in to comment.