Пример #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, 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
Пример #2
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
Пример #3
0
 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)
Пример #4
0
 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
Пример #5
0
 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
Пример #6
0
    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)
Пример #7
0
    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
Пример #8
0
    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'))
Пример #9
0
    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'))
Пример #10
0
 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
             )
         )
Пример #11
0
    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
Пример #12
0
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
Пример #13
0
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
Пример #14
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
Пример #15
0
    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
        )
Пример #16
0
    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
Пример #17
0
    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)
Пример #18
0
    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
        )
Пример #19
0
    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)
Пример #20
0
 def testGetNoDefault(self):
     self.assertEqual(None, get_default_consumption(domain, 'whatever', 'goes', 'here'))
Пример #21
0
 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'))
Пример #22
0
    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))
Пример #23
0
    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'))
Пример #24
0
    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'))
Пример #25
0
 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'))
Пример #26
0
 def testGetNoDefault(self):
     self.assertEqual(None, get_default_consumption(domain, 'whatever', 'goes', 'here'))
Пример #27
0
    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))