django-orchestra/orchestra/apps/orders/pricing.py

37 lines
1012 B
Python
Raw Normal View History

2014-09-08 14:23:06 +00:00
import sys
2014-09-10 16:53:09 +00:00
def best_price(rates, metric, cache={}):
steps = cache.get('steps')
if not steps:
rates = rates.order_by('quantity').order_by('plan')
ix = 0
steps = []
num = rates.count()
while ix < num:
if ix+1 == num or rates[ix].plan != rates[ix+1].plan:
quantity = sys.maxint
else:
quantity = rates[ix+1].quantity - rates[ix].quantity
steps.append({
'quantity': quantity,
'price': rates[ix].price
})
ix += 1
steps.sort(key=lambda s: s['price'])
cache['steps'] = steps
return steps
2014-09-08 14:23:06 +00:00
2014-09-10 16:53:09 +00:00
def match_price(rates, metric, cache={}):
2014-09-08 14:23:06 +00:00
minimal = None
for plan, rates in rates.order_by('-metric').group_by('plan'):
if minimal is None:
minimal = rates[0].price
else:
minimal = min(minimal, rates[0].price)
return [{
2014-09-10 16:53:09 +00:00
'quantity': sys.maxint,
2014-09-08 14:23:06 +00:00
'price': minimal
}]