Exemplo n.º 1
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', EMPTY_CELL)),
                    cell_format(loc_data.get('promptitude', EMPTY_CELL)),
                ]
Exemplo n.º 2
0
    def rows(self):
        data = self.model.data
        localizations = sorted(list(set(zip(
            *data.keys())[1]))) if data.keys() else []
        rows = []

        formatter = DataFormatter(
            DictDataFormat(self.model.columns, no_value=self.no_value))
        if isinstance(self.data_source, NombreData) or isinstance(
                self.data_source, 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[3], k[2], k[1])
                else:
                    helper_tuple = (k[2], k[1])
                if helper_tuple in result:
                    r = result[helper_tuple]
                    if r['date'] <= v['date']:
                        result[helper_tuple] = v
                else:
                    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, localization) in result_sum:
                                r = result_sum[(group, localization)]
                                cols = self.data_source.sum_cols
                                for col in cols:
                                    r[col] += result.get(
                                        (group, 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, localization)] = result.get(
                                    (group, 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, localization) in data:
                    product = data[(group, localization)]
                    row.extend([product[p] for p in self.model.col_names])
                elif (self._safe_get(reversed_map,
                                     group), localization) in data:
                    product = data[(reversed_map[group], 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
Exemplo n.º 3
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()))