mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Use a checkbox input when editing a boolean setting
This commit is contained in:
parent
f1e8afa314
commit
3a325399c6
@ -11,6 +11,7 @@ import decimal
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.core.validators import MinValueValidator, MaxValueValidator
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
@ -287,11 +288,11 @@ class InvenTreeSetting(models.Model):
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
print("Running validator:", validator, self.key, self.value)
|
|
||||||
|
|
||||||
# Check if a 'type' has been specified for this value
|
# Check if a 'type' has been specified for this value
|
||||||
if type(validator) == type:
|
if type(validator) == type:
|
||||||
|
|
||||||
if validator == bool:
|
if validator == bool:
|
||||||
|
# Value must "look like" a boolean value
|
||||||
if InvenTree.helpers.is_bool(self.value):
|
if InvenTree.helpers.is_bool(self.value):
|
||||||
# Coerce into either "True" or "False"
|
# Coerce into either "True" or "False"
|
||||||
self.value = str(InvenTree.helpers.str2bool(self.value))
|
self.value = str(InvenTree.helpers.str2bool(self.value))
|
||||||
@ -300,8 +301,6 @@ class InvenTreeSetting(models.Model):
|
|||||||
'value': _('Value must be a boolean value')
|
'value': _('Value must be a boolean value')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def validate_unique(self, exclude=None):
|
def validate_unique(self, exclude=None):
|
||||||
""" Ensure that the key:value pair is unique.
|
""" Ensure that the key:value pair is unique.
|
||||||
In addition to the base validators, this ensures that the 'key'
|
In addition to the base validators, this ensures that the 'key'
|
||||||
@ -317,6 +316,15 @@ class InvenTreeSetting(models.Model):
|
|||||||
except InvenTreeSetting.DoesNotExist:
|
except InvenTreeSetting.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def is_bool(self):
|
||||||
|
"""
|
||||||
|
Check if this setting is required to be a boolean value
|
||||||
|
"""
|
||||||
|
|
||||||
|
validator = InvenTreeSetting.get_setting_validator(self.key)
|
||||||
|
|
||||||
|
return validator == bool
|
||||||
|
|
||||||
|
|
||||||
class Currency(models.Model):
|
class Currency(models.Model):
|
||||||
"""
|
"""
|
||||||
|
@ -4,11 +4,11 @@
|
|||||||
{% block pre_form_content %}
|
{% block pre_form_content %}
|
||||||
|
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
|
<!--
|
||||||
<p>
|
<p>
|
||||||
<b>{{ name }}</b><br>
|
<b>{{ name }}</b><br>
|
||||||
{{ description }}<br>
|
{{ description }}<br>
|
||||||
<i>{% trans "Current value" %}: {{ value }}</i>
|
<i>{% trans "Current value" %}: {{ value }}</i>
|
||||||
</p>
|
</p>
|
||||||
|
-->
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -6,8 +6,10 @@ Django views for interacting with common models
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
from django.forms import CheckboxInput
|
||||||
|
|
||||||
from InvenTree.views import AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
from InvenTree.views import AjaxCreateView, AjaxUpdateView, AjaxDeleteView
|
||||||
|
from InvenTree.helpers import str2bool
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
from . import forms
|
from . import forms
|
||||||
@ -63,3 +65,30 @@ class SettingEdit(AjaxUpdateView):
|
|||||||
ctx['description'] = models.InvenTreeSetting.get_setting_description(setting.key)
|
ctx['description'] = models.InvenTreeSetting.get_setting_description(setting.key)
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
def get_form(self):
|
||||||
|
"""
|
||||||
|
Override default get_form behaviour
|
||||||
|
"""
|
||||||
|
|
||||||
|
form = super().get_form()
|
||||||
|
|
||||||
|
setting = self.get_object()
|
||||||
|
|
||||||
|
if setting.is_bool():
|
||||||
|
form.fields['value'].widget = CheckboxInput()
|
||||||
|
|
||||||
|
self.object.value = str2bool(setting.value)
|
||||||
|
form.fields['value'].value = str2bool(setting.value)
|
||||||
|
|
||||||
|
name = models.InvenTreeSetting.get_setting_name(setting.key)
|
||||||
|
|
||||||
|
if name:
|
||||||
|
form.fields['value'].label = name
|
||||||
|
|
||||||
|
description = models.InvenTreeSetting.get_setting_description(setting.key)
|
||||||
|
|
||||||
|
if description:
|
||||||
|
form.fields['value'].help_text = description
|
||||||
|
|
||||||
|
return form
|
||||||
|
Loading…
Reference in New Issue
Block a user