parent
4929c78e47
commit
1e5a6a11f7
7 changed files with 57 additions and 23 deletions
@ -1,20 +1,48 @@ |
||||
#!/usr/bin/env python |
||||
# -*- coding: utf-8 -*- |
||||
from markupsafe import Markup |
||||
from .models import Currency as CurrencyModel |
||||
|
||||
MAIN_CURRENCY_ID = '1' |
||||
MAIN_CURRENCY_ID = 1 |
||||
DEFAULT_SYMBOL = '₸' # Тенге |
||||
|
||||
|
||||
class Currency(object): |
||||
currency_id = '' |
||||
id = '' |
||||
exchange = '' |
||||
symbol = '' |
||||
|
||||
def __init__(self, request): |
||||
self.cart_id = request.session.get('currency_id', None) |
||||
if not self.currency_id: |
||||
self.currency_id = MAIN_CURRENCY_ID |
||||
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 Markup('{price} {code}'.format(price=_price, code=self.symbol)) |
||||
|
||||
|
||||
Loading…
Reference in new issue