def get_total_row(self): def _clean_total_row(val, col): if isinstance(val, numbers.Number): return val elif col.calculate_total: return 0 return '' def _get_relevant_column_ids(col, column_id_to_expanded_column_ids): return column_id_to_expanded_column_ids.get( col.column_id, [col.column_id]) expanded_columns = get_expanded_columns(self.top_level_columns, self.config) qc = self.query_context() session_helper = connection_manager.get_session_helper(self.engine_id) with session_helper.session_context() as session: totals = qc.totals(session.connection(), [ column_id for col in self.top_level_columns for column_id in _get_relevant_column_ids( col, expanded_columns) if col.calculate_total ], self.filter_values) total_row = [ _clean_total_row(totals.get(column_id), col) for col in self.top_level_columns for column_id in _get_relevant_column_ids(col, expanded_columns) ] if total_row and total_row[0] is '': total_row[0] = ugettext('Total') return total_row
def export_table(self): try: data = self.data_source data.set_filter_values(self.filter_values) data.set_order_by([(o['field'], o['order']) for o in self.spec.sort_expression]) except UserReportsError as e: return self.render_json_response({ 'error': e.message, }) raw_rows = list(data.get_data()) headers = [column.header for column in self.data_source.columns] column_id_to_expanded_column_ids = get_expanded_columns(data.top_level_columns, data.config) column_ids = [] for column in self.spec.report_columns: column_ids.extend(column_id_to_expanded_column_ids.get(column.column_id, [column.column_id])) rows = [[raw_row[column_id] for column_id in column_ids] for raw_row in raw_rows] total_rows = [data.get_total_row()] if data.has_total_row else [] return [ [ self.title, [headers] + rows + total_rows ] ]
def export_table(self): try: data = self.data_source data.set_filter_values(self.filter_values) data.set_order_by([(o['field'], o['order']) for o in self.spec.sort_expression]) except UserReportsError as e: return self.render_json_response({ 'error': e.message, }) raw_rows = list(data.get_data()) headers = [column.header for column in self.data_source.columns] column_id_to_expanded_column_ids = get_expanded_columns(data.column_configs, data.config) column_ids = [] for column in self.spec.report_columns: column_ids.extend(column_id_to_expanded_column_ids.get(column.column_id, [column.column_id])) rows = [[raw_row[column_id] for column_id in column_ids] for raw_row in raw_rows] total_rows = ( [get_total_row( raw_rows, data.aggregation_columns, data.column_configs, column_id_to_expanded_column_ids )] if data.has_total_row else [] ) return [ [ self.title, [headers] + rows + total_rows ] ]
def get_ajax(self, request): try: data_source = self.data_source if len(data_source.columns) > 50: raise UserReportsError(_("This report has too many columns to be displayed")) data_source.set_filter_values(self.filter_values) sort_column = request.GET.get('iSortCol_0') sort_order = request.GET.get('sSortDir_0', 'ASC') echo = int(request.GET.get('sEcho', 1)) if sort_column and echo != 1: data_source.set_order_by( [(data_source.column_configs[int(sort_column)].column_id, sort_order.upper())] ) total_records = data_source.get_total_records() except UserReportsError as e: if settings.DEBUG: raise return self.render_json_response({ 'error': e.message, 'aaData': [], 'iTotalRecords': 0, 'iTotalDisplayRecords': 0, }) except TableNotFoundWarning: if self.spec.report_meta.created_by_builder: msg = _( "The database table backing your report does not exist yet. " "Please wait while the report is populated." ) else: msg = _( "The database table backing your report does not exist yet. " "You must rebuild the data source before viewing the report." ) return self.render_json_response({ 'warning': msg }) # todo: this is ghetto pagination - still doing a lot of work in the database datatables_params = DatatablesParams.from_request_dict(request.GET) end = min(datatables_params.start + datatables_params.count, total_records) data = list(data_source.get_data()) page = data[datatables_params.start:end] json_response = { 'aaData': page, "sEcho": self.request_dict.get('sEcho', 0), "iTotalRecords": total_records, "iTotalDisplayRecords": total_records, } if data_source.has_total_row: json_response.update({ "total_row": get_total_row( data, data_source.aggregation_columns, data_source.column_configs, get_expanded_columns(data_source.column_configs, data_source.config) ), }) return self.render_json_response(json_response)
def final_column_ids(self): from corehq.apps.userreports.reports.util import get_expanded_columns def _get_relevant_column_ids(col, column_id_to_expanded_column_ids): return column_id_to_expanded_column_ids.get(col.column_id, [col.column_id]) expanded_columns = get_expanded_columns(self.top_level_columns, self.config) return OrderedDict([ (column_id, col) for col in self.top_level_columns for column_id in _get_relevant_column_ids(col, expanded_columns) ])
def export_table(self): try: data = self.data_source data.set_filter_values(self.filter_values) data.set_order_by([(o['field'], o['order']) for o in self.spec.sort_expression]) except UserReportsError as e: return self.render_json_response({ 'error': e.message, }) raw_rows = list(data.get_data()) headers = [ column.header for column in self.data_source.inner_columns if column.data_tables_column.visible ] column_id_to_expanded_column_ids = get_expanded_columns( data.top_level_columns, data.config) column_ids = [] for column in self.spec.report_columns: if column.visible: column_ids.extend( column_id_to_expanded_column_ids.get( column.column_id, [column.column_id])) rows = [[raw_row[column_id] for column_id in column_ids] for raw_row in raw_rows] total_rows = [data.get_total_row()] if data.has_total_row else [] export_table = [[self.title, [headers] + rows + total_rows]] if INCLUDE_METADATA_IN_UCR_EXCEL_EXPORTS.enabled(self.domain): time_zone = get_timezone_for_domain(self.domain) export_table.append([ 'metadata', [ [_('Report Name'), self.title], [ _('Generated On'), datetime.now(time_zone).strftime('%Y-%m-%d %H:%M') ], ] + list(self._get_filter_values()) ]) return export_table
def get_total_row(self): def _clean_total_row(val, col): if isinstance(val, numbers.Number): return val elif col.calculate_total: return 0 return '' def _get_relevant_column_ids(col, column_id_to_expanded_column_ids): return column_id_to_expanded_column_ids.get(col.column_id, [col.column_id]) expanded_columns = get_expanded_columns(self.column_configs, self.config) qc = self.query_context() for c in self.columns: qc.append_column(c.view) session = connection_manager.get_scoped_session(self.engine_id) totals = qc.totals( session.connection(), [ column_id for col in self.column_configs for column_id in _get_relevant_column_ids(col, expanded_columns) if col.calculate_total ], self.filter_values ) total_row = [ _clean_total_row(totals.get(column_id), col) for col in self.column_configs for column_id in _get_relevant_column_ids( col, expanded_columns ) ] if total_row and total_row[0] is '': total_row[0] = ugettext('Total') return total_row
def _get_relevant_column_ids(col): col_id_to_expanded_col = get_expanded_columns( self.top_level_columns, self.config) return col_id_to_expanded_col.get(col.column_id, [col.column_id])