Example #1
0
 def rows(self):
     formatter = DataFormatter(
         self.format_class(self.columns, self.report.get_users()))
     raw_data = formatter.format(self.data,
                                 keys=self.keys,
                                 group_by=self.group_by)
     return transpose(self.columns, list(raw_data))
Example #2
0
    def rows(self):

        def cell_format(data):
            percent = 0
            if isinstance(data, dict):
                percent = 1
            return {
                'sort_key': percent,
                'html': "oui" if percent else "non"
            }

        formatter = DataFormatter(DictDataFormat(self.columns, no_value=self.no_value))
        data = formatter.format(self.data, keys=self.keys, group_by=self.group_by)
        selected_location = location_filter(self.request, location_filter_selected=True)
        if selected_location:
            locations = SQLLocation.objects.get(
                location_id=selected_location
            ).get_descendants(include_self=True).filter(location_type__code='centre-de-sante').order_by('name')
        else:
            locations = SQLLocation.objects.filter(
                domain=self.domain,
                location_type__code='centre-de-sante',
                is_archived=False
            ).order_by('name')
        user_locations = users_locations()
        for site in locations:
            loc_data = data.get(site.location_id, {})
            if site.location_id in user_locations:
                yield [
                    site.name,
                    cell_format(loc_data.get('completude', 0)),
                    cell_format(loc_data.get('promptitude', 0)),
                ]
Example #3
0
    def rows(self):
        data = self.model.data
        if isinstance(self.model, (NombreData, TauxConsommationData)):
            localizations = sorted(set(key[0] for key in data))
        else:
            localizations = sorted(set(key[1] for key in data))

        rows = []

        formatter = DataFormatter(DictDataFormat(self.model.columns, no_value=self.no_value))
        if isinstance(self.data_source, (NombreData, TauxConsommationData)):
            result = {}
            ppss = set()
            for k, v in data.iteritems():
                ppss.add(k[-2])
                if 'region_id' in self.data_source.config:
                    helper_tuple = (k[2], k[1], k[0])
                else:
                    helper_tuple = (k[1], k[0])

                result[helper_tuple] = v

            if 'region_id' in self.data_source.config:
                result_sum = {}
                for localization in localizations:
                    for pps in ppss:
                        for group in self.groups:
                            if(group.code, localization) in result_sum:
                                r = result_sum[(group.code, localization)]
                                cols = self.data_source.sum_cols
                                for col in cols:
                                    r[col] += result.get((group.code, pps, localization), {col: 0})[col]
                            else:
                                helper_dict = {}
                                for col in self.data_source.sum_cols:
                                    helper_dict[col] = 0
                                helper_dict['district_name'] = localization
                                result_sum[(group.code, localization)] = result.get((group.code,
                                                                                     pps, localization),
                                                                                    helper_dict)
                result = result_sum

            data = dict(formatter.format(result, keys=self.model.keys, group_by=self.model.group_by))
        else:
            data = dict(formatter.format(self.model.data, keys=self.model.keys, group_by=self.model.group_by))

        reversed_map = dict(zip(self.PRODUCT_NAMES.values(), self.PRODUCT_NAMES.keys()))
        for localization in localizations:
            row = [localization]
            for group in self.groups:
                if (group.code, localization) in data:
                    product = data[(group.code, localization)]
                    row.extend([product[p] for p in self.model.col_names])
                elif (self._safe_get(reversed_map, group.code), localization) in data:
                    product = data[(reversed_map[group.code], localization)]
                    row.extend([product[p] for p in self.model.col_names])
                else:
                    row.extend([self.no_value for p in self.model.col_names])
            rows.append(row)
        return rows
