11# vim: ai ts=4 sts=4 et sw=4
2+ import calendar
23import csv
34import json
45
@@ -206,7 +207,6 @@ def facilities(request):
206207 return response
207208
208209
209-
210210class CSVBuffer (object ):
211211 def write (self , value ):
212212 return value
@@ -258,10 +258,34 @@ def get_context_data(self, **kwargs):
258258
259259 context ['page_title' ] = self .page_title
260260 context ['filter_form' ] = self .filter_form
261- context ['year' ] = self .request .GET .get ('year' ) or now ().year
262- context ['month' ] = self .request .GET .get ('month' ) or ''
261+ context ['year' ] = self .selected_year
262+ context ['month' ] = self .selected_month or ''
263+ context ['location' ] = self .filter_location .pk if self .filter_location else ''
264+ context ['caption' ] = self .get_header_title ()
263265
264266 return context
267+
268+ def get_header_title (self ):
269+ if self .selected_month and self .filter_location :
270+ return 'Non-reporting centres for {}, {} {}' .format (
271+ self .filter_location .name ,
272+ calendar .month_name [int (self .selected_month )],
273+ self .selected_year
274+ )
275+ elif self .selected_month :
276+ return 'Non-reporting centres for {} {}' .format (
277+ calendar .month_name [int (self .selected_month )],
278+ self .selected_year
279+ )
280+ elif self .filter_location :
281+ return 'Non-reporting centres for {}, {}' .format (
282+ self .filter_location .name ,
283+ self .selected_year
284+ )
285+ else :
286+ return 'Non-reporting centres for {}' .format (
287+ self .selected_year
288+ )
265289
266290 def get_queryset (self ):
267291 self .filter_form = NonReportingCentresFilterForm (self .request .GET )
@@ -271,17 +295,23 @@ def get_queryset(self):
271295 filter_data = {}
272296
273297 filter_kwargs = {}
274- year = filter_data .get ('year' ) or now ().year
275- filter_kwargs ['birthregistration_records__time__year' ] = int (year )
276- month = filter_data .get ('month' )
277- if month :
278- filter_kwargs ['birthregistration_records__time__month' ] = int (month )
298+ alt_filter_kwargs = {'type__name' : 'RC' }
299+ self .selected_year = filter_data .get ('year' ) or now ().year
300+ filter_kwargs ['birthregistration_records__time__year' ] = int (self .selected_year )
301+ self .selected_month = filter_data .get ('month' )
302+ if self .selected_month :
303+ filter_kwargs ['birthregistration_records__time__month' ] = int (self .selected_month )
304+
305+ self .filter_location = filter_data .get ('location' )
306+ if self .filter_location :
307+ alt_filter_kwargs ['lft__gt' ] = self .filter_location .lft
308+ alt_filter_kwargs ['rgt__lt' ] = self .filter_location .rgt
279309
280310 if len (filter_kwargs ) == 0 :
281311 centres = Location .objects .none ()
282312 else :
283313 centres = Location .objects .filter (
284- type__name = 'RC'
314+ ** alt_filter_kwargs
285315 ).annotate (
286316 cnt = Count (Case (When (then = 1 , ** filter_kwargs )))
287317 ).filter (cnt = 0 )
0 commit comments