Ejemplo n.º 1
0
def _update_or_create_default(domain, amount, default, type, **kwargs):
    if default and default.default_consumption == amount:
        return default
    elif default:
        default.default_consumption = amount
        default.save()
        return default
    else:
        default = DefaultConsumption(domain=domain, default_consumption=amount, type=type, **kwargs)
        default.save()
        return default
Ejemplo n.º 2
0
def get_default_monthly_consumption(domain, product_id, location_type, case_id):
    """
    Return the most specific consumption value for the passed
    parameters.
    """

    keys = [
        [domain, product_id, {}, case_id],
        [domain, product_id, location_type, None],
        [domain, product_id, None, None],
        [domain, None, None, None],
    ]

    results = cache_core.cached_view(
        DefaultConsumption.get_db(),
        'consumption/consumption_index',
        keys=keys,
        reduce=False,
        limit=1,
    )
    results = results[0] if results else None
    if results and results['value']:
        return Decimal(results['value'])
    else:
        return None
Ejemplo n.º 3
0
def get_default_monthly_consumption(domain, product_id, location_type, case_id):
    """
    Return the most specific consumption value for the passed
    parameters.
    """

    keys = [
        [domain, product_id, {}, case_id],
        [domain, product_id, location_type, None],
        [domain, product_id, None, None],
        [domain, None, None, None],
    ]

    results = cache_core.cached_view(
        DefaultConsumption.get_db(),
        'consumption/consumption_index',
        keys=keys,
        reduce=False,
        limit=1,
    )
    results = results[0] if results else None
    if results and results['value']:
        return Decimal(results['value'])
    else:
        return None
Ejemplo n.º 4
0
def set_default_consumption_for_product(domain, product_id, amount):
    default = DefaultConsumption.get_product_default(domain, product_id)
    return _update_or_create_default(domain,
                                     amount,
                                     default,
                                     TYPE_PRODUCT,
                                     product_id=product_id)
Ejemplo n.º 5
0
def set_default_consumption_for_supply_point(domain, product_id,
                                             supply_point_id, amount):
    default = DefaultConsumption.get_supply_point_default(
        domain, product_id, supply_point_id)
    return _update_or_create_default(domain,
                                     amount,
                                     default,
                                     TYPE_SUPPLY_POINT,
                                     product_id=product_id,
                                     supply_point_id=supply_point_id)
Ejemplo n.º 6
0
def get_domain_monthly_consumption_data(domain):
    """
    Get all default consumption rows for this domain.
    """
    results = cache_core.cached_view(
        DefaultConsumption.get_db(),
        'consumption/consumption_index',
        startkey=[domain],
        endkey=[domain, {}],
        reduce=False,
    )
    return results
Ejemplo n.º 7
0
def get_domain_monthly_consumption_data(domain):
    """
    Get all default consumption rows for this domain.
    """
    results = cache_core.cached_view(
        DefaultConsumption.get_db(),
        'consumption/consumption_index',
        startkey=[domain],
        endkey=[domain, {}],
        reduce=False,
    )
    return results
Ejemplo n.º 8
0
def get_default_consumption(domain, product_id, location_type, case_id):
    keys = [
        [domain, product_id, {}, case_id],
        [domain, product_id, location_type, None],
        [domain, product_id, None, None],
        [domain, None, None, None],
    ]

    results = cache_core.cached_view(DefaultConsumption.get_db(), 'consumption/consumption_index',
        keys=keys, reduce=False, limit=1, descending=True,
    )
    results = results[0] if results else None
    if results and results['value']:
        return Decimal(float(results['value']) / DAYS_IN_MONTH)
    else:
        return None
Ejemplo n.º 9
0
def get_default_consumption(domain, product_id, location_type, case_id):
    keys = [
        [domain, product_id, {}, case_id],
        [domain, product_id, location_type, None],
        [domain, product_id, None, None],
        [domain, None, None, None],
    ]
    results = DefaultConsumption.get_db().view(
        'consumption/consumption_index',
        keys=keys, reduce=False, limit=1, descending=True,
    )
    results = results.one()
    if results and results['value']:
        return Decimal(results['value'])
    else:
        return None
Ejemplo n.º 10
0
def get_default_consumption(domain, product_id, location_type, case_id):
    keys = [
        [domain, product_id, {}, case_id],
        [domain, product_id, location_type, None],
        [domain, product_id, None, None],
        [domain, None, None, None],
    ]

    results = cache_core.cached_view(
        DefaultConsumption.get_db(),
        'consumption/consumption_index',
        keys=keys,
        reduce=False,
        limit=1,
        descending=True,
    )
    results = results[0] if results else None
    if results and results['value']:
        return Decimal(results['value'])
    else:
        return None
Ejemplo n.º 11
0
def set_default_consumption_for_supply_point(domain, product_id, supply_point_id, amount):
    default = DefaultConsumption.get_supply_point_default(domain, product_id, supply_point_id)
    return _update_or_create_default(domain, amount, default, TYPE_SUPPLY_POINT,
                                     product_id=product_id, supply_point_id=supply_point_id)
Ejemplo n.º 12
0
def set_default_consumption_for_product(domain, product_id, amount):
    default = DefaultConsumption.get_product_default(domain, product_id)
    return _update_or_create_default(domain, amount, default, TYPE_PRODUCT, product_id=product_id)
Ejemplo n.º 13
0
def set_default_monthly_consumption_for_domain(domain, amount):
    default = DefaultConsumption.get_domain_default(domain)
    return _update_or_create_default(domain, amount, default, TYPE_DOMAIN)
Ejemplo n.º 14
0
def set_default_consumption_for_domain(domain, amount):
    default = DefaultConsumption.get_domain_default(domain)
    return _update_or_create_default(domain, amount, default, TYPE_DOMAIN)
Ejemplo n.º 15
0
 def tearDown(self):
     default = DefaultConsumption.get_domain_default(self.domain)
     if default:
         default.delete()
     super(StockStateConsumptionTest, self).tearDown()