def update_summary_results(self, query_result): """ Updates the summary results with the given QuerySet or results list :param self: :param query_result: :return: """ if isinstance(query_result, QuerySet): # Find aggregate and normal field names aggregate_names = query_result.aggregate_names if hasattr(query_result, 'aggregate_names') else [] self.summary_fields = (query_result.field_names if hasattr(query_result, 'field_names') else []) + aggregate_names # Find aggregate and normal field titles aggregate_titles = map_dict(lambda key, value: self.cleanup_title(key), query_result.query.aggregates) if hasattr(query_result.query, 'aggregates') else [] titles = map(lambda tup: self.cleanup_title(tup[1]), query_result.query.select) + aggregate_titles # Create a lookup from field name to title self.summary_field_title_lookup = dual_map_to_dict(lambda key, value: [key, value], self.summary_fields, titles) self.summary_query_sql = str(query_result.query) elif len(query_result) > 0: # For single row aggregates. TODO figure out who to extract the names from the query self.summary_fields = query_result[0].keys() self.summary_field_title_lookup = map_to_dict(lambda key: [key, key], self.summary_fields) self.summary_query_sql = str(query_result.query) self.summary_results = list(query_result) self.save()
def create_column_to_label(self): """ Create a mapping between table column names and labels, based on the attribute names :return: """ return dual_map_to_dict( lambda attribute, label: [self.db_column_lookup[attribute], label], self.attributes, self.labels)
def create_column_to_label(self): """ Create a mapping between table column names and labels, based on the attribute names :return: """ return dual_map_to_dict( lambda attribute, label: [self.db_column_lookup[attribute], label], self.attributes, self.labels)