File: tools.py

package info (click to toggle)
tryton-modules-currency 7.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 1,175; xml: 234; makefile: 11; sh: 3
file content (42 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime
from decimal import Decimal

from proteus import Model

__all__ = ['get_currency']

_names = {
    'USD': 'U.S. Dollar',
    'EUR': 'Euro',
    }
_symbols = {
    'USD': '$',
    'EUR': '€',
    }
_rates = {
    'USD': Decimal('1.0'),
    'EUR': Decimal('2.0'),
    }


def get_currency(code='USD', config=None):
    "Get currency with code"
    Currency = Model.get('currency.currency', config=config)
    CurrencyRate = Model.get('currency.currency.rate', config=config)

    currencies = Currency.find([('code', '=', code)])
    if not currencies:
        currency = Currency(name=_names.get(code, code),
            symbol=_symbols.get(code), code=code,
            rounding=Decimal('0.01'))
        currency.save()
        rate = _rates.get(code)
        if rate is not None:
            CurrencyRate(date=datetime.date.min, rate=rate,
                currency=currency).save()
    else:
        currency, = currencies
    return currency