From 8a301004888ca11dee3ef502aa5073d2e97aadd9 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 5 Sep 2018 23:26:51 +0200 Subject: [PATCH 1/6] Use price in stripe plan name too --- datacenterlight/tests.py | 3 ++- datacenterlight/views.py | 22 ++++++++++++++-------- hosting/views.py | 22 ++++++++++++++-------- utils/stripe_utils.py | 29 +++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/datacenterlight/tests.py b/datacenterlight/tests.py index c755cc6f..ca1bb930 100644 --- a/datacenterlight/tests.py +++ b/datacenterlight/tests.py @@ -105,7 +105,8 @@ class CeleryTaskTestCase(TestCase): disk_size=disk_size) plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu, memory=memory, - disk_size=disk_size) + disk_size=disk_size, + price=amount_to_be_charged) stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, ram=memory, ssd=disk_size, diff --git a/datacenterlight/views.py b/datacenterlight/views.py index bf87d9b9..be4e5700 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -508,14 +508,20 @@ class OrderConfirmationView(DetailView): memory = specs.get('memory') disk_size = specs.get('disk_size') amount_to_be_charged = specs.get('total_price') - plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu, - memory=memory, - disk_size=disk_size) - stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, - ram=memory, - ssd=disk_size, - version=1, - app='dcl') + plan_name = StripeUtils.get_stripe_plan_name( + cpu=cpu, + memory=memory, + disk_size=disk_size, + price=amount_to_be_charged + ) + stripe_plan_id = StripeUtils.get_stripe_plan_id( + cpu=cpu, + ram=memory, + ssd=disk_size, + version=1, + app='dcl', + price=amount_to_be_charged + ) stripe_plan = stripe_utils.get_or_create_stripe_plan( amount=amount_to_be_charged, name=plan_name, diff --git a/hosting/views.py b/hosting/views.py index 6af1885b..5e006fee 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1032,14 +1032,20 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView): memory = specs.get('memory') disk_size = specs.get('disk_size') amount_to_be_charged = specs.get('total_price') - plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu, - memory=memory, - disk_size=disk_size) - stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, - ram=memory, - ssd=disk_size, - version=1, - app='dcl') + plan_name = StripeUtils.get_stripe_plan_name( + cpu=cpu, + memory=memory, + disk_size=disk_size, + price=amount_to_be_charged + ) + stripe_plan_id = StripeUtils.get_stripe_plan_id( + cpu=cpu, + ram=memory, + ssd=disk_size, + version=1, + app='dcl', + price=amount_to_be_charged + ) stripe_plan = stripe_utils.get_or_create_stripe_plan( amount=amount_to_be_charged, name=plan_name, diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 2045df8e..0c3b112c 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -291,7 +291,8 @@ class StripeUtils(object): return charge @staticmethod - def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None): + def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None, + price=None): """ Returns the Stripe plan id string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters @@ -303,6 +304,7 @@ class StripeUtils(object): :param version: The version of the Stripe plans :param app: The application to which the stripe plan belongs to. By default it is 'dcl' + :param price: The price for this plan :return: A string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb` """ dcl_plan_string = 'cpu-{cpu}-ram-{ram}gb-ssd-{ssd}gb'.format(cpu=cpu, @@ -314,19 +316,30 @@ class StripeUtils(object): stripe_plan_id_string = '{app}-v{version}-{plan}'.format( app=app, version=version, - plan=dcl_plan_string) - return stripe_plan_id_string + plan=dcl_plan_string + ) + if price is not None: + stripe_plan_id_string_with_price = '{}-{}chf'.format( + stripe_plan_id_string, + price + ) + return stripe_plan_id_string_with_price + else: + return stripe_plan_id_string @staticmethod - def get_stripe_plan_name(cpu, memory, disk_size): + def get_stripe_plan_name(cpu, memory, disk_size, price): """ Returns the Stripe plan name :return: """ - return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( - cpu=cpu, - memory=memory, - disk_size=disk_size) + return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD " \ + "@ {price} CHF".format( + cpu=cpu, + memory=memory, + disk_size=disk_size, + price=price + ) @handleStripeError def set_subscription_meta_data(self, subscription_id, meta_data): From 642153345c34269853ae3e558c33d8f222a8c5c8 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 6 Sep 2018 00:18:04 +0200 Subject: [PATCH 2/6] Round price, vat and discount to 2 decimal places --- utils/hosting_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index 1859a82c..ecbeb098 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -1,6 +1,7 @@ import decimal import logging import subprocess + from oca.pool import WrongIdError from datacenterlight.models import VMPricing @@ -80,7 +81,7 @@ def get_vm_price(cpu, memory, disk_size, hdd_size=0, pricing_name='default'): (decimal.Decimal(hdd_size) * pricing.hdd_unit_price)) cents = decimal.Decimal('.01') price = price.quantize(cents, decimal.ROUND_HALF_UP) - return float(price) + return round(float(price), 2) def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0, @@ -128,7 +129,8 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0, 'name': pricing.discount_name, 'amount': float(pricing.discount_amount), } - return float(price), float(vat), float(vat_percent), discount + return (round(float(price), 2), round(float(vat), 2), + round(float(vat_percent)), discount) def ping_ok(host_ipv6): From d8a532e7b082b0fec0999fc35f5bd889ebeaa9dc Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 6 Sep 2018 00:26:08 +0200 Subject: [PATCH 3/6] Round discount to 2 decimal points also --- utils/hosting_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index ecbeb098..2918deb3 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -127,7 +127,7 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0, vat = vat.quantize(cents, decimal.ROUND_HALF_UP) discount = { 'name': pricing.discount_name, - 'amount': float(pricing.discount_amount), + 'amount': round(float(pricing.discount_amount),2) } return (round(float(price), 2), round(float(vat), 2), round(float(vat_percent)), discount) From fdffe2389b2f65356bb4195984c5c97e4116e581 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 6 Sep 2018 00:31:17 +0200 Subject: [PATCH 4/6] Round price --- utils/stripe_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 0c3b112c..b4410746 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -321,7 +321,7 @@ class StripeUtils(object): if price is not None: stripe_plan_id_string_with_price = '{}-{}chf'.format( stripe_plan_id_string, - price + round(price, 2) ) return stripe_plan_id_string_with_price else: @@ -338,7 +338,7 @@ class StripeUtils(object): cpu=cpu, memory=memory, disk_size=disk_size, - price=price + price=round(price, 2) ) @handleStripeError From b03cb073c2d3dd3f8e21e67f2dbcf41bbff0ce9f Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 6 Sep 2018 00:44:47 +0200 Subject: [PATCH 5/6] Replace @ with a comma --- utils/stripe_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index b4410746..7acf7003 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -334,7 +334,7 @@ class StripeUtils(object): :return: """ return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD " \ - "@ {price} CHF".format( + ", {price} CHF".format( cpu=cpu, memory=memory, disk_size=disk_size, From fc8f9993af79328cca1036ed19c48761813c86bb Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 6 Sep 2018 00:52:55 +0200 Subject: [PATCH 6/6] Remove whitespace in Stripe plan --- utils/stripe_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 7acf7003..a3224a0e 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -333,8 +333,8 @@ class StripeUtils(object): Returns the Stripe plan name :return: """ - return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD " \ - ", {price} CHF".format( + return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD, " \ + "{price} CHF".format( cpu=cpu, memory=memory, disk_size=disk_size,