You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.5 KiB
51 lines
1.5 KiB
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
from markupsafe import Markup
|
|
from .models import Currency as CurrencyModel
|
|
|
|
MAIN_CURRENCY_ID = 1
|
|
DEFAULT_SYMBOL = '₸' # Тенге
|
|
|
|
|
|
class Currency(object):
|
|
id = ''
|
|
exchange = ''
|
|
symbol = ''
|
|
|
|
def __init__(self, request):
|
|
self.id = request.session.get('currency_id', None)
|
|
|
|
if self.id:
|
|
try:
|
|
_currency = CurrencyModel.objects.get(pk=self.id)
|
|
self.exchange = _currency.exchange
|
|
self.symbol = _currency.HTML_letter_code
|
|
except CurrencyModel.DoesNotExist:
|
|
pass
|
|
|
|
else:
|
|
self.id = MAIN_CURRENCY_ID
|
|
self.symbol = DEFAULT_SYMBOL
|
|
self.exchange = 1
|
|
request.session['currency_id'] = MAIN_CURRENCY_ID
|
|
|
|
def set_currency(self, c_id, request):
|
|
self.id = c_id
|
|
try:
|
|
_currency = CurrencyModel.objects.get(pk=self.id)
|
|
self.symbol = _currency.HTML_letter_code
|
|
self.exchange = _currency.exchange
|
|
except CurrencyModel.DoesNotExist:
|
|
self.symbol = DEFAULT_SYMBOL
|
|
request.session['currency_id'] = c_id
|
|
|
|
def get_symbol(self):
|
|
return Markup(self.symbol)
|
|
|
|
def get_price(self, price):
|
|
_price = int(price * self.exchange)
|
|
return _price
|
|
|
|
def get_string_price(self, price):
|
|
return Markup('{price} {code}'.format(price=self.get_price(price), code=self.symbol))
|
|
|
|
|