Skip to content

Commit 463182d

Browse files
author
Michael Lee
committed
Update ticket 205
* remove clutter from previous commit * if we get rid of 'none' from the drop down and use '--------' instead, then we can even remove LooseModelChoiceField and simply use django's ModelChoiceField. However, it may be confused in user's perspective. - Legacy-Id: 953
1 parent d143d41 commit 463182d

4 files changed

Lines changed: 53 additions & 97 deletions

File tree

ietf/mailinglists/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

3-
# for area code for Non-WG-Mailinglists
4-
CODE_AREA = {
5-
"none" : "-100",
6-
}
7-

ietf/mailinglists/forms.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

33
from django import newforms as forms
4+
from django.newforms.models import QuerySetIterator, ModelChoiceField
5+
from django.newforms.fields import Field
46
from models import NonWgMailingList, ImportedMailingList
57
from ietf.idtracker.models import PersonOrOrgInfo, IETFWG
6-
from ietf.mailinglists import CODE_AREA
78
import re
89

910
class NonWgStep1(forms.Form):
@@ -222,11 +223,34 @@ def clean(self, value):
222223
class ApprovalComment(forms.Form):
223224
add_comment = forms.CharField(label="Approver's comments to the requestor (will be emailed to the requestor)", widget=forms.Textarea(attrs={'cols':41, 'rows': 4}))
224225

226+
class LooseModelChoiceField (ModelChoiceField) :
227+
def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
228+
required=True, widget=forms.Select, label=None, initial=None,
229+
help_text=None):
225230

226-
class SelectWidgetArea (forms.Select) :
227-
def render(self, name, value, attrs=None, choices=()) :
228-
choices = ((CODE_AREA["none"], "none", ), )
229-
return super(SelectWidgetArea, self).render(name, value, attrs, choices)
231+
super(LooseModelChoiceField, self).__init__(queryset, empty_label, cache_choices, required, widget, label, initial, help_text)
232+
233+
def _get_choices(self):
234+
return [i for i in super(LooseModelChoiceField, self)._get_choices()] + [("none", "none", )]
235+
236+
def _set_choices (self, value) :
237+
return super(LooseModelChoiceField, self)._set_choices(value)
238+
239+
choices = property(_get_choices, _set_choices)
240+
241+
def clean(self, value):
242+
Field.clean(self, value)
243+
if value in ('', None):
244+
return None
245+
246+
if value == "none" :
247+
return value
248+
249+
try:
250+
value = self.queryset.model._default_manager.get(pk=value)
251+
except self.queryset.model.DoesNotExist:
252+
raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
253+
return value
230254

231255

232256

ietf/mailinglists/models.py

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

33
from django.db import models
4-
from django.db.models.fields.related import ManyToOneRel
5-
from django import newforms as forms
6-
from django.newforms import Field, util
7-
from django.utils.text import capfirst
8-
from django.utils.translation import gettext
9-
104
from ietf.idtracker.models import Acronym, Area, PersonOrOrgInfo
115
from ietf.idtracker.models import Role
12-
from ietf.mailinglists import CODE_AREA
13-
146
import random
157
from datetime import datetime
168

@@ -128,48 +120,6 @@ class Meta:
128120
class Admin:
129121
pass
130122

