Merge pull request #215 from Inboxen/form-mixin

Move CustomField to field class code to a mixin
This commit is contained in:
Ross Poulton 2014-06-06 10:15:19 +10:00
commit c4fdbb554c

View File

@ -24,7 +24,50 @@ from helpdesk.lib import send_templated_mail, safe_template_context
from helpdesk.models import Ticket, Queue, FollowUp, Attachment, IgnoreEmail, TicketCC, CustomField, TicketCustomFieldValue, TicketDependency from helpdesk.models import Ticket, Queue, FollowUp, Attachment, IgnoreEmail, TicketCC, CustomField, TicketCustomFieldValue, TicketDependency
from helpdesk import settings as helpdesk_settings from helpdesk import settings as helpdesk_settings
class EditTicketForm(forms.ModelForm): class CustomFieldMixin(object):
"""
Mixin that provides a method to turn CustomFields into an actual field
"""
def customfield_to_field(self, field, instanceargs):
if field.data_type == 'varchar':
fieldclass = forms.CharField
instanceargs['max_length'] = field.max_length
elif field.data_type == 'text':
fieldclass = forms.CharField
instanceargs['widget'] = forms.Textarea
instanceargs['max_length'] = field.max_length
elif field.data_type == 'integer':
fieldclass = forms.IntegerField
elif field.data_type == 'decimal':
fieldclass = forms.DecimalField
instanceargs['decimal_places'] = field.decimal_places
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
choices = field.choices_as_array
if field.empty_selection_list:
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
elif field.data_type == 'date':
fieldclass = forms.DateField
elif field.data_type == 'time':
fieldclass = forms.TimeField
elif field.data_type == 'datetime':
fieldclass = forms.DateTimeField
elif field.data_type == 'email':
fieldclass = forms.EmailField
elif field.data_type == 'url':
fieldclass = forms.URLField
elif field.data_type == 'ipaddress':
fieldclass = forms.IPAddressField
elif field.data_type == 'slug':
fieldclass = forms.SlugField
self.fields['custom_%s' % field.name] = fieldclass(**instanceargs)
class EditTicketForm(CustomFieldMixin, forms.ModelForm):
class Meta: class Meta:
model = Ticket model = Ticket
exclude = ('created', 'modified', 'status', 'on_hold', 'resolution', 'last_escalation', 'assigned_to') exclude = ('created', 'modified', 'status', 'on_hold', 'resolution', 'last_escalation', 'assigned_to')
@ -47,43 +90,8 @@ class EditTicketForm(forms.ModelForm):
'required': field.required, 'required': field.required,
'initial': initial_value, 'initial': initial_value,
} }
if field.data_type == 'varchar':
fieldclass = forms.CharField self.customfield_to_field(field, instanceargs)
instanceargs['max_length'] = field.max_length
elif field.data_type == 'text':
fieldclass = forms.CharField
instanceargs['widget'] = forms.Textarea
instanceargs['max_length'] = field.max_length
elif field.data_type == 'integer':
fieldclass = forms.IntegerField
elif field.data_type == 'decimal':
fieldclass = forms.DecimalField
instanceargs['decimal_places'] = field.decimal_places
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
choices = field.choices_as_array
if field.empty_selection_list:
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
elif field.data_type == 'date':
fieldclass = forms.DateField
elif field.data_type == 'time':
fieldclass = forms.TimeField
elif field.data_type == 'datetime':
fieldclass = forms.DateTimeField
elif field.data_type == 'email':
fieldclass = forms.EmailField
elif field.data_type == 'url':
fieldclass = forms.URLField
elif field.data_type == 'ipaddress':
fieldclass = forms.IPAddressField
elif field.data_type == 'slug':
fieldclass = forms.SlugField
self.fields['custom_%s' % field.name] = fieldclass(**instanceargs)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
@ -111,7 +119,7 @@ class EditFollowUpForm(forms.ModelForm):
model = FollowUp model = FollowUp
exclude = ('date', 'user',) exclude = ('date', 'user',)
class TicketForm(forms.Form): class TicketForm(CustomFieldMixin, forms.Form):
queue = forms.ChoiceField( queue = forms.ChoiceField(
label=_('Queue'), label=_('Queue'),
required=True, required=True,
@ -186,44 +194,8 @@ class TicketForm(forms.Form):
'help_text': field.help_text, 'help_text': field.help_text,
'required': field.required, 'required': field.required,
} }
if field.data_type == 'varchar':
fieldclass = forms.CharField self.customfield_to_field(field, instanceargs)
instanceargs['max_length'] = field.max_length
elif field.data_type == 'text':
fieldclass = forms.CharField
instanceargs['widget'] = forms.Textarea
instanceargs['max_length'] = field.max_length
elif field.data_type == 'integer':
fieldclass = forms.IntegerField
elif field.data_type == 'decimal':
fieldclass = forms.DecimalField
instanceargs['decimal_places'] = field.decimal_places
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
choices = field.choices_as_array
if field.empty_selection_list:
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
elif field.data_type == 'date':
fieldclass = forms.DateField
instanceargs['widget'] = extras.SelectDateWidget
elif field.data_type == 'time':
fieldclass = forms.TimeField
elif field.data_type == 'datetime':
fieldclass = forms.DateTimeField
elif field.data_type == 'email':
fieldclass = forms.EmailField
elif field.data_type == 'url':
fieldclass = forms.URLField
elif field.data_type == 'ipaddress':
fieldclass = forms.IPAddressField
elif field.data_type == 'slug':
fieldclass = forms.SlugField
self.fields['custom_%s' % field.name] = fieldclass(**instanceargs)
def save(self, user): def save(self, user):
@ -347,7 +319,7 @@ class TicketForm(forms.Form):
return t return t
class PublicTicketForm(forms.Form): class PublicTicketForm(CustomFieldMixin, forms.Form):
queue = forms.ChoiceField( queue = forms.ChoiceField(
label=_('Queue'), label=_('Queue'),
required=True, required=True,
@ -406,43 +378,8 @@ class PublicTicketForm(forms.Form):
'help_text': field.help_text, 'help_text': field.help_text,
'required': field.required, 'required': field.required,
} }
if field.data_type == 'varchar':
fieldclass = forms.CharField self.customfield_to_field(field, instanceargs)
instanceargs['max_length'] = field.max_length
elif field.data_type == 'text':
fieldclass = forms.CharField
instanceargs['widget'] = forms.Textarea
instanceargs['max_length'] = field.max_length
elif field.data_type == 'integer':
fieldclass = forms.IntegerField
elif field.data_type == 'decimal':
fieldclass = forms.DecimalField
instanceargs['decimal_places'] = field.decimal_places
instanceargs['max_digits'] = field.max_length
elif field.data_type == 'list':
fieldclass = forms.ChoiceField
choices = field.choices_as_array
if field.empty_selection_list:
choices.insert(0, ('','---------' ) )
instanceargs['choices'] = choices
elif field.data_type == 'boolean':
fieldclass = forms.BooleanField
elif field.data_type == 'date':
fieldclass = forms.DateField
elif field.data_type == 'time':
fieldclass = forms.TimeField
elif field.data_type == 'datetime':
fieldclass = forms.DateTimeField
elif field.data_type == 'email':
fieldclass = forms.EmailField
elif field.data_type == 'url':
fieldclass = forms.URLField
elif field.data_type == 'ipaddress':
fieldclass = forms.IPAddressField
elif field.data_type == 'slug':
fieldclass = forms.SlugField
self.fields['custom_%s' % field.name] = fieldclass(**instanceargs)
def save(self): def save(self):
""" """