diff --git a/InvenTree/InvenTree/fields.py b/InvenTree/InvenTree/fields.py index c461c0e752..6fd0637045 100644 --- a/InvenTree/InvenTree/fields.py +++ b/InvenTree/InvenTree/fields.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +import sys 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.core import validators from django import forms -from django.conf import settings from decimal import Decimal 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): """ custom MoneyField for clean migrations while using dynamic currency settings """ def __init__(self, **kwargs): - # remove currency information for a clean migration - kwargs['default_currency'] = '' - kwargs['currency_choices'] = [] + # detect if creating migration + if 'makemigrations' in sys.argv: + # remove currency information for a clean migration + kwargs['default_currency'] = '' + kwargs['currency_choices'] = [] + else: + # set defaults + kwargs.update(money_kwargs()) super().__init__(**kwargs) @@ -57,8 +70,7 @@ class InvenTreeMoneyField(MoneyField): """ custom MoneyField for clean migrations while using dynamic currency settings """ def __init__(self, *args, **kwargs): # override initial values with the real info from database - kwargs['currency_choices'] = [(a, a) for a in settings.CURRENCIES] - kwargs['default_currency'] = common.settings.currency_code_default + kwargs.update(money_kwargs()) super().__init__(*args, **kwargs) diff --git a/InvenTree/common/settings.py b/InvenTree/common/settings.py index 299d142ea9..9b107e9282 100644 --- a/InvenTree/common/settings.py +++ b/InvenTree/common/settings.py @@ -6,6 +6,7 @@ User-configurable settings for the common app from __future__ import unicode_literals from moneyed import CURRENCIES +from django.conf import settings import common.models @@ -23,6 +24,14 @@ def currency_code_default(): return code +def currency_codes(): + """ + Returns the current currency choices + """ + return [(a, a) for a in settings.CURRENCIES] + + + def stock_expiry_enabled(): """ Returns True if the stock expiry feature is enabled