Example #4
0
    def rows(self):
        data = self.model.data
        if isinstance(self.model, (NombreData, TauxConsommationData)):
            localizations = sorted(set(key[0] for key in data))
        else:
            localizations = sorted(set(key[1] for key in data))

        rows = []

        formatter = DataFormatter(DictDataFormat(self.model.columns, no_value=self.no_value))
        if isinstance(self.data_source, (NombreData, TauxConsommationData)):
            result = {}
            ppss = set()
            for k, v in data.iteritems():
                ppss.add(k[-2])
                if 'region_id' in self.data_source.config:
                    helper_tuple = (k[2], k[1], k[0])
                else:
                    helper_tuple = (k[1], k[0])

                result[helper_tuple] = v

            if 'region_id' in self.data_source.config:
                result_sum = {}
                for localization in localizations:
                    for pps in ppss:
                        for group in self.groups:
                            if(group.product_id, localization) in result_sum:
                                r = result_sum[(group.product_id, localization)]
                                cols = self.data_source.sum_cols
                                for col in cols:
                                    r[col] += result.get((group.product_id, pps, localization), {col: 0})[col]
                            else:
                                helper_dict = {}
                                for col in self.data_source.sum_cols:
                                    helper_dict[col] = 0
                                helper_dict['district_name'] = localization
                                result_sum[(group.product_id, localization)] = result.get(
                                    (group.product_id, pps, localization), helper_dict)
                result = result_sum

            data = dict(formatter.format(result, keys=self.model.keys, group_by=self.model.group_by))
        else:
            data = dict(formatter.format(self.model.data, keys=self.model.keys, group_by=self.model.group_by))

        reversed_map = dict(zip(self.PRODUCT_NAMES.values(), self.PRODUCT_NAMES.keys()))
        for localization in localizations:
            row = [localization]
            for group in self.groups:
                if (group.product_id, localization) in data:
                    product = data[(group.product_id, localization)]
                    row.extend([product[p] for p in self.model.col_names])
                elif (self._safe_get(reversed_map, group.product_id), localization) in data:
                    product = data[(reversed_map[group.product_id], localization)]
                    row.extend([product[p] for p in self.model.col_names])
                else:
                    row.extend([self.no_value for p in self.model.col_names])
            rows.append(row)
        return rows
    def rows(self):
        no_value = dict(sort_key=0L, html=0L)
        model = self.model
        formatter = DataFormatter(TableDataFormat(model.columns, no_value=no_value))
        rows = list(formatter.format(model.data, keys=model.keys, group_by=model.group_by))
        if not rows:
            return []

        assert len(rows) == 1
        row = [row.get('sort_key') or 0L for row in rows[0]]
Example #6
0
    def rows(self):
        formatter = DataFormatter(TableDataFormat(self.columns, no_value=self.no_value))
        rows = list(formatter.format(self.data, keys=self.keys, group_by=self.group_by))

        #Months are displayed in chronological order
        if 'month' in self.group_by:
            from custom.intrahealth.reports import get_localized_months
            return sorted(rows, key=lambda row: get_localized_months().index(row[0]))

        return rows
Example #7
0
    def data(self):
        try:
            data = super(SqlIndicatorSet, self).data
        except (TableNotFoundException, ColumnNotFoundException) as e:
            logger.exception(e)
            return {}

        format = DictDataFormat(self.columns, no_value=self.no_value)
        formatter = DataFormatter(format, row_filter=self.include_row)
        return formatter.format(data, keys=self.keys, group_by=self.group_by)
Example #8
0
    def rows(self):
        formatter = DataFormatter(DictDataFormat(self.columns, no_value=self.no_value))
        data = formatter.format(self.data, keys=self.keys, group_by=self.group_by)

        selected_location = location_filter(self.request, location_filter_selected=True)
        selected_hierarchy = []
        if selected_location:
            selected_hierarchy = SQLLocation.objects.get(
                location_id=selected_location
            ).get_descendants(
                include_self=True
            ).filter(
                location_type__code='centre-de-sante'
            ).values_list(
                'location_id',
                flat=True
            )

        locations = SQLLocation.objects.filter(
            domain=self.domain,
            location_type__code='zone',
            is_archived=False
        ).order_by('name')
        user_locations = users_locations()
        for zone in locations:
            for reg in zone.children.order_by('name'):
                for dis in reg.children.order_by('name'):
                    for site in dis.children.order_by('name'):
                        row = data.get(site.location_id, {})
                        if show_location(site, user_locations, selected_hierarchy):
                            yield [
                                reg.name,
                                dis.name,
                                site.name,
                                row.get('cas_vus_5', EMPTY_CELL),
                                row.get('cas_suspects_5', EMPTY_CELL),
                                row.get('cas_confirmes_5', EMPTY_CELL),
                                row.get('cas_vus_5_10', EMPTY_CELL),
                                row.get('cas_suspects_5_10', EMPTY_CELL),
                                row.get('cas_confirmes_5_10', EMPTY_CELL),
                                row.get('cas_vus_10', EMPTY_CELL),
                                row.get('cas_suspects_10', EMPTY_CELL),
                                row.get('cas_confirmes_10', EMPTY_CELL),
                                row.get('cas_vus_fe', EMPTY_CELL),
                                row.get('cas_suspects_fe', EMPTY_CELL),
                                row.get('cas_confirmes_fe', EMPTY_CELL),
                                row.get('total_cas', EMPTY_CELL),
                                row.get('per_cas_5', EMPTY_CELL),
                                row.get('per_cas_5_10', EMPTY_CELL),
                                row.get('per_cas_10', EMPTY_CELL),
                                row.get('per_cas_fa', EMPTY_CELL),
                                zone.name
                            ]
