<s> import itertools <EOL> from sqlagg . columns import MonthColumn , YearColumn , YearQuarterColumn <EOL> from sqlagg . filters import IN <EOL> from corehq . apps . reports . sqlreport import SqlReportException , AggregateColumn , DatabaseColumn <EOL> from corehq . apps . reports . util import format_datatables_data , get_INFilter_bindparams <EOL> from corehq . sql_db . connections import connection_manager <EOL> def combine_month_year ( year , month ) : <EOL> months = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> return "<STR_LIT>" % ( months [ int ( month ) - <NUM_LIT:1> ] , int ( year ) ) , int ( year * <NUM_LIT:100> + month ) <EOL> def combine_quarter_year ( year , quarter ) : <EOL> return "<STR_LIT>" % ( int ( year ) , int ( quarter ) ) , int ( year * <NUM_LIT:10> + quarter ) <EOL> def format_year ( year ) : <EOL> return format_datatables_data ( int ( year ) , int ( year ) ) <EOL> def format_date ( value ) : <EOL> return format_datatables_data ( value [ <NUM_LIT:0> ] , value [ <NUM_LIT:1> ] ) <EOL> class CVSUSqlDataMixin ( object ) : <EOL> def _find_column_view ( self , column_name ) : <EOL> for column in self . columns : <EOL> if isinstance ( column , AggregateColumn ) : <EOL> for c in column . view . columns : <EOL> if c . name == column_name : <EOL> return c <EOL> elif column . slug == column_name : <EOL> return column . view <EOL> def _find_column ( self , column_name ) : <EOL> for column in self . columns : <EOL> if column . slug == column_name : <EOL> return column <EOL> def _get_data ( self ) : <EOL> if self . keys is not None and not self . group_by : <EOL> raise SqlReportException ( '<STR_LIT>' ) <EOL> qc = self . query_context ( ) <EOL> for c in self . columns : <EOL> qc . append_column ( c . view ) <EOL> session = connection_manager . get_scoped_session ( self . engine_id ) <EOL> try : <EOL> for qm in qc . query_meta . values ( ) : <EOL> date_aggregation_column = None <EOL> if len ( qm . columns ) == <NUM_LIT:1> : <EOL> c = self . _find_column ( qm . columns [ <NUM_LIT:0> ] . column_name ) <EOL> if hasattr ( c , '<STR_LIT>' ) : <EOL> date_aggregation_column = c . date_aggregation_column <EOL> columns_names = itertools . chain . from_iterable ( [ ( col . column_name , col . alias ) for col in qm . columns ] ) <EOL> for group_by in self . group_by : <EOL> if group_by not in columns_names : <EOL> column = self . _find_column_view ( group_by ) <EOL> if column : <EOL> if date_aggregation_column : <EOL> column . key = date_aggregation_column <EOL> column . sql_column . column_name = date_aggregation_column <EOL> qm . append_column ( column ) <EOL> return qc . resolve ( session . connection ( ) , self . filter_values ) <EOL> except : <EOL> session . rollback ( ) <EOL> raise <EOL> class DateColumnMixin ( object ) : <EOL> @ property <EOL> def date_column ( self ) : <EOL> if self . grouping == '<STR_LIT>' : <EOL> return AggregateColumn ( <EOL> "<STR_LIT>" , combine_month_year , <EOL> [ YearColumn ( '<STR_LIT>' , alias = '<STR_LIT>' ) , MonthColumn ( '<STR_LIT>' , alias = '<STR_LIT>' ) ] , <EOL> format_fn = format_date ) <EOL> elif self . grouping == '<STR_LIT>' : <EOL> return AggregateColumn ( <EOL> "<STR_LIT>" , combine_quarter_year , <EOL> [ YearColumn ( '<STR_LIT>' , alias = '<STR_LIT>' ) , YearQuarterColumn ( '<STR_LIT>' , alias = '<STR_LIT>' ) ] , <EOL> format_fn = format_date ) <EOL> else : <EOL> return DatabaseColumn ( "<STR_LIT>" , YearColumn ( '<STR_LIT>' , alias = '<STR_LIT>' ) , format_fn = format_year ) <EOL> class FilterMixin ( object ) : <EOL> @ property <EOL> def filters ( self ) : <EOL> if not self . group_by_district : <EOL> users = tuple ( [ user . user_id for user in self . users ] ) <EOL> return [ IN ( "<STR_LIT>" , get_INFilter_bindparams ( "<STR_LIT>" , users ) ) ] <EOL> return [ ] </s>
