def check_value(self, value): value = str(value) try: get_currency(value.upper()) return True except CurrencyDoesNotExist: return False
def _dump_currency(obj): """Serialize standard (ISO-defined) currencies to currency code only, and non-standard (user-added) currencies in full. """ from moneyed import get_currency, CurrencyDoesNotExist try: get_currency(obj.code) return obj.code except CurrencyDoesNotExist: return getattrs(obj, ['code', 'numeric', 'name', 'countries'])
def get_currencies_by_user(user_id: int) -> list: """ Returns all currencies used by user in format: (currency_code_1, currency_name_1), (currency_code_2, currency_name_2) """ accounts = get_accounts_by_user(user_id) result = list() unique_currencies = accounts \ .values_list('balance_currency') \ .order_by('balance_currency') \ .distinct('balance_currency') for currency in unique_currencies: CODE_POSITION = 0 code = currency[CODE_POSITION] try: name = get_currency(code=code).name except CurrencyDoesNotExist: continue result.append({'code': code, 'name': name}) return result
def _apply(self, value): # Lazy-load dependencies to reduce memory usage when this # filter is not used in a project. from moneyed import ( Currency as CurrencyType, CurrencyDoesNotExist, get_currency, ) value = self._filter(value, Type(string_types + (CurrencyType, ))) if self._has_errors: return None if isinstance(value, CurrencyType): return value try: # # Note that ``get_currency`` explicitly casts the incoming # code to ASCII, so it is possible to get a # UnicodeDecodeError here (e.g., the incoming value is # a currency symbol instead of an ISO currency code). # # To keep things simple for the end user, we will treat # this error the same as if the incoming value was a non- # matching ASCII value. # return get_currency(code=value.upper()) except (CurrencyDoesNotExist, UnicodeDecodeError): return self._invalid_value(value, self.CODE_INVALID, exc_info=True)
def _set_decimal(self: GS1ElementString) -> None: variable_measure = self.ai.ai[:2] in ( "31", "32", "33", "34", "35", "36", ) amount_payable = self.ai.ai[:3] in ("390", "392") amount_payable_with_currency = self.ai.ai[:3] in ("391", "393") percentage = self.ai.ai[:3] in ("394", ) if (variable_measure or amount_payable or amount_payable_with_currency or percentage): # See GS1 General Specifications, chapter 3.6 for details. # Only group for variable_measure, amount_payable, and percentage. # Second and last group for amount_payable_with_currency. value = self.pattern_groups[-1] num_decimals = int(self.ai.ai[3]) num_units = len(value) - num_decimals units = value[:num_units] decimals = value[num_units:] self.decimal = Decimal(f"{units}.{decimals}") if amount_payable_with_currency and moneyed is not None: currency = moneyed.get_currency(iso=self.pattern_groups[0]) self.money = moneyed.Money(amount=self.decimal, currency=currency)
def moneyformat(value, currency = 'PLN', decimal_pos = 2, locale = None): locale = get_money_locale(locale) if isinstance(value, Money): pass else: value = Money(value, currency = get_currency(str(currency))) return format_money(value, decimal_places = decimal_pos, locale = locale)
def _load_currency(val): """Deserialize string values as standard currencies, but manually define fully-defined currencies (with code/name/numeric/countries). """ from moneyed import get_currency try: return get_currency(code=val) except: return Currency(**val)
def transaction_data_to_dict(self, data: tuple, invoice: Invoice = None): """ Internal method used to transform tuple with transactions data to dict. See `InvoiceCreateOrEditView.get_transactions_data` source for details. :param invoice: invoice for returned transaction :param data: tuple with 6 elements :return: tuple that can be used for Transaction create_or_update method """ return (int(data[0]) if data[0] else None, { 'date': parse(data[1]).date(), 'amount': self.get_decimal(data[2]), 'currency': get_currency(data[3]), 'quantity': self.get_decimal(data[4]), 'unit': data[5], 'comment': data[6], 'account': self.accounts[int(data[7])], 'invoice': invoice })
def convert_row(self, row: Sequence[str]) -> Optional[Transaction]: # CSV format documentation is available at # https://www.rabobank.nl/images/formaatbeschrijving_csv_kommagescheiden_nieuw_29539176.pdf. # The colums are: # 1) IBAN of own bank account. Required. # 2) ISO 4217 currency code. # 3) Interest date. ^\d{6}$ (YYYYMMDD). # 4) "D" for debit and "C" for credit transactions respectively. # 5) Two-decimal amount, with a period as decimal separator. Max 14 # characters. # 6) Number of the remote bank account. Optional. Max 35 characters. # 7) Remote name. Max 70 characters. # 8) Transaction date. ^\d{6}$ (YYYYMMDD). # 9) Booking code. 2 characters. # 10) Filler. Max 6 characters. # 11) Description, line 1. Max 35 characters. # 12) Description, line 2. Max 35 characters. # 13) Description, line 3. Max 35 characters. # 14) Description, line 4. Max 35 characters. # 15) Description, line 5. Max 35 characters. # 16) Description, line 6. Max 35 characters. # 17) SEPA credit transfer end-to-end ID. Max 35 characters. # 18) SEPA credit transfer remove account ID. Max 35 characters. # 19) SEPA direct debit mandate ID. Max 35 characters. remote_id = 'rabobank-csv:' + hashlib.sha256( "\n".join(row).encode('utf-8')).hexdigest() if Transaction.objects.filter(remote_id=remote_id).exists(): return None transaction = Transaction() transaction.remote_id = remote_id try: own_account = Account.objects.get(number=row[0]) except Account.DoesNotExist: own_account = Account(number=row[0], label=row[0]) own_account.save() transaction.own_account = own_account transaction.opposing_name = row[6] transaction.amount = Money(Decimal(row[4]), get_currency(row[1])) transaction.description = ' '.join( [row[10], row[11], row[12], row[13], row[14], row[15]]).strip() return transaction
def get_available_currencies() -> list: """ Returns list of available currencies for rate backend. """ result = list() available_currencies = Rate.objects.values_list('currency') for currency in available_currencies: CODE_POSITION = 0 code = currency[CODE_POSITION] try: name = get_currency(code=code).name except CurrencyDoesNotExist: continue result.append({'code': code, 'name': name}) return result
def to_internal_value(self, data): return get_currency(iso=data)
def test_currency_std(self): from moneyed import get_currency a = get_currency('USD') b = self.dump_and_load(a) self.assertEqual(b.numeric, a.numeric)
def get_default_currency(): return get_currency(bazaar_settings.DEFAULT_CURRENCY)
def test_pass_currency_object(self): """ The incoming value is already a :py:class:`moneyed.Currency` object. """ self.assertFilterPasses(get_currency(code='USD'))
GBP_CHOICE_TUPLE = [(DEFAULT_CURRENCY_CODE, "Pound Sterling")] _FORMATTER.add_sign_definition(DEFAULT, moneyed.GBP, prefix=u"£") _FORMATTER.add_sign_definition(DEFAULT, moneyed.USD, prefix=u"$") _FORMATTER.add_sign_definition(DEFAULT, moneyed.AUD, prefix=u"A$") _FORMATTER.add_sign_definition(DEFAULT, moneyed.EUR, prefix=u"€") CURRENCIES = [ 'GBP', 'USD', 'AUD', 'EUR', ] CURRENCY_CHOICES = [ (DEFAULT_CURRENCY_CODE, moneyed.get_currency(DEFAULT_CURRENCY_CODE).name), ] # API SETTINGS AND CREDENTIALS #-------------------------------------------------------- # Production key, but test list. MAILCHIMP_API_KEY = '2e175c4f1d007c9a1750e2d7993eb626-us10' MAILCHIMP_MAIN_LIST_ID = '075d485454' TICKET_EVOLUTION_TOKEN = 'bdb802b503ff68f0072b95f35f3f2fa5' TICKET_EVOLUTION_SECRET = 'efDpKWr6yeUH20FnMLgY5IHdKpteoK+g4UzCgDRr' TICKET_EVOLUTION_USE_SANDBOX = True TICKET_EVOLUTION_URL_PREFIX = '/v9' STUBHUB_APPLICATION_TOKEN = 'FR_EAm_IXA_vHtromlp6200jQvIa'