Skip to content

Commit

Permalink
Update to use a Class instead of a global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep-Chill committed Nov 13, 2023
1 parent bb880aa commit 4f54ff9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
4 changes: 2 additions & 2 deletions saas/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from . import settings
from .compat import gettext_lazy as _
from .models import AdvanceDiscount, Plan, Subscription
from .utils import get_organization_model, get_currency_choices
from .utils import get_organization_model, CurrencyDataLoader

#pylint: disable=no-member

Expand Down Expand Up @@ -260,7 +260,7 @@ class PlanForm(forms.ModelForm):
(slugify(choice[1]), choice[1]) for choice in Plan.INTERVAL_CHOICES])
renewal_type = forms.ChoiceField(choices=[
(slugify(choice[1]), choice[1]) for choice in Plan.RENEWAL_CHOICES])
unit = forms.ChoiceField(choices=get_currency_choices())
unit = forms.ChoiceField(choices=CurrencyDataLoader.get_currency_choices())
period_amount = forms.DecimalField(max_digits=7, decimal_places=2)
advance_discount_type = forms.ChoiceField(choices=[
(slugify(choice[1]), choice[1])
Expand Down
4 changes: 2 additions & 2 deletions saas/humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from . import settings
from .compat import gettext_lazy as _
from .utils import get_currency_symbols
from .utils import CurrencyDataLoader

# prevents an import loop with models.py
HOURLY = 1
Expand Down Expand Up @@ -191,7 +191,7 @@ def as_money(value, currency=settings.DEFAULT_UNIT, negative_format="(%s)"):
negative = True
currency = currency[1:]
currency = currency.lower()
currency_symbol = get_currency_symbols(currency) if currency \
currency_symbol = CurrencyDataLoader.get_currency_symbols(currency) if currency \
else None
if currency_symbol:
unit_prefix = currency_symbol
Expand Down
59 changes: 28 additions & 31 deletions saas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,36 +404,33 @@ def build_absolute_uri(request, location='/', provider=None, with_scheme=True):
# than throwing an error.
return location

class CurrencyDataLoader:
_currency_data = None

_currency_data = None


def load_currency_data():
global _currency_data
if _currency_data is None:
try:
with open(settings.CURRENCY_JSON_PATH, 'r') as file:
currency_list = json.load(file)
_currency_data = {currency['cc']: currency for
currency in currency_list}
except FileNotFoundError:
raise
return _currency_data


def get_currency_symbols(currency_code):
data = load_currency_data()
if data:
currency = data.get(currency_code.upper())
if currency:
return currency['symbol']
return None

@classmethod
def load_currency_data(cls):
if cls._currency_data is None:
try:
with open(settings.CURRENCY_JSON_PATH, 'r') as file:
currency_list = json.load(file)
cls._currency_data = {currency['cc']:
currency for currency in currency_list}
except FileNotFoundError:
raise
return cls._currency_data

@classmethod
def get_currency_symbols(cls, currency_code):
data = cls.load_currency_data()
if data:
currency = data.get(currency_code.upper())
if currency:
return currency['symbol']
return None

def get_currency_choices():
data = load_currency_data()
if data:
return [(
code.lower(), code.lower()) for
code, info in data.items()]
return []
@classmethod
def get_currency_choices(cls):
data = cls.load_currency_data()
if data:
return [(code.lower(), code.lower()) for code in data.keys()]
return []

0 comments on commit 4f54ff9

Please sign in to comment.