<s> import json <EOL> import os <EOL> from corehq . apps . locations . models import SQLLocation <EOL> from corehq . apps . reports . datatables import DataTablesHeader , DataTablesColumn , DataTablesColumnGroup <EOL> from corehq . apps . reports_core . filters import Choice <EOL> from corehq . apps . userreports . models import ReportConfiguration <EOL> from corehq . apps . userreports . reports . factory import ReportFactory <EOL> from django . utils . translation import ugettext as _ <EOL> from corehq . util . couch import get_document_or_not_found <EOL> class ICDSData ( object ) : <EOL> def __init__ ( self , domain , filters , report_id ) : <EOL> report_config = ReportFactory . from_spec ( <EOL> get_document_or_not_found ( <EOL> ReportConfiguration , <EOL> domain , <EOL> report_id <EOL> ) <EOL> ) <EOL> report_config . set_filter_values ( filters ) <EOL> self . report_config = report_config <EOL> def data ( self ) : <EOL> return self . report_config . get_data ( ) <EOL> class ICDSMixin ( object ) : <EOL> @ property <EOL> def sources ( self ) : <EOL> with open ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) as f : <EOL> return json . loads ( f . read ( ) ) [ self . slug ] <EOL> def custom_data ( self , filters , domain ) : <EOL> data = { } <EOL> for config in self . sources [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in config : <EOL> filters . update ( { config [ '<STR_LIT>' ] : self . config [ '<STR_LIT>' ] } ) <EOL> report_data = ICDSData ( domain , filters , config [ '<STR_LIT:id>' ] ) . data ( ) <EOL> for column in config [ '<STR_LIT>' ] : <EOL> column_agg_func = column [ '<STR_LIT>' ] <EOL> column_name = column [ '<STR_LIT>' ] <EOL> column_data = <NUM_LIT:0> <EOL> if column_agg_func == '<STR_LIT>' : <EOL> column_data = sum ( [ x [ column_name ] for x in report_data ] ) <EOL> elif column_agg_func == '<STR_LIT:count>' : <EOL> column_data = len ( report_data ) <EOL> data . update ( { <EOL> column_name : data . get ( column_name , <NUM_LIT:0> ) + column_data <EOL> } ) <EOL> return data <EOL> class Identification ( object ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> @ property <EOL> def headers ( self ) : <EOL> return DataTablesHeader ( <EOL> DataTablesColumn ( '<STR_LIT>' , sortable = False ) , <EOL> DataTablesColumn ( '<STR_LIT:Name>' , sortable = False ) , <EOL> DataTablesColumn ( '<STR_LIT>' , sortable = False ) <EOL> ) <EOL> @ property <EOL> def rows ( self ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> chosen_location = SQLLocation . objects . get ( <EOL> location_id = self . config [ '<STR_LIT>' ] <EOL> ) . get_ancestors ( include_self = True ) <EOL> rows = [ ] <EOL> for loc_type in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> loc = chosen_location . filter ( location_type__name = loc_type . lower ( ) ) <EOL> if len ( loc ) == <NUM_LIT:1> : <EOL> rows . append ( [ loc_type , loc [ <NUM_LIT:0> ] . name , loc [ <NUM_LIT:0> ] . site_code ] ) <EOL> else : <EOL> rows . append ( [ loc_type , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> return rows <EOL> class Operationalization ( ICDSMixin ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> @ property <EOL> def headers ( self ) : <EOL> return DataTablesHeader ( <EOL> DataTablesColumn ( '<STR_LIT>' , sortable = False ) , <EOL> DataTablesColumn ( '<STR_LIT>' , sortable = False ) , <EOL> DataTablesColumn ( '<STR_LIT>' , sortable = False ) , <EOL> DataTablesColumn ( '<STR_LIT>' , sortable = False ) <EOL> ) <EOL> @ property <EOL> def rows ( self ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> selected_location = SQLLocation . objects . get ( <EOL> location_id = self . config [ '<STR_LIT>' ] <EOL> ) <EOL> awcs = selected_location . get_descendants ( include_self = True ) . filter ( <EOL> location_type__name = '<STR_LIT>' <EOL> ) <EOL> awc_number = [ <EOL> loc for loc in awcs <EOL> if '<STR_LIT:test>' not in loc . metadata and loc . metadata . get ( '<STR_LIT:test>' , '<STR_LIT>' ) . lower ( ) != '<STR_LIT:yes>' <EOL> ] <EOL> key = selected_location . location_type . name . lower ( ) + '<STR_LIT>' <EOL> data = self . custom_data ( <EOL> domain = self . config [ '<STR_LIT>' ] , <EOL> filters = { <EOL> key : [ Choice ( value = selected_location . location_id , display = selected_location . name ) ] , <EOL> } <EOL> ) <EOL> return [ <EOL> [ <EOL> '<STR_LIT>' , <EOL> len ( awc_number ) , <EOL> <NUM_LIT:0> , <EOL> data [ '<STR_LIT>' ] <EOL> ] , <EOL> [ <EOL> '<STR_LIT>' , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> <EOL> ] <EOL> ] <EOL> class Sectors ( object ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> @ property <EOL> def headers ( self ) : <EOL> return [ ] <EOL> @ property <EOL> def rows ( self ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> selected_location = SQLLocation . objects . get ( <EOL> location_id = self . config [ '<STR_LIT>' ] <EOL> ) <EOL> supervisors = selected_location . get_descendants ( include_self = True ) . filter ( <EOL> location_type__name = '<STR_LIT>' <EOL> ) <EOL> sup_number = [ <EOL> loc for loc in supervisors <EOL> if '<STR_LIT:test>' not in loc . metadata and loc . metadata . get ( '<STR_LIT:test>' , '<STR_LIT>' ) . lower ( ) != '<STR_LIT:yes>' <EOL> ] <EOL> return [ <EOL> [ <EOL> "<STR_LIT>" , <EOL> len ( sup_number ) <EOL> ] <EOL> ] <EOL> class Population ( ICDSMixin ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> def __init__ ( self , config ) : <EOL> self . config = dict ( <EOL> domain = config [ '<STR_LIT>' ] , <EOL> location_id = config [ '<STR_LIT>' ] <EOL> ) <EOL> @ property <EOL> def headers ( self ) : <EOL> return [ ] <EOL> @ property <EOL> def rows ( self ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> data = self . custom_data ( <EOL> domain = self . config [ '<STR_LIT>' ] , <EOL> filters = { } <EOL> ) <EOL> return [ <EOL> [ <EOL> "<STR_LIT>" , <EOL> data [ '<STR_LIT>' ] <EOL> ] <EOL> ] <EOL> class BirthsAndDeaths ( ICDSMixin ) : <EOL> title = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> @ property <EOL> def headers ( self ) : <EOL> return DataTablesHeader ( <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumnGroup ( <EOL> _ ( '<STR_LIT>' ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) , sortable = False ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) , sortable = False ) , <EOL> ) , <EOL> DataTablesColumnGroup ( <EOL> _ ( '<STR_LIT>' ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) , sortable = False ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) , sortable = False ) , <EOL> ) <EOL> ) <EOL> @ property <EOL> def rows ( self ) : <EOL> if self . config [ '<STR_LIT>' ] : <EOL> selected_location = SQLLocation . objects . get ( <EOL> location_id = self . config [ '<STR_LIT>' ] <EOL> ) <EOL> key = selected_location . location_type . name . lower ( ) + '<STR_LIT>' <EOL> data = self . custom_data ( <EOL> domain = self . config [ '<STR_LIT>' ] , <EOL> filters = { <EOL> key : [ Choice ( value = selected_location . location_id , display = selected_location . name ) ] , <EOL> } <EOL> ) <EOL> rows = [ ] <EOL> for row in self . row_config : <EOL> row_data = [ ] <EOL> for idx , cell in enumerate ( row ) : <EOL> if isinstance ( cell , tuple ) : <EOL> x = data . get ( cell [ <NUM_LIT:0> ] , <NUM_LIT:0> ) <EOL> y = data . get ( cell [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> row_data . append ( x + y ) <EOL> else : <EOL> row_data . append ( data . get ( cell , cell if cell == '<STR_LIT>' or idx in [ <NUM_LIT:0> , <NUM_LIT:1> ] else <NUM_LIT:0> ) ) <EOL> rows . append ( row_data ) <EOL> return rows <EOL> @ property <EOL> def row_config ( self ) : <EOL> return ( <EOL> ( <EOL> _ ( '<STR_LIT:A>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:B>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:C>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:D>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:E>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:F>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:H>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> _ ( '<STR_LIT:I>' ) , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) , <EOL> ( <EOL> '<STR_LIT>' , <EOL> _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) , <EOL> ) </s>
