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
This commit is contained in:
Oliver Walters 2020-11-09 23:16:04 +11:00
parent e1b70ff68f
commit 75ab7b247b
4 changed files with 69 additions and 14 deletions

View File

@ -263,9 +263,11 @@ class InvenTreeSetting(models.Model):
setting = InvenTreeSetting.get_setting_object(key) setting = InvenTreeSetting.get_setting_object(key)
if setting: if setting:
return setting.value value = setting.value
else: else:
return backup_value value = backup_value
return value
@classmethod @classmethod
def set_setting(cls, key, value, user, create=True): def set_setting(cls, key, value, user, create=True):

View File

@ -47,6 +47,7 @@ from company.models import SupplierPart
from stock import models as StockModels from stock import models as StockModels
import common.models import common.models
import part.settings as part_settings
class PartCategory(InvenTreeTree): 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')) 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')) notes = MarkdownxField(blank=True, null=True, help_text=_('Part notes - supports Markdown formatting'))

View File

@ -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'))

View File

@ -700,11 +700,6 @@ class PartCreate(AjaxCreateView):
if label in self.request.GET: if label in self.request.GET:
initials[label] = self.request.GET.get(label) 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 return initials