Skip to content

Commit

Permalink
Used lazy choices for display list names
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Barbosa <alexandreab@live.com>
Signed-off-by: Lucas Moura <lucas.moura128@gmail.com>
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Signed-off-by: Sergio Oliveira <sergio@tracy.com.br>
  • Loading branch information
seocam committed Oct 1, 2015
1 parent 601362e commit 31c7cbe
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions colab/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 _
Expand Down Expand Up @@ -139,16 +140,32 @@ class Meta:
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):
Expand Down

0 comments on commit 31c7cbe

Please sign in to comment.