From 31c7cbea016b711b6042f736ff59b0f1015f3a6a Mon Sep 17 00:00:00 2001 From: Sergio Oliveira Date: Thu, 1 Oct 2015 17:28:58 -0300 Subject: [PATCH] Used lazy choices for display list names --- colab/accounts/forms.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/colab/accounts/forms.py b/colab/accounts/forms.py index ec399e7..58668d2 100644 --- a/colab/accounts/forms.py +++ b/colab/accounts/forms.py @@ -11,6 +11,7 @@ from django.contrib.sites.shortcuts import get_current_site from django.core.urlresolvers import reverse from django.template import loader from django.utils.encoding import force_bytes +from django.utils.functional import lazy from django.utils.http import urlsafe_base64_encode from django.utils.text import capfirst from django.utils.translation import ugettext_lazy as _ @@ -139,16 +140,32 @@ class UserUpdateForm(UserForm): required=False) +def get_lists_choices(): + lists_names = [] + for mlist in mailman.all_lists(): + name = mlist.get('listname') + desc = mlist.get('description') + formatted_desc = u'{} ({})'.format(name, desc) + lists_names.append((name, formatted_desc)) + return lists_names + + +# XXX: This field is no longer required when using django 1.8 +class MultipleChoiceFieldLazy(forms.MultipleChoiceField): + def _set_choices(self, value): + self._choices = self.widget.choices = value + + def _get_choices(self): + return list(self._choices) + + choices = property(_get_choices, _set_choices) + + class ListsForm(forms.Form): - LISTS_NAMES = (( - mlist.get('listname'), u'{} ({})'.format(mlist.get('listname'), - mlist.get('description')) - ) for mlist in mailman.all_lists()) - - lists = forms.MultipleChoiceField(label=_(u'Mailing lists'), - required=False, - widget=forms.CheckboxSelectMultiple, - choices=LISTS_NAMES) + lists = MultipleChoiceFieldLazy(label=_(u'Mailing lists'), + required=False, + widget=forms.CheckboxSelectMultiple, + choices=lazy(get_lists_choices, list)()) class UserCreationForm(UserForm): -- libgit2 0.21.2