Example #1
0
def get_default_column_data(domain, location_types):
    data = {
        'headers': {},
        'values': {}
    }

    if Domain.get_by_name(domain).commtrack_settings.individual_consumption_defaults:
        products = Product.by_domain(domain)

        supply_point_map = SupplyPointCase.get_location_map_by_domain(domain)

        consumption_dict = build_consumption_dict(domain)

        if not consumption_dict:
            return data

        for loc_type in location_types:
            loc = get_loc_config(domain)[loc_type]
            if not loc.administrative:
                data['headers'][loc_type] = [
                    'default_' +
                    p.code for p in products
                ]

                locations = Location.filter_by_type(domain, loc_type)
                for loc in locations:
                    if loc._id in supply_point_map:
                        sp_id = supply_point_map[loc._id]
                    else:
                        # this only happens if the supply point case did
                        # not already exist
                        sp_id = SupplyPointCase.get_or_create_by_location(loc)._id

                    data['values'][loc._id] = [
                        get_loaded_default_monthly_consumption(
                            consumption_dict,
                            domain,
                            p._id,
                            loc_type,
                            sp_id
                        ) or '' for p in products
                    ]
            else:
                data['headers'][loc_type] = []
    return data
Example #2
0
 def get_consumption(self, loc):
     if (
         not self.include_consumption or
         loc.location_type in self.administrative_types or
         not self.consumption_dict
     ):
         return {}
     if loc._id in self.supply_point_map:
         sp_id = self.supply_point_map[loc._id]
     else:
         # this only happens if the supply point case did
         # not already exist
         sp_id = SupplyPointCase.get_or_create_by_location(loc)._id
     return {
         p.code: get_loaded_default_monthly_consumption(
             self.consumption_dict,
             self.domain,
             p._id,
             loc.location_type,
             sp_id
         ) or ''
         for p in self.products
     }