|
1 | | -from forms import NonWgStep1, ListReqStep1, PickApprover, DeletionPickApprover, UrlMultiWidget |
| 1 | +from forms import NonWgStep1, ListReqStep1, PickApprover, DeletionPickApprover, UrlMultiWidget, Preview |
2 | 2 | from models import NonWgMailingList |
3 | | -from ietf.idtracker.models import Areas |
| 3 | +from ietf.idtracker.models import Areas, PersonOrOrgInfo |
4 | 4 | from django import newforms as forms |
5 | 5 | from django.shortcuts import render_to_response |
| 6 | +from django.template import RequestContext |
6 | 7 | from ietf.contrib import wizard, form_decorator |
| 8 | +from ietf.utils.mail import send_mail |
| 9 | + |
| 10 | +def formchoice(form, field): |
| 11 | + if not(form.is_valid()): |
| 12 | + return None |
| 13 | + d = str(form.clean_data[field]) |
| 14 | + for k, v in form.fields[field].choices: |
| 15 | + if str(k) == d: |
| 16 | + return v |
| 17 | + # oddly, one of the forms stores the translated value |
| 18 | + # in clean_data; the other stores the key. This second |
| 19 | + # if wouldn't be needed if both stored the key. |
| 20 | + # This whole function wouldn't be needed if both stored |
| 21 | + # the value. |
| 22 | + if str(v) == d: |
| 23 | + return v |
| 24 | + return None |
7 | 25 |
|
8 | 26 | nonwg_fields = { |
9 | 27 | 'id': None, |
@@ -41,37 +59,64 @@ def __init__(self, *args, **kwargs): |
41 | 59 | return BoundApproval |
42 | 60 |
|
43 | 61 | class NonWgWizard(wizard.Wizard): |
44 | | - form0 = None |
| 62 | + clean_forms = [] |
45 | 63 | def get_template(self): |
46 | 64 | templates = [] |
47 | | - if self.form0: |
48 | | - action = {'add': 'addedit', 'edit': 'addedit', 'delete': 'delete'}[self.form0.clean_data['add_edit']] |
| 65 | + if self.step > 0: |
| 66 | + action = {'add': 'addedit', 'edit': 'addedit', 'delete': 'delete'}[self.clean_forms[0].clean_data['add_edit']] |
49 | 67 | templates.append("mailinglists/nwg_wizard_%s_step%d.html" % (action, self.step)) |
50 | 68 | templates.append("mailinglists/nwg_wizard_%s.html" % (action)) |
51 | 69 | templates.append("mailinglists/nwg_wizard_step%d.html" % (self.step)) |
52 | 70 | templates.append("mailinglists/nwg_wizard.html") |
53 | 71 | return templates |
| 72 | + def render_template(self, *args, **kwargs): |
| 73 | + self.extra_context['clean_forms'] = self.clean_forms |
| 74 | + if self.step == 3: |
| 75 | + form0 = self.clean_forms[0] |
| 76 | + add_edit = form0.clean_data['add_edit'] |
| 77 | + if add_edit == 'add' or add_edit == 'edit': |
| 78 | + # Can't get the choice mapping directly from the form |
| 79 | + self.extra_context['area'] = formchoice(self.clean_forms[1], 'area') |
| 80 | + self.extra_context['approver'] = formchoice(self.clean_forms[2], 'approver') |
| 81 | + print "formchoice for area = %s" % formchoice(self.clean_forms[1], 'area') |
| 82 | + else: |
| 83 | + print "add_edit = %s" % add_edit |
| 84 | + return super(NonWgWizard, self).render_template(*args, **kwargs) |
54 | 85 | def failed_hash(self, step): |
55 | 86 | raise NotImplementedError("step %d hash failed" % step) |
56 | 87 | def process_step(self, request, form, step): |
57 | 88 | form.full_clean() |
58 | 89 | if step == 0: |
59 | | - self.form0 = form |
| 90 | + self.clean_forms = [ form ] |
60 | 91 | if form.clean_data['add_edit'] == 'add': |
61 | 92 | self.form_list.append(forms.form_for_model(NonWgMailingList, formfield_callback=nonwg_callback)) |
62 | 93 | elif form.clean_data['add_edit'] == 'edit': |
63 | 94 | self.form_list.append(forms.form_for_instance(NonWgMailingList.objects.get(pk=form.clean_data['list_id']), formfield_callback=nonwg_callback)) |
64 | 95 | elif form.clean_data['add_edit'] == 'delete': |
65 | 96 | list = NonWgMailingList.objects.get(pk=form.clean_data['list_id_delete']) |
66 | 97 | self.form_list.append(gen_approval([ad.person_id for ad in list.area.areadirectors_set.all()], DeletionPickApprover)) |
| 98 | + self.form_list.append(Preview) |
| 99 | + else: |
| 100 | + self.clean_forms.append(form) |
67 | 101 | if step == 1: |
68 | | - form0 = self.get_form(0, request.POST) |
69 | | - form0.full_clean() |
70 | | - self.form0 = form0 |
| 102 | + form0 = self.clean_forms[0] |
71 | 103 | add_edit = form0.clean_data['add_edit'] |
72 | 104 | if add_edit == 'add' or add_edit == 'edit': |
73 | 105 | self.form_list.append(gen_approval([ad.person_id for ad in Areas.objects.get(area_acronym=form.clean_data['area']).areadirectors_set.all()], PickApprover)) |
| 106 | + self.form_list.append(Preview) |
74 | 107 | super(NonWgWizard, self).process_step(request, form, step) |
| 108 | + def done(self, request, form_list): |
| 109 | + add_edit = self.clean_forms[0].clean_data['add_edit'] |
| 110 | + # save row to database properly |
| 111 | + if add_edit == 'add' or add_edit == 'edit': |
| 112 | + template = 'mailinglists/nwg_addedit_email.txt' |
| 113 | + approver = self.clean_forms[2].clean_data['approver'] |
| 114 | + else: |
| 115 | + template = 'mailinglists/nwg_delete_email.txt' |
| 116 | + approver = self.clean_forms[1].clean_data['approver'] |
| 117 | + approver_email = PersonOrOrgInfo.objects.get(pk=approver).email() |
| 118 | + send_mail(request, [ approver_email ], None, 'Request to %s on the Non-WG Mailing List Web Page' % add_edit, template, {'forms': self.clean_forms}) |
| 119 | + return render_to_response( 'mailinglists/nwg_wizard_done.html', {'forms': self.clean_forms}, context_instance=RequestContext(request) ) |
75 | 120 |
|
76 | 121 | def non_wg_wizard(request): |
77 | 122 | wiz = NonWgWizard([ NonWgStep1 ]) |
|
0 commit comments