currency codes in own function

This commit is contained in:
Matthias 2021-07-01 10:32:07 +02:00
parent 3c6c9c59d6
commit 66c1e9c97d
2 changed files with 27 additions and 6 deletions

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
import sys
from .validators import allowable_url_schemes from .validators import allowable_url_schemes
@ -11,7 +12,6 @@ from django.forms.fields import URLField as FormURLField
from django.db import models as models from django.db import models as models
from django.core import validators from django.core import validators
from django import forms from django import forms
from django.conf import settings
from decimal import Decimal from decimal import Decimal
from djmoney.models.fields import MoneyField as ModelMoneyField from djmoney.models.fields import MoneyField as ModelMoneyField
@ -38,12 +38,25 @@ class InvenTreeURLField(models.URLField):
}) })
def money_kwargs():
""" returns the database settings for MoneyFields """
kwargs = {}
kwargs['currency_choices'] = common.settings.currency_codes()
kwargs['default_currency'] = common.settings.currency_code_default
return kwargs
class InvenTreeModelMoneyField(ModelMoneyField): class InvenTreeModelMoneyField(ModelMoneyField):
""" custom MoneyField for clean migrations while using dynamic currency settings """ """ custom MoneyField for clean migrations while using dynamic currency settings """
def __init__(self, **kwargs): def __init__(self, **kwargs):
# remove currency information for a clean migration # detect if creating migration
kwargs['default_currency'] = '' if 'makemigrations' in sys.argv:
kwargs['currency_choices'] = [] # remove currency information for a clean migration
kwargs['default_currency'] = ''
kwargs['currency_choices'] = []
else:
# set defaults
kwargs.update(money_kwargs())
super().__init__(**kwargs) super().__init__(**kwargs)
@ -57,8 +70,7 @@ class InvenTreeMoneyField(MoneyField):
""" custom MoneyField for clean migrations while using dynamic currency settings """ """ custom MoneyField for clean migrations while using dynamic currency settings """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# override initial values with the real info from database # override initial values with the real info from database
kwargs['currency_choices'] = [(a, a) for a in settings.CURRENCIES] kwargs.update(money_kwargs())
kwargs['default_currency'] = common.settings.currency_code_default
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

View File

@ -6,6 +6,7 @@ User-configurable settings for the common app
from __future__ import unicode_literals from __future__ import unicode_literals
from moneyed import CURRENCIES from moneyed import CURRENCIES
from django.conf import settings
import common.models import common.models
@ -23,6 +24,14 @@ def currency_code_default():
return code return code
def currency_codes():
"""
Returns the current currency choices
"""
return [(a, a) for a in settings.CURRENCIES]
def stock_expiry_enabled(): def stock_expiry_enabled():
""" """
Returns True if the stock expiry feature is enabled Returns True if the stock expiry feature is enabled