131-
class AreaPsuedo (object) :
132-
"""
133-
Psuedo class for Area model.
134-
This only works when add new NonWgMailingList entry.
135-
"""
136-
area_acronym = CODE_AREA["none"]
137-
area_acronym_id = CODE_AREA["none"]
138-
start_date = datetime.now()
139-
concluded_date = datetime.now()
140-
status = 1
141-
comments = str()
142-
last_modified_date = datetime.now()
143-
extra_email_addresses = str()
144-
145-
def __str__ (self) :
146-
return CODE_AREA["none"]
147-
148-
class ModelChoiceFieldArea (forms.ModelChoiceField) :
149-
def clean(self, value):
150-
Field.clean(self, value)
151-
if value in ('', None):
152-
return None
153-
154-
if value in CODE_AREA.values() :
155-
return AreaPsuedo()
156-
else :
157-
try:
158-
value = self.queryset.model._default_manager.get(pk=value)
159-
except self.queryset.model.DoesNotExist:
160-
raise util.ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
161-
162-
return value
163-
164-
165-
class ForeignKeyArea (models.ForeignKey) :
166-
167-
def formfield(self, **kwargs):
168-
defaults = {'queryset': self.rel.to._default_manager.all(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
169-
defaults.update(kwargs)
170-
return ModelChoiceFieldArea(**defaults)
171-
172-
173123
class NonWgMailingList(models.Model):
174124
id = models.CharField(primary_key=True, maxlength=35)
175125
s_name = models.CharField("Submitter's Name", blank=True, maxlength=255)
@@ -178,7 +128,7 @@ class NonWgMailingList(models.Model):
178128
list_url = models.CharField("List URL", maxlength=255)
179129
admin = models.TextField("Administrator(s)' Email Address(es)", blank=True)
180130
purpose = models.TextField(blank=True)
181-
area = ForeignKeyArea(Area, db_column='area_acronym_id', null=True)
131+
area = models.ForeignKey(Area, db_column='area_acronym_id', null=True)
182132
subscribe_url = models.CharField("Subscribe URL", blank=True, maxlength=255)
183133
subscribe_other = models.TextField("Subscribe Other", blank=True)
184134
# Can be 0, 1, -1, or what looks like a person_or_org_tag, positive or neg.
@@ -202,15 +152,6 @@ def save(self, *args, **kwargs):
202152
def choices():
203153
return [(list.id, list.list_name) for list in NonWgMailingList.objects.all().filter(status__gt=0)]
204154
choices = staticmethod(choices)
205-
206-
def __setattr__ (self, name, value) :
207-
if name == "area_id" and str(value) in CODE_AREA.values() :
208-
for k, v in CODE_AREA.iteritems() :
209-
if v == str(value) :
210-
setattr(self, "_area_cache", k)
211-
212-
super(NonWgMailingList, self).__setattr__(name,value)
213-
214155
class Meta:
215156
db_table = 'none_wg_mailing_list'
216157
ordering = ['list_name']

ietf/mailinglists/views.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Copyright The IETF Trust 2007, All Rights Reserved
22

3-
from forms import NonWgStep1, ListReqStep1, PickApprover, DeletionPickApprover, UrlMultiWidget, Preview, ListReqAuthorized, ListReqClose, MultiEmailField, AdminRequestor, ApprovalComment, ListApprover, SelectWidgetArea
3+
from forms import NonWgStep1, ListReqStep1, PickApprover, DeletionPickApprover, UrlMultiWidget, Preview, ListReqAuthorized, ListReqClose, MultiEmailField, AdminRequestor, ApprovalComment, ListApprover, LooseModelChoiceField
44
from models import NonWgMailingList, MailingList, Domain
55
from ietf.idtracker.models import Area, PersonOrOrgInfo, AreaDirector, WGChair, Role
6-
from ietf.mailinglists import CODE_AREA
76
from django import newforms as forms
87
from django.shortcuts import get_object_or_404, render_to_response
98
from django.template import RequestContext
@@ -13,15 +12,15 @@
1312
from ietf.utils.mail import send_mail_subj
1413
from datetime import datetime
1514

15+
def get_approvers_from_area (area_id) :
16+
if area_id == "none" :
17+
return [ad.person_id for ad in Role.objects.filter(role_name__in=("IETF", "IAB", ))]
18+
else :
19+
return [ad.person_id for ad in Area.objects.get(area_acronym=area_id).areadirector_set.all()]
20+
1621
def formchoice(form, field):
1722
if not(form.is_valid()):
1823
return None
19-
20-
if field == "area" and str(form.clean_data[field]) in CODE_AREA.values() :
21-
for k, v in CODE_AREA.iteritems() :
22-
if v == str(form.clean_data[field]) :
23-
return k
24-
2524
d = str(form.clean_data[field])
2625
for k, v in form.fields[field].choices:
2726
if str(k) == d:
@@ -56,14 +55,12 @@ def formchoice(form, field):
5655
'purpose': forms.Textarea(attrs = {'rows': 4, 'cols': 70}),
5756
'subscribe_url': UrlMultiWidget(choices=(('n/a', 'Not Applicable'), ('http://', 'http://'), ('https://', 'https://')), attrs = {'size': 50}),
5857
'subscribe_other': forms.Textarea(attrs = {'rows': 3, 'cols': 50}),
59-
'area' : SelectWidgetArea(),
6058
}
6159

6260
nonwg_querysets = {
63-
'area': Area.objects.filter(status=1)
61+
#'area': Area.objects.filter(status=1)
6462
}
6563

66-
nonwg_callback = form_decorator(fields=nonwg_fields, widgets=nonwg_widgets, attrs=nonwg_attrs, querysets=nonwg_querysets)
6764

6865
def gen_approval(approvers, parent):
6966
class BoundApproval(parent):
@@ -102,37 +99,33 @@ def render_template(self, *args, **kwargs):
10299
# def failed_hash(self, request, step):
103100
# raise NotImplementedError("step %d hash failed" % step)
104101
def process_step(self, request, form, step):
102+
105103
form.full_clean()
104+
106105
if step == 0:
107106
self.clean_forms = [ form ]
108107
if form.clean_data['add_edit'] == 'add':
108+
nonwg_fields["area"] = LooseModelChoiceField(Area.objects.filter(status=1))
109+
nonwg_callback = form_decorator(fields=nonwg_fields, widgets=nonwg_widgets, attrs=nonwg_attrs, querysets=nonwg_querysets)
110+
109111
self.form_list.append(forms.form_for_model(NonWgMailingList, formfield_callback=nonwg_callback))
110112
elif form.clean_data['add_edit'] == 'edit':
111-
self.form_list.append(forms.form_for_instance(NonWgMailingList.objects.get(pk=form.clean_data['list_id']), formfield_callback=nonwg_callback))
113+
list = NonWgMailingList.objects.get(pk=form.clean_data['list_id'])
114+
nonwg_fields["area"] = LooseModelChoiceField(Area.objects.filter(status=1), initial=list.area_id is None and "none" or list.area_id)
115+
nonwg_callback = form_decorator(fields=nonwg_fields, widgets=nonwg_widgets, attrs=nonwg_attrs, querysets=nonwg_querysets)
116+
117+
self.form_list.append(forms.form_for_instance(list, formfield_callback=nonwg_callback))
112118
elif form.clean_data['add_edit'] == 'delete':
113119
list = NonWgMailingList.objects.get(pk=form.clean_data['list_id_delete'])
114-
if str(list.area_id) in CODE_AREA.values() :
115-
__list_persons = [r.person.person_or_org_tag for r in Role.objects.filter(role_name__in=("IETF", "IAB"))]
116-
else :
117-
__list_persons = [ad.person_id for ad in list.area.areadirector_set.all()]
118-
119-
self.form_list.append(gen_approval(__list_persons, DeletionPickApprover))
120-
120+
self.form_list.append(gen_approval(get_approvers_from_area(list.area is None and "none" or list.area_id), DeletionPickApprover))
121121
self.form_list.append(Preview)
122122
else:
123123
self.clean_forms.append(form)
124124
if step == 1:
125125
form0 = self.clean_forms[0]
126126
add_edit = form0.clean_data['add_edit']
127127
if add_edit == 'add' or add_edit == 'edit':
128-
# if area value is in CODE_AREA, approval person must be in 'IETF' and 'IAB'.
129-
if str(form.clean_data["area"]) in CODE_AREA.values() :
130-
__list_persons = [r.person.person_or_org_tag for r in Role.objects.filter(role_name__in=("IETF", "IAB"))]
131-
else :
132-
__list_persons = [ad.person_id for ad in Area.objects.get(area_acronym=form.clean_data['area']).areadirector_set.all()]
133-
134-
self.form_list.append(gen_approval(__list_persons, PickApprover))
135-
128+
self.form_list.append(gen_approval(get_approvers_from_area(form.clean_data['area']), PickApprover))
136129
self.form_list.append(Preview)
137130
super(NonWgWizard, self).process_step(request, form, step)
138131
def done(self, request, form_list):
@@ -142,6 +135,9 @@ def done(self, request, form_list):
142135
if add_edit == 'add' or add_edit == 'edit':
143136
template = 'mailinglists/nwg_addedit_email.txt'
144137
approver = self.clean_forms[2].clean_data['approver']
138+
if self.clean_forms[1].clean_data["area"] == "none" :
139+
self.clean_forms[1].clean_data["area"] = None
140+
145141
list = NonWgMailingList(**self.clean_forms[1].clean_data)
146142
list.__dict__.update(self.clean_forms[2].clean_data)
147143
list.id = None # create a new row no matter what

0 commit comments

Comments
 (0)