Refactor HelperForm to easily allow setting prepended text / placeholder / etc

This commit is contained in:
Oliver Walters 2020-05-16 09:33:34 +10:00
parent 0ccac09962
commit 8fae32e3c7
2 changed files with 46 additions and 15 deletions

View File

@ -7,13 +7,19 @@ from __future__ import unicode_literals
from django import forms from django import forms
from crispy_forms.helper import FormHelper 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 from django.contrib.auth.models import User
class HelperForm(forms.ModelForm): class HelperForm(forms.ModelForm):
""" Provides simple integration of crispy_forms extension. """ """ 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): def __init__(self, *args, **kwargs):
super(forms.ModelForm, self).__init__(*args, **kwargs) super(forms.ModelForm, self).__init__(*args, **kwargs)
self.helper = FormHelper() self.helper = FormHelper()
@ -28,7 +34,36 @@ class HelperForm(forms.ModelForm):
Simply create a 'blank' layout for each available field. 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): class DeleteForm(forms.Form):

View File

@ -100,8 +100,6 @@ class SerializeStockForm(HelperForm):
# Extract the stock item # Extract the stock item
item = kwargs.pop('item') item = kwargs.pop('item')
super().__init__(*args, **kwargs)
# Pre-calculate what the serial numbers should be! # Pre-calculate what the serial numbers should be!
sn = item.part.get_next_serial_number() sn = item.part.get_next_serial_number()
@ -110,17 +108,15 @@ class SerializeStockForm(HelperForm):
else: else:
sn = str(sn) sn = str(sn)
# TODO - Refactor this? Should not have to specify Field('field') for each field... self.prefix = {
self.helper.layout = Layout( 'serial_numbers': 'fa-hashtag',
Field('quantity'), }
Field(PrependedText(
'serial_numbers', self.placeholder = {
'#', 'serial_numbers': sn
placeholder=sn }
)),
Field('destination'), super().__init__(*args, **kwargs)
Field('note'),
)
class Meta: class Meta:
model = StockItem model = StockItem