@@ -264,37 +264,58 @@ def get_databank(self, request, pk):
264264 )
265265 def get_country_figure (self , request , pk ):
266266 country = self .get_object ()
267- end_date = timezone .now ()
268- start_date = end_date + timedelta (days = - 2 * 365 )
269- start_date = request .GET .get ("start_date" , start_date )
270- end_date = request .GET .get ("end_date" , end_date )
271- appeal_conditions = Q (atype = AppealType .APPEAL )
272-
273- all_appealhistory = AppealHistory .objects .select_related ("appeal" ).filter (appeal__code__isnull = False )
274- all_appealhistory = all_appealhistory .filter (country = country )
275- if start_date and end_date :
276- all_appealhistory = all_appealhistory .filter (start_date__lte = end_date , end_date__gte = start_date )
267+
268+ now = timezone .now ()
269+ start_date_from = request .GET .get ("start_date_from" , timezone .now () + timedelta (days = - 2 * 365 ))
270+ start_date_to = request .GET .get ("start_date_to" , timezone .now ())
271+
272+ appeal_conditions = Q (atype = AppealType .APPEAL ) | Q (atype = AppealType .INTL )
273+
274+ all_appealhistory = AppealHistory .objects .select_related ("appeal" ).filter (
275+ country = country ,
276+ appeal__code__isnull = False ,
277+ valid_from__lt = now , # TODO: Allow user to provide this?
278+ valid_to__gt = now , # TODO: Allow user to provide this?
279+ )
280+ if start_date_from and start_date_to :
281+ all_appealhistory = all_appealhistory .filter (
282+ start_date__gte = start_date_from ,
283+ start_date__lte = start_date_to ,
284+ )
285+
277286 appeals_aggregated = all_appealhistory .annotate (
278287 appeal_with_dref = Count (
279288 Case (
280289 When (Q (atype = AppealType .DREF ), then = 1 ),
281290 output_field = IntegerField (),
282291 )
283292 ),
284- appeal_without_dref = Count (Case (When (Q ( atype = AppealType . APPEAL ) , then = 1 ), output_field = IntegerField ()), distinct = True ),
293+ appeal_without_dref = Count (Case (When (appeal_conditions , then = 1 ), output_field = IntegerField ())),
285294 total_population = (
286295 Case (
287296 When (appeal_conditions | Q (atype = AppealType .DREF ), then = F ("num_beneficiaries" )),
288297 output_field = IntegerField (),
289298 )
290299 ),
291300 amount_requested_all = (
301+ Case (
302+ When (appeal_conditions , then = F ("amount_requested" )),
303+ output_field = IntegerField (),
304+ )
305+ ),
306+ amordref = (
292307 Case (
293308 When (appeal_conditions | Q (atype = AppealType .DREF ), then = F ("amount_requested" )),
294309 output_field = IntegerField (),
295310 )
296311 ),
297- amount_funded_all = (
312+ amof = (
313+ Case (
314+ When (appeal_conditions , then = F ("amount_funded" )),
315+ output_field = IntegerField (),
316+ )
317+ ),
318+ amofdref = (
298319 Case (
299320 When (appeal_conditions | Q (atype = AppealType .DREF ), then = F ("amount_funded" )),
300321 output_field = IntegerField (),
@@ -305,8 +326,10 @@ def get_country_figure(self, request, pk):
305326 active_drefs = Sum ("appeal_with_dref" ),
306327 active_appeals = Sum ("appeal_without_dref" ),
307328 target_population = Sum ("total_population" ),
308- total_amount_requested = Sum ("amount_requested_all" ),
309- total_amount_funded = Sum ("amount_funded_all" ),
329+ amount_requested = Sum ("amount_requested_all" ),
330+ amount_requested_dref_included = Sum ("amordref" ),
331+ amount_funded = Sum ("amof" ),
332+ amount_funded_dref_included = Sum ("amofdref" ),
310333 emergencies = Sum ("emergencies_count" ),
311334 )
312335 return Response (CountryKeyFigureSerializer (appeals_aggregated ).data )
@@ -320,10 +343,9 @@ def get_country_figure(self, request, pk):
320343 @action (detail = True , url_path = "disaster-count" , pagination_class = None )
321344 def get_country_disaster_count (self , request , pk ):
322345 country = self .get_object ()
323- end_date = timezone .now ()
324- start_date = end_date + timedelta (days = - 2 * 365 )
325- start_date = request .GET .get ("start_date" , start_date )
326- end_date = request .GET .get ("end_date" , end_date )
346+
347+ start_date_from = request .GET .get ("start_date_from" , timezone .now () + timedelta (days = - 2 * 365 ))
348+ start_date_to = request .GET .get ("start_date_to" , timezone .now ())
327349
328350 queryset = (
329351 Event .objects .filter (
@@ -339,8 +361,12 @@ def get_country_disaster_count(self, request, pk):
339361 .order_by ("countries" , "dtype__name" )
340362 )
341363
342- if start_date and end_date :
343- queryset = queryset .filter (disaster_start_date__gte = start_date , disaster_start_date__lte = end_date )
364+ if start_date_from and start_date_to :
365+ queryset = queryset .filter (
366+ disaster_start_date__gte = start_date_from ,
367+ disaster_start_date__lte = start_date_to ,
368+ )
369+
344370 return Response (CountryDisasterTypeCountSerializer (queryset , many = True ).data )
345371
346372 @extend_schema (
@@ -351,16 +377,16 @@ def get_country_disaster_count(self, request, pk):
351377 @action (detail = True , url_path = "disaster-monthly-count" , pagination_class = None )
352378 def get_country_disaster_monthly_count (self , request , pk ):
353379 country = self .get_object ()
354- end_date = timezone . now ()
355- start_date = end_date + timedelta (days = - 2 * 365 )
356- start_date = request .GET .get ("start_date " , start_date )
357- end_date = request . GET . get ( "end_date" , end_date )
380+
381+ start_date_from = request . GET . get ( "start_date_from" , timezone . now () + timedelta (days = - 2 * 365 ) )
382+ start_date_to = request .GET .get ("start_date_to " , timezone . now () )
383+
358384 queryset = (
359385 Event .objects .filter (
360386 countries__in = [country .id ],
361387 dtype__isnull = False ,
362388 )
363- .annotate (date = TruncMonth ("created_at " ))
389+ .annotate (date = TruncMonth ("disaster_start_date " ))
364390 .values ("date" , "countries" , "dtype" )
365391 .annotate (
366392 appeal_targeted_population = Coalesce (
@@ -393,8 +419,11 @@ def get_country_disaster_monthly_count(self, request, pk):
393419 .order_by ("date" , "countries" , "dtype__name" )
394420 )
395421
396- if start_date and end_date :
397- queryset = queryset .filter (disaster_start_date__gte = start_date , disaster_start_date__lte = end_date )
422+ if start_date_from and start_date_to :
423+ queryset = queryset .filter (
424+ disaster_start_date__gte = start_date_from ,
425+ disaster_start_date__lte = start_date_to ,
426+ )
398427
399428 return Response (CountryDisasterTypeMonthlySerializer (queryset , many = True ).data )
400429
@@ -406,18 +435,18 @@ def get_country_disaster_monthly_count(self, request, pk):
406435 @action (detail = True , url_path = "historical-disaster" , pagination_class = None )
407436 def get_country_historical_disaster (self , request , pk ):
408437 country = self .get_object ()
409- end_date = timezone . now ()
410- start_date = end_date + timedelta (days = - 2 * 365 )
411- start_date = request .GET .get ("start_date " , start_date )
412- end_date = request . GET . get ( "end_date" , end_date )
438+
439+ start_date_from = request . GET . get ( "start_date_from" , timezone . now () + timedelta (days = - 2 * 365 ) )
440+ start_date_to = request .GET .get ("start_date_to " , timezone . now () )
441+
413442 dtype = request .GET .get ("dtype" , None )
414443
415444 queryset = (
416445 Event .objects .filter (
417446 countries__in = [country .id ],
418447 dtype__isnull = False ,
419448 )
420- .annotate (date = TruncMonth ("created_at " ))
449+ .annotate (date = TruncMonth ("disaster_start_date " ))
421450 .values ("date" , "dtype" , "countries" )
422451 .annotate (
423452 appeal_targeted_population = Coalesce (
@@ -452,8 +481,11 @@ def get_country_historical_disaster(self, request, pk):
452481 .order_by ("date" , "countries" , "dtype__name" )
453482 )
454483
455- if start_date and end_date :
456- queryset = queryset .filter (disaster_start_date__gte = start_date , disaster_start_date__lte = end_date )
484+ if start_date_from and start_date_to :
485+ queryset = queryset .filter (
486+ disaster_start_date__gte = start_date_from ,
487+ disaster_start_date__lte = start_date_to ,
488+ )
457489
458490 if dtype :
459491 queryset = queryset .filter (dtype = dtype )
@@ -747,15 +779,14 @@ def get_serializer_class(self):
747779class AppealViewset (mixins .ListModelMixin , viewsets .GenericViewSet ):
748780 """Used to get Appeals from AppealHistory. Has no 'read' option, just 'list'."""
749781
750- # queryset = Appeal .objects.select_related('dtype', 'country', 'region').all()
751- # queryset = AppealHistory.objects.select_related(' appeal__event', 'dtype', 'country', 'region').all()
752- queryset = AppealHistory . objects . select_related ( "appeal__event" , "dtype" , "country" , "region" ). filter (
753- appeal__code__isnull = False
754- )
755- # serializer_class = AppealSerializer
782+ queryset = AppealHistory .objects .select_related (
783+ " appeal__event" ,
784+ "dtype" ,
785+ "country" ,
786+ "region" ,
787+ ). filter ( appeal__code__isnull = False )
756788 serializer_class = AppealHistorySerializer
757789 ordering_fields = "__all__"
758- # filterset_class = AppealFilter
759790 filterset_class = AppealHistoryFilter
760791 search_fields = (
761792 "appeal__name" ,
@@ -765,9 +796,7 @@ class AppealViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
765796 def get_serializer_class (self ):
766797 if is_tableau (self .request ) is True :
767798 return AppealHistoryTableauSerializer
768- # return AppealTableauSerializer
769799 return AppealHistorySerializer
770- # return AppealSerializer
771800
772801 def remove_unconfirmed_event (self , obj ):
773802 if obj ["needs_confirmation" ]:
@@ -779,9 +808,13 @@ def remove_unconfirmed_events(self, objs):
779808
780809 # Overwrite to exclude the events which require confirmation
781810 def list (self , request , * args , ** kwargs ):
782- now = timezone .now ()
783- date = request .GET .get ("date" , now )
784- queryset = self .filter_queryset (self .get_queryset ()).filter (valid_from__lt = date , valid_to__gt = date )
811+ date = request .GET .get ("date" , timezone .now ())
812+ queryset = self .filter_queryset (
813+ self .get_queryset ().filter (
814+ valid_from__lt = date ,
815+ valid_to__gt = date ,
816+ )
817+ )
785818
786819 page = self .paginate_queryset (queryset )
787820 if page is not None :
0 commit comments