Exemple #1
0
 def _set_up_supply_point(self, location, ews_location):
     if location.location_type in ['country', 'region', 'district']:
         supply_point_with_stock_data = filter(
             lambda x: x.last_reported and x.active, ews_location.supply_points
         )
         for supply_point in supply_point_with_stock_data:
             created_location = self._create_location_from_supply_point(supply_point, location)
             fake_location = Loc(
                 _id=created_location._id,
                 name=supply_point.name,
                 external_id=str(supply_point.id),
                 domain=self.domain
             )
             SupplyPointCase.get_or_create_by_location(fake_location)
             created_location.save()
     elif ews_location.supply_points:
         active_supply_points = filter(lambda sp: sp.active, ews_location.supply_points)
         if active_supply_points:
             supply_point = active_supply_points[0]
         else:
             supply_point = ews_location.supply_points[0]
         location.name = supply_point.name
         location.site_code = supply_point.code
         location.location_type = supply_point.type
         self._create_supply_point_from_location(supply_point, location)
         location.save()
     else:
         SupplyPointCase.get_or_create_by_location(location)
         location.save()
         location.archive()
Exemple #2
0
 def _create_supply_point_from_location(self, supply_point, location):
     if not SupplyPointCase.get_by_location(location):
         if supply_point.supervised_by:
             location.metadata['supervised_by'] = supply_point.supervised_by
             location.save()
             sql_loc = location.sql_location
             sql_loc.products = SQLProduct.objects.filter(domain=self.domain, code__in=supply_point.products)
             sql_loc.save()
         SupplyPointCase.get_or_create_by_location(Loc(_id=location._id,
                                                       name=supply_point.name,
                                                       external_id=str(supply_point.id),
                                                       domain=self.domain))
Exemple #3
0
def locations_fix(domain):
    locations = SQLLocation.objects.filter(domain=domain, location_type__in=['country', 'region', 'district'])
    for loc in locations:
        sp = Location.get(loc.location_id).linked_supply_point()
        if sp:
            sp.external_id = None
            sp.save()
        else:
            fake_location = Location(
                _id=loc.location_id,
                name=loc.name,
                domain=domain
            )
            SupplyPointCase.get_or_create_by_location(fake_location)
Exemple #4
0
def locations_fix(domain):
    locations = SQLLocation.objects.filter(domain=domain, location_type__in=['country', 'region', 'district'])
    for loc in locations:
        sp = Location.get(loc.location_id).linked_supply_point()
        if sp:
            sp.external_id = None
            sp.save()
        else:
            fake_location = Location(
                _id=loc.location_id,
                name=loc.name,
                domain=domain
            )
            SupplyPointCase.get_or_create_by_location(fake_location)
Exemple #5
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)

        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:
                    sp = SupplyPointCase.get_or_create_by_location(loc)

                    data['values'][loc._id] = [
                        get_default_monthly_consumption(
                            domain,
                            p._id,
                            loc_type,
                            sp._id
                        ) or '' for p in products
                    ]
            else:
                data['headers'][loc_type] = []
    return data
Exemple #6
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)

        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:
                    sp = SupplyPointCase.get_or_create_by_location(loc)

                    data['values'][loc._id] = [
                        get_default_consumption(
                            domain,
                            p._id,
                            loc_type,
                            sp._id
                        ) or '' for p in products
                    ]
            else:
                data['headers'][loc_type] = []
    return data
Exemple #7
0
 def _set_up_supply_point(self, location, ews_location):
     supply_point_with_stock_data = filter(lambda x: x.last_reported, ews_location.supply_points)
     if location.location_type in ['country', 'region', 'district']:
         for supply_point in supply_point_with_stock_data:
             created_location = self._create_location_from_supply_point(supply_point, location)
             fake_location = Loc(
                 _id=created_location._id,
                 name=supply_point.name,
                 external_id=str(supply_point.id),
                 domain=self.domain
             )
             SupplyPointCase.get_or_create_by_location(fake_location)
             created_location.save()
         fake_location = Loc(_id=location._id,
                             name=location.name,
                             domain=self.domain)
         SupplyPointCase.get_or_create_by_location(fake_location)
     elif ews_location.supply_points:
         supply_point = ews_location.supply_points[0]
         location.location_type = supply_point.type
         self._create_supply_point_from_location(supply_point, location)
         location.save()
Exemple #8
0
 def _create_supply_point_from_location(self, supply_point, location):
     if not SupplyPointCase.get_by_location(location):
         if supply_point.supervised_by:
             location.metadata['supervised_by'] = supply_point.supervised_by
             location.save()
             sql_loc = location.sql_location
             sql_loc.products = SQLProduct.objects.filter(
                 domain=self.domain, code__in=supply_point.products)
             sql_loc.save()
         return SupplyPointCase.get_or_create_by_location(
             Loc(_id=location._id,
                 name=supply_point.name,
                 external_id=str(supply_point.id),
                 domain=self.domain))
