From 75ab7b247b4ced75581b9f3fbd194e78920d2eb0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 9 Nov 2020 23:16:04 +1100 Subject: [PATCH] Push part settings into part/settings.py - Use the user-configurable defaults in the database model itself - This means they are observed even when using the API / etc --- InvenTree/common/models.py | 6 ++++-- InvenTree/part/models.py | 38 +++++++++++++++++++++++++++++++------- InvenTree/part/settings.py | 34 ++++++++++++++++++++++++++++++++++ InvenTree/part/views.py | 5 ----- 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 InvenTree/part/settings.py diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 2657804e37..0f12c57c3c 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -263,9 +263,11 @@ class InvenTreeSetting(models.Model): setting = InvenTreeSetting.get_setting_object(key) if setting: - return setting.value + value = setting.value else: - return backup_value + value = backup_value + + return value @classmethod def set_setting(cls, key, value, user, create=True): diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 0c8aeb5665..c421bbf863 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -47,6 +47,7 @@ from company.models import SupplierPart from stock import models as StockModels import common.models +import part.settings as part_settings class PartCategory(InvenTreeTree): @@ -656,19 +657,42 @@ class Part(MPTTModel): units = models.CharField(max_length=20, default="", blank=True, null=True, help_text=_('Stock keeping units for this part')) - assembly = models.BooleanField(default=False, verbose_name='Assembly', help_text=_('Can this part be built from other parts?')) + assembly = models.BooleanField( + default=False, + verbose_name=_('Assembly'), + help_text=_('Can this part be built from other parts?') + ) - component = models.BooleanField(default=True, verbose_name='Component', help_text=_('Can this part be used to build other parts?')) + component = models.BooleanField( + default=True, + verbose_name=_('Component'), + help_text=_('Can this part be used to build other parts?') + ) - trackable = models.BooleanField(default=False, help_text=_('Does this part have tracking for unique items?')) + trackable = models.BooleanField( + default=part_settings.part_trackable_default, + verbose_name=_('Trackable'), + help_text=_('Does this part have tracking for unique items?')) - purchaseable = models.BooleanField(default=True, help_text=_('Can this part be purchased from external suppliers?')) + purchaseable = models.BooleanField( + default=part_settings.part_purchaseable_default, + verbose_name=_('Purchaseable'), + help_text=_('Can this part be purchased from external suppliers?')) - salable = models.BooleanField(default=False, help_text=_("Can this part be sold to customers?")) + salable = models.BooleanField( + default=part_settings.part_salable_default, + verbose_name=_('Salable'), + help_text=_("Can this part be sold to customers?")) - active = models.BooleanField(default=True, help_text=_('Is this part active?')) + active = models.BooleanField( + default=True, + verbose_name=_('Active'), + help_text=_('Is this part active?')) - virtual = models.BooleanField(default=False, help_text=_('Is this a virtual part, such as a software product or license?')) + virtual = models.BooleanField( + default=False, + verbose_name=_('Virtual'), + help_text=_('Is this a virtual part, such as a software product or license?')) notes = MarkdownxField(blank=True, null=True, help_text=_('Part notes - supports Markdown formatting')) diff --git a/InvenTree/part/settings.py b/InvenTree/part/settings.py new file mode 100644 index 0000000000..a06ad8473d --- /dev/null +++ b/InvenTree/part/settings.py @@ -0,0 +1,34 @@ +""" +User-configurable settings for the Part app +""" + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from InvenTree.helpers import str2bool + +from common.models import InvenTreeSetting + + +def part_purchaseable_default(): + """ + Returns the default value for the 'purchasable' field for a Part object + """ + + return str2bool(InvenTreeSetting.get_setting('PART_PURCHASEABLE')) + + +def part_salable_default(): + """ + Returns the default value for the 'salable' field for a Part object + """ + + return str2bool(InvenTreeSetting.get_setting('PART_SALABLE')) + + +def part_trackable_default(): + """ + Returns the defualt value fro the 'trackable' field for a Part object + """ + + return str2bool(InvenTreeSetting.get_setting('PART_TRACKABLE')) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 7961434af0..2504b056c5 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -700,11 +700,6 @@ class PartCreate(AjaxCreateView): if label in self.request.GET: initials[label] = self.request.GET.get(label) - # Default values for part options - initials['purchaseable'] = str2bool(InvenTreeSetting.get_setting('PART_PURCHASEABLE')) - initials['salable'] = str2bool(InvenTreeSetting.get_setting('PART_SALABLE')) - initials['trackable'] = str2bool(InvenTreeSetting.get_setting('PART_TRACKABLE')) - return initials