Improve the details in products that we show

This commit is contained in:
PCoder 2019-09-15 13:05:14 +05:30
parent 9b8a9c563a
commit 6c2183a6c8

View file

@ -53,13 +53,41 @@ def get_order_id():
client.put("/v1/last_order_id", str(order_id))
return "OR-{}".format(order_id)
def get_human_readable_period(recurring_period_secs):
if recurring_period_secs <= 24 * 60 * 60:
return "day"
elif recurring_period_secs <= 30 * 24 * 60 * 60:
return "month"
elif recurring_period_secs <= 365 * 30 * 24 * 60 * 60:
return "year"
def get_pricing(price_in_chf_cents, product_type, recurring_period_secs):
if product_type == "recurring":
return "CHF {}/ {}".format(
price_in_chf_cents/100,
get_human_readable_period(recurring_period_secs)
)
elif product_type == "one-time":
return "CHF {}".format(price_in_chf_cents/100)
class ListProducts(Resource):
@staticmethod
def get():
products = client.get_prefix("/v1/products/", value_in_json=False)
prod_dict = {}
for p in products:
prod_dict[p.key] = json.loads(p.value)
p_json = json.loads(p.value)
product_id = p.key[p.key.rindex("/")+1:]
actual_product = {
"name": p_json["name"],
"description": p_json["product_description"],
"product_id": product_id,
"pricing": get_pricing(p_json["price"], p_json["type"],
p_json["recurring_duration"]),
"minumum_subscription_duration": "1 month"
}
prod_dict[product_id] = actual_product
logging.debug("Products = {}".format(prod_dict))
return prod_dict, 200