From 66c1e9c97d97b76e9c35ce070b3f7860769a1baa Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 1 Jul 2021 10:32:07 +0200 Subject: [PATCH] currency codes in own function --- InvenTree/InvenTree/fields.py | 24 ++++++++++++++++++------ InvenTree/common/settings.py | 9 +++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) 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