Commit 31c7cbea016b711b6042f736ff59b0f1015f3a6a
1 parent
601362e6
Exists in
master
and in
14 other branches
Used lazy choices for display list names
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>
Showing
1 changed file
with
26 additions
and
9 deletions
Show diff stats
colab/accounts/forms.py
| ... | ... | @@ -11,6 +11,7 @@ from django.contrib.sites.shortcuts import get_current_site |
| 11 | 11 | from django.core.urlresolvers import reverse |
| 12 | 12 | from django.template import loader |
| 13 | 13 | from django.utils.encoding import force_bytes |
| 14 | +from django.utils.functional import lazy | |
| 14 | 15 | from django.utils.http import urlsafe_base64_encode |
| 15 | 16 | from django.utils.text import capfirst |
| 16 | 17 | from django.utils.translation import ugettext_lazy as _ |
| ... | ... | @@ -139,16 +140,32 @@ class UserUpdateForm(UserForm): |
| 139 | 140 | required=False) |
| 140 | 141 | |
| 141 | 142 | |
| 143 | +def get_lists_choices(): | |
| 144 | + lists_names = [] | |
| 145 | + for mlist in mailman.all_lists(): | |
| 146 | + name = mlist.get('listname') | |
| 147 | + desc = mlist.get('description') | |
| 148 | + formatted_desc = u'{} ({})'.format(name, desc) | |
| 149 | + lists_names.append((name, formatted_desc)) | |
| 150 | + return lists_names | |
| 151 | + | |
| 152 | + | |
| 153 | +# XXX: This field is no longer required when using django 1.8 | |
| 154 | +class MultipleChoiceFieldLazy(forms.MultipleChoiceField): | |
| 155 | + def _set_choices(self, value): | |
| 156 | + self._choices = self.widget.choices = value | |
| 157 | + | |
| 158 | + def _get_choices(self): | |
| 159 | + return list(self._choices) | |
| 160 | + | |
| 161 | + choices = property(_get_choices, _set_choices) | |
| 162 | + | |
| 163 | + | |
| 142 | 164 | class ListsForm(forms.Form): |
| 143 | - LISTS_NAMES = (( | |
| 144 | - mlist.get('listname'), u'{} ({})'.format(mlist.get('listname'), | |
| 145 | - mlist.get('description')) | |
| 146 | - ) for mlist in mailman.all_lists()) | |
| 147 | - | |
| 148 | - lists = forms.MultipleChoiceField(label=_(u'Mailing lists'), | |
| 149 | - required=False, | |
| 150 | - widget=forms.CheckboxSelectMultiple, | |
| 151 | - choices=LISTS_NAMES) | |
| 165 | + lists = MultipleChoiceFieldLazy(label=_(u'Mailing lists'), | |
| 166 | + required=False, | |
| 167 | + widget=forms.CheckboxSelectMultiple, | |
| 168 | + choices=lazy(get_lists_choices, list)()) | |
| 152 | 169 | |
| 153 | 170 | |
| 154 | 171 | class UserCreationForm(UserForm): | ... | ... |