Example #9
0
 def get_row_data(self, row):
     if 'user_data' in row['_source'] and 'awc' in row['_source']['user_data']:
         sql_data = OpmHealthStatusSqlData(DOMAIN, row['_id'], self.datespan)
         if sql_data.data:
             formatter = DataFormatter(DictDataFormat(sql_data.columns, no_value=format_percent(0, 0)))
             data = dict(formatter.format(sql_data.data, keys=sql_data.keys, group_by=sql_data.group_by))
             data[row['_id']].update({'awc': row['_source']['user_data']['awc']})
             return HealthStatus(**data[row['_id']])
         else:
             model = HealthStatus()
             model.awc = row['_source']['user_data']['awc']
             return model
     else:
         raise InvalidRow
Example #10
0
 def get_user_ids(self):
     # make an empty copy of this to just get the ids out
     header_sql_data = McSqlData(
         [], # blank sections
         self.domain,
         self.datespan,
         self.fixture_type,
         self.fixture_item
     )
     user_formatter = DataFormatter(TableDataFormat([self.user_column], no_value=NO_VALUE))
     results = list(user_formatter.format(header_sql_data.data,
                                          keys=header_sql_data.keys,
                                          group_by=header_sql_data.group_by))
     return [r[0] for r in results]
Example #11
0
    def rows(self):
        formatter = DataFormatter(self.format_class(self.columns, self.report.get_users()))
        column_headers = [column.data_tables_column.html for column in self.columns]
        raw_data = list(formatter.format(self.data, keys=self.keys, group_by=self.group_by))

        # manually bolt on an in-memory calculation of the total to the headers and each row
        if self.show_total:
            def _total(row):
                return sum([val for val in row if val != NO_VALUE]) or NO_VALUE

            column_headers += [_(self.total_slug)]
            for row in raw_data:
                row.append(_total(row))

        return transpose(column_headers, raw_data)
Example #12
0
    def get_data(self, start=None, limit=None):
        ret = self._provider.get_data(start, limit)

        # A way to bypass these transforms should be implemented.
        # We can format data more efficiently in custom code if necessary
        formatter = DataFormatter(DictDataFormat(self.columns, no_value=None))
        formatted_data = list(formatter.format(ret, group_by=self.group_by).values())

        for report_column in self.top_level_db_columns:
            report_column.format_data(formatted_data)

        for computed_column in self.top_level_computed_columns:
            for row in formatted_data:
                row[computed_column.column_id] = computed_column.wrapped_expression(row)

        return formatted_data
Example #13
0
    def rows(self):

        def cell_format(data):
            percent = 0
            if isinstance(data, dict):
                percent = 100
            return {
                'sort_key': percent,
                'html': "%.2f%%" % percent
            }

        users = CommCareUser.by_domain(self.domain)
        users_dict = {}
        for user in users:
            if user.location_id not in users_dict:
                users_dict.update({user.location_id: [user.get_id]})
            else:
                users_dict[user.location_id].append(user.get_id)

        formatter = DataFormatter(DictDataFormat(self.columns, no_value=self.no_value))
        data = formatter.format(self.data, keys=self.keys, group_by=self.group_by)
        selected_location = location_filter(self.request, location_filter_selected=True)
        if selected_location:
            locations = SQLLocation.objects.get(
                location_id=selected_location
            ).get_descendants(include_self=True).filter(location_type__code='centre-de-sante').order_by('name')
        else:
            locations = SQLLocation.objects.filter(
                domain=self.domain,
                location_type__code='centre-de-sante',
                is_archived=False
            ).order_by('name')
        user_locations = users_locations()
        for site in locations:
            loc_data = data.get(site.location_id, {})
            if site.location_id in user_locations:
                yield [
                    site.name,
                    cell_format(loc_data.get('completude', 0)),
                    cell_format(loc_data.get('promptitude', 0)),
                ]
