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, wrap=False) 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: data['values'][loc._id] = [ get_default_consumption( domain, p['_id'], loc_type, loc._id ) or '' for p in products ] else: data['headers'][loc_type] = [] return data
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
def _default_consumption_function(case_id, product_id): # note: for now as an optimization hack, per-supply point type is not supported # unless explicitly configured, because it will require looking up the case facility_type = None if self.consumption_config.use_supply_point_type_default_consumption: try: supply_point = SupplyPointCase.get(case_id) facility_type = supply_point.location.location_type except ResourceNotFound: pass return get_default_consumption(self.domain, product_id, facility_type, case_id)
def consumption(self): consumptions = [] for product in Product.by_domain(self.domain): consumption = get_default_consumption( self.domain, product._id, self.location.location_type, self.supply_point._id if self.supply_point else None, ) if consumption: consumptions.append((product.name, consumption)) return consumptions
def test_should_import_consumption(self): existing = make_loc('existingloc', type='state') sp = make_supply_point(self.loc.domain, existing) data = {'id': existing._id, 'name': 'existingloc', 'default_pp': 77} import_location(self.domain.name, 'state', data) self.assertEqual( get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp._id, ), 77)
def no_changes_needed(self, existing, form_data, consumption): if not existing: return False for key, val in form_data.iteritems(): if getattr(existing, key, None) != val: return False for product_code, val in consumption: product = self.get_product(product_code) if get_default_consumption(self.domain, product._id, existing.location_type, existing._id) != val: return False return True
def testGetForProduct(self): _create_product_consumption(5) self.assertEqual(5, get_default_consumption(domain, product_id, 'doesnt', 'matter')) self.assertEqual(None, get_default_consumption(domain, 'wrong', 'doesnt', 'matter')) self.assertEqual(None, get_default_consumption('wrong', product_id, 'doesnt', 'matter')) _create_domain_consumption(3) self.assertEqual(5, get_default_consumption(domain, product_id, 'doesnt', 'matter')) self.assertEqual(3, get_default_consumption(domain, 'wrong', 'doesnt', 'matter')) self.assertEqual(None, get_default_consumption('wrong', product_id, 'doesnt', 'matter'))
def __init__(self, domain, *args, **kwargs): self.domain = domain super(ConsumptionForm, self).__init__(*args, **kwargs) products = Product.by_domain(domain) for p in products: field_name = 'default_%s' % p.code display = _('Default %(product_name)s') % {'product_name': p.name} self.fields[field_name] = forms.DecimalField( label=display, required=False, initial=get_default_consumption( self.domain, p._id, None, None ) )
def no_changes_needed(self, existing, form_data, consumption): if not existing: return False for key, val in form_data.iteritems(): if getattr(existing, key, None) != val: return False for product_code, val in consumption: product = self.get_product(product_code) if get_default_consumption( self.domain, product._id, existing.location_type, existing._id ) != val: return False return True
def no_changes_needed(domain, existing, properties, form_data, consumption, sp=None): if not existing: return False for prop, val in properties.iteritems(): if getattr(existing, prop[1], None) != val: return False for key, val in form_data.iteritems(): if getattr(existing, key, None) != val: return False for product_code, val in consumption: product = Product.get_by_code(domain, product_code) if get_default_consumption( domain, product._id, existing.location_type, existing._id ) != val: return False return True
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
def test_should_import_consumption(self): existing = make_loc('existingloc', type='state') sp = make_supply_point(self.loc.domain, existing) data = { 'id': existing._id, 'name': 'existingloc', 'default_pp': 77 } import_location(self.domain.name, 'state', data) self.assertEqual( get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp._id, ), 77 / DAYS_IN_MONTH )
def no_changes_needed(self, existing, form_data, consumption): if not existing: return False for prop in ['name', 'coordinates', 'site_code', 'external_id']: if getattr(existing, prop, None) != form_data.get(prop): return False if (form_data.get('location_type') != existing.location_type_name or form_data.get('parent_id') != existing.parent_location_id): return False if get_prefixed(form_data) != existing.metadata: return False for product_code, val in consumption: product = self.get_product(product_code) if get_default_consumption(self.domain, product._id, existing.location_type_name, existing.location_id) != val: return False return True
def test_should_import_consumption(self): existing = make_loc('existingloc', type='state') sp = make_supply_point(self.loc.domain, existing) data = { 'site_code': existing.site_code, 'name': 'existingloc', 'consumption': { 'pp': 77 }, } import_location(self.domain.name, 'state', data) self.assertEqual( float( get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp._id, )), 77 / DAYS_IN_MONTH)
def test_should_import_consumption(self): parent = make_loc('originalparent', type='village') existing = make_loc('existingloc', type='outlet', parent=parent) sp = existing.linked_supply_point() data = { 'site_code': existing.site_code, 'name': 'existingloc', 'parent_site_code': parent.site_code, 'consumption': {'pp': 77}, } import_location(self.domain.name, 'outlet', data) self.assertEqual( float(get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp.case_id, )), 77 / DAYS_IN_MONTH )
def test_should_import_consumption(self): parent = make_loc('originalparent', type='village') existing = make_loc('existingloc', type='outlet', parent=parent) sp = existing.linked_supply_point() data = { 'site_code': existing.site_code, 'name': 'existingloc', 'parent_site_code': parent.site_code, 'consumption': { 'pp': 77 }, } import_location(self.domain.name, 'outlet', data) self.assertEqual( float( get_default_consumption( self.domain.name, Product.get_by_code(self.domain.name, 'pp')._id, 'state', sp.case_id, )), 77 / DAYS_IN_MONTH)
def testGetNoDefault(self): self.assertEqual(None, get_default_consumption(domain, 'whatever', 'goes', 'here'))
def testGetDomainOnly(self): _create_domain_consumption(5) self.assertEqual(5, get_default_consumption(domain, 'whatever', 'goes', 'here')) self.assertEqual(None, get_default_consumption('wrong', 'whatever', 'goes', 'here'))
def testGetForId(self): _create_id_consumption(5) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, supply_point_id)) self.assertEqual(5, get_default_consumption(domain, product_id, 'useless', supply_point_id)) self.assertEqual(None, get_default_consumption(domain, product_id, type_id, 'wrong')) self.assertEqual(None, get_default_consumption(domain, product_id, 'wrong', 'wrong')) self.assertEqual(None, get_default_consumption(domain, 'wrong', type_id, supply_point_id)) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, supply_point_id)) _create_type_consumption(4) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, supply_point_id)) self.assertEqual(5, get_default_consumption(domain, product_id, 'useless', supply_point_id)) self.assertEqual(4, get_default_consumption(domain, product_id, type_id, 'wrong')) self.assertEqual(None, get_default_consumption(domain, product_id, 'wrong', 'wrong')) self.assertEqual(None, get_default_consumption(domain, 'wrong', type_id, supply_point_id)) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, supply_point_id)) _create_product_consumption(3) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, supply_point_id)) self.assertEqual(5, get_default_consumption(domain, product_id, 'useless', supply_point_id)) self.assertEqual(4, get_default_consumption(domain, product_id, type_id, 'wrong')) self.assertEqual(3, get_default_consumption(domain, product_id, 'wrong', 'wrong')) self.assertEqual(None, get_default_consumption(domain, 'wrong', type_id, supply_point_id)) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, supply_point_id)) _create_domain_consumption(2) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, supply_point_id)) self.assertEqual(5, get_default_consumption(domain, product_id, 'useless', supply_point_id)) self.assertEqual(4, get_default_consumption(domain, product_id, type_id, 'wrong')) self.assertEqual(3, get_default_consumption(domain, product_id, 'wrong', 'wrong')) self.assertEqual(2, get_default_consumption(domain, 'wrong', type_id, supply_point_id)) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, supply_point_id))
def testGetForType(self): _create_type_consumption(5) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, 'useless')) self.assertEqual(None, get_default_consumption(domain, product_id, 'wrong', 'useless')) self.assertEqual(None, get_default_consumption(domain, 'wrong', type_id, 'useless')) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, 'useless')) _create_product_consumption(3) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, 'useless')) self.assertEqual(3, get_default_consumption(domain, product_id, 'wrong', 'useless')) self.assertEqual(None, get_default_consumption(domain, 'wrong', type_id, 'useless')) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, 'useless')) _create_domain_consumption(2) self.assertEqual(5, get_default_consumption(domain, product_id, type_id, 'useless')) self.assertEqual(3, get_default_consumption(domain, product_id, 'wrong', 'useless')) self.assertEqual(2, get_default_consumption(domain, 'wrong', type_id, 'useless')) self.assertEqual(None, get_default_consumption('wrong', product_id, type_id, 'useless'))