Ejemplo n.º 1
0
def get_selected_currency():
    cookie = request.cookies.get('currency')
    return cookie if cookie is not None else prop.get_currency_default()
Ejemplo n.º 2
0
def get_data():
    return {'default': prop.get_currency_default(),
            'selected': get_selected_currency(),
            'all': prop.get_currencies(),
            'rates': get_rates()}
Ejemplo n.º 3
0
        logging.warn('Couldn\'t load currency rates', e)
    if xml_string is None:
        xml_string = rates_string
    tree = etree.fromstring(xml_string)
    rates = {}
    for rate in tree.findall('.//*[@currency]'):
        cur_name = rate.attrib['currency']
        if cur_name in prop.get_currencies():
            rates[cur_name] = {
                'val': float(rate.text),
                'multiplier':
                int(rate.attrib['multiplier'])
                if 'multiplier' in rate.attrib
                else 1
            }
    rates[prop.get_currency_default()] = {'val': 1, 'multiplier': 1}
    return rates


def convert(val, currency=None, rates=None):
    currency = currency if currency is not None else get_selected_currency()
    rates = rates if rates is not None else get_rates()
    rate = rates[currency]
    return val / rate['val'] * rate['multiplier']


def get_selected_currency():
    cookie = request.cookies.get('currency')
    return cookie if cookie is not None else prop.get_currency_default()