Merge pull request #663 from pcoder/bugfix/plan-with-price
Use price in stripe plan name too
This commit is contained in:
		
				commit
				
					
						c6612153e8
					
				
			
		
					 5 changed files with 56 additions and 28 deletions
				
			
		| 
						 | 
					@ -105,7 +105,8 @@ class CeleryTaskTestCase(TestCase):
 | 
				
			||||||
                                            disk_size=disk_size)
 | 
					                                            disk_size=disk_size)
 | 
				
			||||||
        plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu,
 | 
					        plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu,
 | 
				
			||||||
                                                     memory=memory,
 | 
					                                                     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,
 | 
					        stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu,
 | 
				
			||||||
                                                        ram=memory,
 | 
					                                                        ram=memory,
 | 
				
			||||||
                                                        ssd=disk_size,
 | 
					                                                        ssd=disk_size,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -508,14 +508,20 @@ class OrderConfirmationView(DetailView):
 | 
				
			||||||
        memory = specs.get('memory')
 | 
					        memory = specs.get('memory')
 | 
				
			||||||
        disk_size = specs.get('disk_size')
 | 
					        disk_size = specs.get('disk_size')
 | 
				
			||||||
        amount_to_be_charged = specs.get('total_price')
 | 
					        amount_to_be_charged = specs.get('total_price')
 | 
				
			||||||
        plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu,
 | 
					        plan_name = StripeUtils.get_stripe_plan_name(
 | 
				
			||||||
                                                     memory=memory,
 | 
					            cpu=cpu,
 | 
				
			||||||
                                                     disk_size=disk_size)
 | 
					            memory=memory,
 | 
				
			||||||
        stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu,
 | 
					            disk_size=disk_size,
 | 
				
			||||||
                                                        ram=memory,
 | 
					            price=amount_to_be_charged
 | 
				
			||||||
                                                        ssd=disk_size,
 | 
					        )
 | 
				
			||||||
                                                        version=1,
 | 
					        stripe_plan_id = StripeUtils.get_stripe_plan_id(
 | 
				
			||||||
                                                        app='dcl')
 | 
					            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(
 | 
					        stripe_plan = stripe_utils.get_or_create_stripe_plan(
 | 
				
			||||||
            amount=amount_to_be_charged,
 | 
					            amount=amount_to_be_charged,
 | 
				
			||||||
            name=plan_name,
 | 
					            name=plan_name,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1032,14 +1032,20 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView):
 | 
				
			||||||
        memory = specs.get('memory')
 | 
					        memory = specs.get('memory')
 | 
				
			||||||
        disk_size = specs.get('disk_size')
 | 
					        disk_size = specs.get('disk_size')
 | 
				
			||||||
        amount_to_be_charged = specs.get('total_price')
 | 
					        amount_to_be_charged = specs.get('total_price')
 | 
				
			||||||
        plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu,
 | 
					        plan_name = StripeUtils.get_stripe_plan_name(
 | 
				
			||||||
                                                     memory=memory,
 | 
					            cpu=cpu,
 | 
				
			||||||
                                                     disk_size=disk_size)
 | 
					            memory=memory,
 | 
				
			||||||
        stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu,
 | 
					            disk_size=disk_size,
 | 
				
			||||||
                                                        ram=memory,
 | 
					            price=amount_to_be_charged
 | 
				
			||||||
                                                        ssd=disk_size,
 | 
					        )
 | 
				
			||||||
                                                        version=1,
 | 
					        stripe_plan_id = StripeUtils.get_stripe_plan_id(
 | 
				
			||||||
                                                        app='dcl')
 | 
					            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(
 | 
					        stripe_plan = stripe_utils.get_or_create_stripe_plan(
 | 
				
			||||||
            amount=amount_to_be_charged,
 | 
					            amount=amount_to_be_charged,
 | 
				
			||||||
            name=plan_name,
 | 
					            name=plan_name,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
import decimal
 | 
					import decimal
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import subprocess
 | 
					import subprocess
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from oca.pool import WrongIdError
 | 
					from oca.pool import WrongIdError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from datacenterlight.models import VMPricing
 | 
					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))
 | 
					             (decimal.Decimal(hdd_size) * pricing.hdd_unit_price))
 | 
				
			||||||
    cents = decimal.Decimal('.01')
 | 
					    cents = decimal.Decimal('.01')
 | 
				
			||||||
    price = price.quantize(cents, decimal.ROUND_HALF_UP)
 | 
					    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,
 | 
					def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0,
 | 
				
			||||||
| 
						 | 
					@ -126,9 +127,10 @@ def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0,
 | 
				
			||||||
    vat = vat.quantize(cents, decimal.ROUND_HALF_UP)
 | 
					    vat = vat.quantize(cents, decimal.ROUND_HALF_UP)
 | 
				
			||||||
    discount = {
 | 
					    discount = {
 | 
				
			||||||
        'name': pricing.discount_name,
 | 
					        'name': pricing.discount_name,
 | 
				
			||||||
        'amount': float(pricing.discount_amount),
 | 
					        'amount': round(float(pricing.discount_amount),2)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    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):
 | 
					def ping_ok(host_ipv6):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -291,7 +291,8 @@ class StripeUtils(object):
 | 
				
			||||||
        return charge
 | 
					        return charge
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @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
 | 
					        Returns the Stripe plan id string of the form
 | 
				
			||||||
        `dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters
 | 
					        `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 version: The version of the Stripe plans
 | 
				
			||||||
        :param app: The application to which the stripe plan belongs
 | 
					        :param app: The application to which the stripe plan belongs
 | 
				
			||||||
        to. By default it is 'dcl'
 | 
					        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`
 | 
					        :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,
 | 
					        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(
 | 
					        stripe_plan_id_string = '{app}-v{version}-{plan}'.format(
 | 
				
			||||||
            app=app,
 | 
					            app=app,
 | 
				
			||||||
            version=version,
 | 
					            version=version,
 | 
				
			||||||
            plan=dcl_plan_string)
 | 
					            plan=dcl_plan_string
 | 
				
			||||||
        return stripe_plan_id_string
 | 
					        )
 | 
				
			||||||
 | 
					        if price is not None:
 | 
				
			||||||
 | 
					            stripe_plan_id_string_with_price = '{}-{}chf'.format(
 | 
				
			||||||
 | 
					                stripe_plan_id_string,
 | 
				
			||||||
 | 
					                round(price, 2)
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            return stripe_plan_id_string_with_price
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            return stripe_plan_id_string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @staticmethod
 | 
					    @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
 | 
					        Returns the Stripe plan name
 | 
				
			||||||
        :return:
 | 
					        :return:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format(
 | 
					        return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD, " \
 | 
				
			||||||
            cpu=cpu,
 | 
					               "{price} CHF".format(
 | 
				
			||||||
            memory=memory,
 | 
					                    cpu=cpu,
 | 
				
			||||||
            disk_size=disk_size)
 | 
					                    memory=memory,
 | 
				
			||||||
 | 
					                    disk_size=disk_size,
 | 
				
			||||||
 | 
					                    price=round(price, 2)
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @handleStripeError
 | 
					    @handleStripeError
 | 
				
			||||||
    def set_subscription_meta_data(self, subscription_id, meta_data):
 | 
					    def set_subscription_meta_data(self, subscription_id, meta_data):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue