Skip to content

Commit

Permalink
make possible to reuse get_change_price()
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed May 16, 2024
1 parent 07b9d56 commit 2a6a182
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
23 changes: 23 additions & 0 deletions plans/plan_change.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# coding=utf-8
from decimal import Decimal
from django.conf import settings
from plans.importer import import_name


class PlanChangePolicy(object):
Expand Down Expand Up @@ -96,3 +98,24 @@ def _calculate_final_price(self, period, day_cost_diff):
return None
else:
return cost


def get_policy():
policy_class = getattr(
settings,
"PLANS_CHANGE_POLICY",
"plans.plan_change.StandardPlanChangePolicy",
)
return import_name(policy_class)()


def get_change_price(userplan, plan):
policy = get_policy()

if userplan.expire is not None:
period = userplan.days_left()
else:
# Use the default period of the new plan
period = 30

return policy.get_change_price(userplan.plan, plan, period)
22 changes: 2 additions & 20 deletions plans/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from plans.signals import order_started
from plans.utils import get_currency
from plans.validators import plan_validation
from plans.plan_change import get_change_price

UserPlan = AbstractUserPlan.get_concrete_model()
PlanPricing = AbstractPlanPricing.get_concrete_model()
Expand Down Expand Up @@ -340,27 +341,8 @@ def get_all_context(self):
)
self.pricing = None

def get_policy(self):
policy_class = getattr(
settings,
"PLANS_CHANGE_POLICY",
"plans.plan_change.StandardPlanChangePolicy",
)
return import_name(policy_class)()

def get_price(self):
policy = self.get_policy()
userplan = self.request.user.userplan

if userplan.expire is not None:
period = self.request.user.userplan.days_left()
else:
# Use the default period of the new plan
period = 30

return policy.get_change_price(
self.request.user.userplan.plan, self.plan, period
)
return get_change_price(self.request.user.userplan, self.plan)

def get_context_data(self, **kwargs):
context = super(CreateOrderView, self).get_context_data(**kwargs)
Expand Down

0 comments on commit 2a6a182

Please sign in to comment.