From b42026ec33f047c516be8fb2f0b03d47ca42f2f2 Mon Sep 17 00:00:00 2001 From: Ross Poulton Date: Mon, 2 May 2011 22:32:15 +0000 Subject: [PATCH] Allow selection of all items on custom "list" fields. Fixes GH-23. --- helpdesk/forms.py | 6 ++---- helpdesk/models.py | 8 ++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index cc455002..03bfe0ae 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -8,6 +8,7 @@ forms.py - Definitions of newforms-based forms for creating and maintaining """ from datetime import datetime +from StringIO import StringIO from django import forms from django.conf import settings @@ -119,10 +120,7 @@ class TicketForm(forms.Form): instanceargs['max_digits'] = field.max_length elif field.data_type == 'list': fieldclass = forms.ChoiceField - choices = [] - for line in field.list_values.split("\n"): - choices.append((line, line)) - instanceargs['choices'] = choices + instanceargs['choices'] = field.choices_as_array elif field.data_type == 'boolean': fieldclass = forms.BooleanField elif field.data_type == 'date': diff --git a/helpdesk/models.py b/helpdesk/models.py index 9171b92d..804c770c 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -1169,6 +1169,14 @@ class CustomField(models.Model): null=True, ) + def _choices_as_array(self): + from StringIO import StringIO + valuebuffer = StringIO(self.list_values) + choices = [[item.strip(), item.strip()] for item in valuebuffer.readlines()] + valuebuffer.close() + return choices + choices_as_array = property(_choices_as_array) + required = models.BooleanField( _('Required?'), help_text=_('Does the user have to enter a value for this field?'),