Example #14
0
    def rows(self):
        formatter = DataFormatter(
            self.format_class(self.columns, self.report.get_users()))
        column_headers = [
            column.data_tables_column.html for column in self.columns
        ]
        raw_data = list(
            formatter.format(self.data, keys=self.keys,
                             group_by=self.group_by))

        # manually bolt on an in-memory calculation of the total to the headers and each row
        if self.show_total:

            def _total(row):
                return sum([val for val in row if val != NO_VALUE]) or NO_VALUE

            column_headers += [_(self.total_slug)]
            for row in raw_data:
                row.append(_total(row))

        return transpose(column_headers, raw_data)
Example #15
0
    def rows(self):
        data = self.model.data
        ppss = sorted(list(set(zip(*data.keys())[1]))) if data.keys() else []

        rows = []

        formatter = DataFormatter(DictDataFormat(self.model.columns, no_value=self.no_value))
        data = dict(formatter.format(self.model.data, keys=self.model.keys, group_by=self.model.group_by))
        reversed_map = dict(zip(self.PRODUCT_NAMES.values(), self.PRODUCT_NAMES.keys()))
        for pps in ppss:
            row = [pps]
            for group in self.groups:
                if (group, pps) in data:
                    product = data[(group, pps)]
                    row.extend([product[p] for p in self.model.col_names])
                elif (self._safe_get(reversed_map, group), pps) in data:
                    product = data[(reversed_map[group], pps)]
                    row.extend([product[p] for p in self.model.col_names])
                else:
                    row.extend([self.no_value for p in self.model.col_names])
            rows.append(row)
        return rows
Example #16
0
    def get_report_context(self, data_provider):
        headers = DataTablesHeader(*[c.data_tables_column for c in data_provider.columns])

        if self.needs_filters:
            rows = []
            charts = []
            total_row = []
        else:
            formatter = DataFormatter(TableDataFormat(data_provider.columns, no_value=self.no_value))
            rows = list(formatter.format(data_provider.data, keys=data_provider.keys, group_by=data_provider.group_by))
            charts = list(self.get_chart(
                rows,
                data_provider.columns,
                x_label=data_provider.chart_x_label,
                y_label=data_provider.chart_y_label,
                has_total_column=data_provider.has_total_column
            ))

            total_row = list(calculate_total_row(rows))
            if total_row:
                total_row[0] = 'Total'

        context = dict(
            report_table=dict(
                title=data_provider.title,
                headers=headers,
                rows=rows,
                total_row=total_row,
                default_rows=self.default_rows,
                datatables=True
            ),
            charts=charts,
            chart_span=12
        )

        return context
Example #17
0
 def rows(self):
     formatter = DataFormatter(TableDataFormat(self.columns, no_value=NO_VALUE))
     raw_data = list(formatter.format(self.data, keys=self.keys, group_by=self.group_by))
     ret = transpose(self.columns, raw_data)
     return ret
Example #18
0
 def rows(self):
     formatter = DataFormatter(TableDataFormat(self.columns, no_value=self.no_value))
     return list(formatter.format(self.data, keys=self.keys, group_by=self.group_by))
Example #19
0
 def _raw_rows(self):
     formatter = DataFormatter(TableDataFormat(self.sqldata.columns, no_value=NO_VALUE))
     return list(formatter.format(self.sqldata.data, keys=self.sqldata.keys, group_by=self.sqldata.group_by))
 def rows(self):
     formatter = DataFormatter(TableDataFormat(self.data_provider.columns, no_value=self.data_provider.no_value))
     return formatter.format(self.data_provider.data, keys=self.data_provider.keys, group_by=self.data_provider.group_by)
Example #21
0
 def records(self):
     formatter = DataFormatter(DictDataFormat(self.columns, no_value=self.no_value))
     return OrderedDict(sorted(formatter.format(self.data, keys=self.keys, group_by=self.group_by).items()))
Example #22
0
 def rows(self):
     formatter = DataFormatter(
         self.format_class(self.columns, self.report.get_users()))
     raw_data = formatter.format(
         self.data, keys=self.keys, group_by=self.group_by)
     return transpose(self.columns, list(raw_data))