mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Refactor HelperForm to easily allow setting prepended text / placeholder / etc
This commit is contained in:
parent
0ccac09962
commit
8fae32e3c7
@ -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 = "<span class='fas {fa}'></span>".format(fa=prefix)
|
||||
|
||||
if suffix and suffix.startswith('fa-'):
|
||||
suffix = "<span class='fas {fa}'></span>".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):
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user