From b5e488e326c5b462076cdf6735c6f8bf3b5fc9ff Mon Sep 17 00:00:00 2001
From: PCoder <purple.coder@yahoo.co.uk>
Date: Thu, 24 Aug 2017 10:31:57 +0530
Subject: [PATCH] Formatted utils/stripe_utils.py

---
 utils/stripe_utils.py | 81 +++++++++++++++++++++++++++----------------
 1 file changed, 52 insertions(+), 29 deletions(-)

diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py
index 92e1dcd3..f35a6b9c 100644
--- a/utils/stripe_utils.py
+++ b/utils/stripe_utils.py
@@ -30,7 +30,8 @@ def handleStripeError(f):
             response.update({'error': err['message']})
             return response
         except stripe.error.RateLimitError as e:
-            response.update({'error': "Too many requests made to the API too quickly"})
+            response.update(
+                {'error': "Too many requests made to the API too quickly"})
             return response
         except stripe.error.InvalidRequestError as e:
             response.update({'error': "Invalid parameters"})
@@ -104,7 +105,8 @@ class StripeUtils(object):
                 customer = stripe.Customer.retrieve(id)
             except stripe.InvalidRequestError:
                 customer = self.create_customer(token, user.email, user.name)
-                user.stripecustomer.stripe_id = customer.get('response_object').get('id')
+                user.stripecustomer.stripe_id = customer.get(
+                    'response_object').get('id')
                 user.stripecustomer.save()
         return customer
 
@@ -139,21 +141,26 @@ class StripeUtils(object):
     @handleStripeError
     def get_or_create_stripe_plan(self, amount, name, stripe_plan_id):
         """
-        This function checks if a StripePlan with the given stripe_plan_id already exists. If it exists then the
-        function returns this object otherwise it creates a new StripePlan and returns the new object.
+        This function checks if a StripePlan with the given
+        stripe_plan_id already exists. If it exists then the function
+        returns this object otherwise it creates a new StripePlan and
+        returns the new object.
 
         :param amount: The amount in CHF
         :param name: The name of the Stripe plan to be created.
-        :param stripe_plan_id: The id of the Stripe plan to be created. Use get_stripe_plan_id_string function to obtain
-                               the name of the plan to be created
-        :return: The StripePlan object if it exists else creates a Plan object in Stripe and a local StripePlan and
-                returns it. Returns None in case of Stripe error
+        :param stripe_plan_id: The id of the Stripe plan to be
+               created. Use get_stripe_plan_id_string function to
+               obtain the name of the plan to be created
+        :return: The StripePlan object if it exists else creates a
+               Plan object in Stripe and a local StripePlan and
+               returns it. Returns None in case of Stripe error
         """
         _amount = float(amount)
         amount = int(_amount * 100)  # stripe amount unit, in cents
         stripe_plan_db_obj = None
         try:
-            stripe_plan_db_obj = StripePlan.objects.get(stripe_plan_id=stripe_plan_id)
+            stripe_plan_db_obj = StripePlan.objects.get(
+                stripe_plan_id=stripe_plan_id)
         except StripePlan.DoesNotExist:
             try:
                 self.stripe.Plan.create(
@@ -162,30 +169,38 @@ class StripeUtils(object):
                     name=name,
                     currency=self.CURRENCY,
                     id=stripe_plan_id)
-                stripe_plan_db_obj = StripePlan.objects.create(stripe_plan_id=stripe_plan_id)
+                stripe_plan_db_obj = StripePlan.objects.create(
+                    stripe_plan_id=stripe_plan_id)
             except stripe.error.InvalidRequestError as e:
                 if self.STRIPE_PLAN_ALREADY_EXISTS in str(e):
-                    logger.debug(self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id))
-                    stripe_plan_db_obj = StripePlan.objects.create(stripe_plan_id=stripe_plan_id)
+                    logger.debug(
+                        self.PLAN_EXISTS_ERROR_MSG.format(stripe_plan_id))
+                    stripe_plan_db_obj = StripePlan.objects.create(
+                        stripe_plan_id=stripe_plan_id)
         return stripe_plan_db_obj
 
     @handleStripeError
     def delete_stripe_plan(self, stripe_plan_id):
         """
-        Deletes the Plan in Stripe and also deletes the local db copy of the plan if it exists
+        Deletes the Plan in Stripe and also deletes the local db copy
+        of the plan if it exists
 
-        :param stripe_plan_id: The stripe plan id that needs to be deleted
-        :return: True if the plan was deleted successfully from Stripe, False otherwise.
+        :param stripe_plan_id: The stripe plan id that needs to be
+               deleted
+        :return: True if the plan was deleted successfully from
+               Stripe, False otherwise.
         """
         return_value = False
         try:
             plan = self.stripe.Plan.retrieve(stripe_plan_id)
             plan.delete()
             return_value = True
-            StripePlan.objects.filter(stripe_plan_id=stripe_plan_id).all().delete()
+            StripePlan.objects.filter(
+                stripe_plan_id=stripe_plan_id).all().delete()
         except stripe.error.InvalidRequestError as e:
             if self.STRIPE_NO_SUCH_PLAN in str(e):
-                logger.debug(self.PLAN_DOES_NOT_EXIST_ERROR_MSG.format(stripe_plan_id))
+                logger.debug(
+                    self.PLAN_DOES_NOT_EXIST_ERROR_MSG.format(stripe_plan_id))
         return return_value
 
     @handleStripeError
@@ -194,13 +209,14 @@ class StripeUtils(object):
         Subscribes the given customer to the list of given plans
 
         :param customer: The stripe customer identifier
-        :param plans: A list of stripe plans. Ref: https://stripe.com/docs/api/python#create_subscription-items
-                      e.g.
-                            plans = [
-                                        {
-                                          "plan": "dcl-v1-cpu-2-ram-5gb-ssd-10gb",
-                                        },
-                                    ]
+        :param plans: A list of stripe plans.
+        Ref: https://stripe.com/docs/api/python#create_subscription-items
+              e.g.
+                    plans = [
+                                {
+                                  "plan": "dcl-v1-cpu-2-ram-5gb-ssd-10gb",
+                                },
+                            ]
         :return: The subscription StripeObject
         """
 
@@ -222,18 +238,25 @@ class StripeUtils(object):
     @staticmethod
     def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None):
         """
-        Returns the stripe plan id string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters
+        Returns the stripe plan id string of the form
+        `dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters
 
         :param cpu: The number of cores
         :param ram: The size of the RAM in GB
         :param ssd: The size of ssd storage in GB
         :param hdd: The size of hdd storage in GB
         :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 app: The application to which the stripe plan belongs
+        to. By default it is 'dcl'
         :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, ram=ram, ssd=ssd)
+        dcl_plan_string = 'cpu-{cpu}-ram-{ram}gb-ssd-{ssd}gb'.format(cpu=cpu,
+                                                                     ram=ram,
+                                                                     ssd=ssd)
         if hdd is not None:
-            dcl_plan_string = '{dcl_plan_string}-hdd-{hdd}gb'.format(dcl_plan_string=dcl_plan_string, hdd=hdd)
-        stripe_plan_id_string = '{app}-v{version}-{plan}'.format(app=app, version=version, plan=dcl_plan_string)
+            dcl_plan_string = '{dcl_plan_string}-hdd-{hdd}gb'.format(
+                dcl_plan_string=dcl_plan_string, hdd=hdd)
+        stripe_plan_id_string = '{app}-v{version}-{plan}'.format(app=app,
+                                                                 version=version,
+                                                                 plan=dcl_plan_string)
         return stripe_plan_id_string