Exemple #9
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
Exemple #10
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
Exemple #11
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
     }
Exemple #12
0
 def _set_up_supply_point(self, location, ews_location):
     if location.location_type in ['country', 'region', 'district']:
         supply_point_with_stock_data = filter(
             lambda x: x.last_reported and x.active, ews_location.supply_points
         )
         location.save()
         for supply_point in supply_point_with_stock_data:
             created_location = self._create_location_from_supply_point(supply_point, location)
             fake_location = Loc(
                 _id=created_location._id,
                 name=supply_point.name,
                 external_id=str(supply_point.id),
                 domain=self.domain
             )
             case = SupplyPointCase.get_or_create_by_location(fake_location)
             sql_location = created_location.sql_location
             sql_location.supply_point_id = case.get_id
             sql_location.save()
     elif ews_location.supply_points:
         active_supply_points = filter(lambda sp: sp.active, ews_location.supply_points)
         if active_supply_points:
             supply_point = active_supply_points[0]
         else:
             supply_point = ews_location.supply_points[0]
         location.name = supply_point.name
         location.site_code = supply_point.code
         if supply_point.code in TEACHING_HOSPITALS:
             location.location_type = 'Teaching Hospital'
             location.lineage = location.lineage[1:]
         else:
             location.location_type = supply_point.type
         try:
             sql_location = SQLLocation.objects.get(domain=self.domain, site_code=supply_point.code)
             if not sql_location.location_type.administrative:
                 raise MigrationException(
                     u'Site code {} is already used by {}'.format(supply_point.code, sql_location.name)
                 )
             couch_location = sql_location.couch_location
             couch_location.site_code = None
             couch_location.save()
         except SQLLocation.DoesNotExist:
             pass
         location.save()
         supply_point = self._create_supply_point_from_location(supply_point, location)
         if supply_point:
             sql_location = location.sql_location
             sql_location.supply_point_id = supply_point.get_id
             sql_location.save()
     else:
         location.save()
         fake_location = Loc(
             _id=location.get_id,
             name=location.name,
             external_id=None,
             domain=self.domain
         )
         supply_point = SupplyPointCase.get_or_create_by_location(fake_location)
         sql_location = location.sql_location
         sql_location.supply_point_id = supply_point.get_id
         sql_location.save()
         location.archive()
Exemple #13
0
 def _set_up_supply_point(self, location, ews_location):
     if location.location_type in ['country', 'region', 'district']:
         supply_point_with_stock_data = filter(
             lambda x: x.last_reported and x.active,
             ews_location.supply_points)
         location.save()
         for supply_point in supply_point_with_stock_data:
             created_location = self._create_location_from_supply_point(
                 supply_point, location)
             fake_location = Loc(_id=created_location._id,
                                 name=supply_point.name,
                                 external_id=str(supply_point.id),
                                 domain=self.domain)
             case = SupplyPointCase.get_or_create_by_location(fake_location)
             sql_location = created_location.sql_location
             sql_location.supply_point_id = case.get_id
             sql_location.save()
     elif ews_location.supply_points:
         active_supply_points = filter(lambda sp: sp.active,
                                       ews_location.supply_points)
         if active_supply_points:
             supply_point = active_supply_points[0]
         else:
             supply_point = ews_location.supply_points[0]
         location.name = supply_point.name
         location.site_code = supply_point.code
         if supply_point.code in TEACHING_HOSPITALS:
             location.location_type = 'Teaching Hospital'
             location.lineage = location.lineage[1:]
         else:
             location.location_type = supply_point.type
         try:
             sql_location = SQLLocation.objects.get(
                 domain=self.domain, site_code=supply_point.code)
             if not sql_location.location_type.administrative:
                 raise MigrationException(
                     u'Site code {} is already used by {}'.format(
                         supply_point.code, sql_location.name))
             couch_location = sql_location.couch_location
             couch_location.site_code = None
             couch_location.save()
         except SQLLocation.DoesNotExist:
             pass
         location.save()
         supply_point = self._create_supply_point_from_location(
             supply_point, location)
         if supply_point:
             sql_location = location.sql_location
             sql_location.supply_point_id = supply_point.get_id
             sql_location.save()
     else:
         location.save()
         fake_location = Loc(_id=location.get_id,
                             name=location.name,
                             external_id=None,
                             domain=self.domain)
         supply_point = SupplyPointCase.get_or_create_by_location(
             fake_location)
         sql_location = location.sql_location
         sql_location.supply_point_id = supply_point.get_id
         sql_location.save()
         location.archive()
Exemple #14
0
 def _create_supply_point_from_location(self, supply_point, location):
     if not SupplyPointCase.get_by_location(location):
         return SupplyPointCase.get_or_create_by_location(Loc(_id=location.get_id,
                                                          name=supply_point.name,
                                                          external_id=str(supply_point.id),
                                                          domain=self.domain))