diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 3d8871f824..fb47f2a32b 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -7,13 +7,19 @@ from __future__ import unicode_literals from django import forms from crispy_forms.helper import FormHelper -from crispy_forms.layout import Layout +from crispy_forms.layout import Layout, Field +from crispy_forms.bootstrap import PrependedAppendedText from django.contrib.auth.models import User class HelperForm(forms.ModelForm): """ Provides simple integration of crispy_forms extension. """ + # Custom field decorations can be specified here, per form class + prefix = {} + suffix = {} + placeholder = {} + def __init__(self, *args, **kwargs): super(forms.ModelForm, self).__init__(*args, **kwargs) self.helper = FormHelper() @@ -28,7 +34,36 @@ class HelperForm(forms.ModelForm): Simply create a 'blank' layout for each available field. """ - self.helper.layout = Layout(*self.fields.keys()) + layouts = [] + + for field in self.fields: + prefix = self.prefix.get(field, None) + suffix = self.suffix.get(field, None) + placeholder = self.placeholder.get(field, None) + + # Look for font-awesome icons + if prefix and prefix.startswith('fa-'): + prefix = "".format(fa=prefix) + + if suffix and suffix.startswith('fa-'): + suffix = "".format(fa=suffix) + + if prefix or suffix or placeholder: + layouts.append( + Field( + PrependedAppendedText( + field, + prepended_text=prefix, + appended_text=suffix, + placeholder=placeholder + ) + ) + ) + + else: + layouts.append(Field(field)) + + self.helper.layout = Layout(*layouts) class DeleteForm(forms.Form): diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 3eb3c1018b..cf04396f54 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -100,8 +100,6 @@ class SerializeStockForm(HelperForm): # Extract the stock item item = kwargs.pop('item') - super().__init__(*args, **kwargs) - # Pre-calculate what the serial numbers should be! sn = item.part.get_next_serial_number() @@ -110,17 +108,15 @@ class SerializeStockForm(HelperForm): else: sn = str(sn) - # TODO - Refactor this? Should not have to specify Field('field') for each field... - self.helper.layout = Layout( - Field('quantity'), - Field(PrependedText( - 'serial_numbers', - '#', - placeholder=sn - )), - Field('destination'), - Field('note'), - ) + self.prefix = { + 'serial_numbers': 'fa-hashtag', + } + + self.placeholder = { + 'serial_numbers': sn + } + + super().__init__(*args, **kwargs) class Meta: model = StockItem