|
| 1 | +from django import forms |
| 2 | +from django.core.validators import validate_email |
| 3 | + |
| 4 | +from models import * |
| 5 | +from ietf.secr.utils.mail import MultiEmailField |
| 6 | +from ietf.secr.utils.group import current_nomcom |
| 7 | + |
| 8 | +from ietf.message.models import Message |
| 9 | +from ietf.ietfauth.decorators import has_role |
| 10 | +from ietf.wgchairs.accounts import get_person_for_user |
| 11 | + |
| 12 | +# --------------------------------------------- |
| 13 | +# Globals |
| 14 | +# --------------------------------------------- |
| 15 | + |
| 16 | +ANNOUNCE_FROM_GROUPS = ['ietf','rsoc','iab',current_nomcom().acronym] |
| 17 | +ANNOUNCE_TO_GROUPS= ['ietf'] |
| 18 | + |
| 19 | +# this list isn't currently available as a Role query so it's hardcoded |
| 20 | +FROM_LIST = ('IETF Secretariat <ietf-secretariat@ietf.org>', |
| 21 | + 'IESG Secretary <iesg-secretary@ietf.org>', |
| 22 | + 'The IESG <iesg@ietf.org>', |
| 23 | + 'Internet-Drafts Administrator <internet-drafts@ietf.org>', |
| 24 | + 'IETF Agenda <agenda@ietf.org>', |
| 25 | + 'IETF Chair <chair@ietf.org>', |
| 26 | + 'IAB Chair <iab-chair@ietf.org> ', |
| 27 | + 'NomCom Chair <nomcom-chair@ietf.org>', |
| 28 | + 'IETF Registrar <ietf-registrar@ietf.org>', |
| 29 | + 'IETF Administrative Director <iad@ietf.org>', |
| 30 | + 'IETF Executive Director <exec-director@ietf.org>', |
| 31 | + 'The IAOC <bob.hinden@gmail.com>', |
| 32 | + 'The IETF Trust <tme@multicasttech.com>', |
| 33 | + 'RSOC Chair <rsoc-chair@iab.org>', |
| 34 | + 'ISOC Board of Trustees <eburger@standardstrack.com>', |
| 35 | + 'RFC Series Editor <rse@rfc-editor.org>') |
| 36 | + |
| 37 | +TO_LIST = ('IETF Announcement List <ietf-announce@ietf.org>', |
| 38 | + 'I-D Announcement List <i-d-announce@ietf.org>', |
| 39 | + 'The IESG <iesg@ietf.org>', |
| 40 | + 'Working Group Chairs <wgchairs@ietf.org>', |
| 41 | + 'BoF Chairs <bofchairs@ietf.org>', |
| 42 | + 'Other...') |
| 43 | +# --------------------------------------------- |
| 44 | +# Custom Fields |
| 45 | +# --------------------------------------------- |
| 46 | + |
| 47 | +class MultiEmailField(forms.Field): |
| 48 | + def to_python(self, value): |
| 49 | + "Normalize data to a list of strings." |
| 50 | + |
| 51 | + # Return an empty list if no input was given. |
| 52 | + if not value: |
| 53 | + return [] |
| 54 | + |
| 55 | + import types |
| 56 | + if isinstance(value, types.StringTypes): |
| 57 | + values = value.split(',') |
| 58 | + return [ x.strip() for x in values ] |
| 59 | + else: |
| 60 | + return value |
| 61 | + |
| 62 | + def validate(self, value): |
| 63 | + "Check if value consists only of valid emails." |
| 64 | + |
| 65 | + # Use the parent's handling of required fields, etc. |
| 66 | + super(MultiEmailField, self).validate(value) |
| 67 | + |
| 68 | + for email in value: |
| 69 | + validate_email(email) |
| 70 | + |
| 71 | +# --------------------------------------------- |
| 72 | +# Helper Functions |
| 73 | +# --------------------------------------------- |
| 74 | + |
| 75 | +def get_from_choices(user): |
| 76 | + ''' |
| 77 | + This function returns a choices tuple containing |
| 78 | + all the Announced From choices. Including |
| 79 | + leadership chairs and other entities. |
| 80 | + ''' |
| 81 | + person = user.get_profile() |
| 82 | + if has_role(user,'Secretariat'): |
| 83 | + f = FROM_LIST |
| 84 | + elif has_role(user,'IETF Chair'): |
| 85 | + f = (FROM_LIST[2],FROM_LIST[5]) |
| 86 | + elif has_role(user,'IAB Chair'): |
| 87 | + f = (FROM_LIST[6],) |
| 88 | + elif has_role(user,'IAD'): |
| 89 | + f = (FROM_LIST[9],) |
| 90 | + # NomCom, RSOC Chair, IAOC Chair aren't supported by has_role() |
| 91 | + elif Role.objects.filter(name="chair", |
| 92 | + group__acronym__startswith="nomcom", |
| 93 | + group__state="active", |
| 94 | + group__type="ietf", |
| 95 | + person=person): |
| 96 | + f = (FROM_LIST[7],) |
| 97 | + elif Role.objects.filter(person=person, |
| 98 | + group__acronym='rsoc', |
| 99 | + name="chair"): |
| 100 | + f = (FROM_LIST[13],) |
| 101 | + elif Role.objects.filter(person=person, |
| 102 | + group__acronym='iaoc', |
| 103 | + name="chair"): |
| 104 | + f = (FROM_LIST[11],) |
| 105 | + elif Role.objects.filter(person=person, |
| 106 | + group__acronym='rse', |
| 107 | + name="chair"): |
| 108 | + f = (FROM_LIST[15],) |
| 109 | + return zip(f,f) |
| 110 | + |
| 111 | +def get_to_choices(): |
| 112 | + #groups = Group.objects.filter(acronym__in=ANNOUNCE_TO_GROUPS) |
| 113 | + #roles = Role.objects.filter(group__in=(groups),name="Announce") |
| 114 | + #choices = [ (r.email, r.person.name) for r in roles ] |
| 115 | + #choices.append(('Other...','Other...'),) |
| 116 | + return zip(TO_LIST,TO_LIST) |
| 117 | + |
| 118 | +# --------------------------------------------- |
| 119 | +# Select Choices |
| 120 | +# --------------------------------------------- |
| 121 | +#TO_CHOICES = tuple(AnnouncedTo.objects.values_list('announced_to_id','announced_to')) |
| 122 | +TO_CHOICES = get_to_choices() |
| 123 | +#FROM_CHOICES = get_from_choices() |
| 124 | + |
| 125 | +# --------------------------------------------- |
| 126 | +# Forms |
| 127 | +# --------------------------------------------- |
| 128 | + |
| 129 | +class AnnounceForm(forms.ModelForm): |
| 130 | + nomcom = forms.BooleanField(required=False) |
| 131 | + to_custom = MultiEmailField(required=False,label='') |
| 132 | + #cc = MultiEmailField(required=False) |
| 133 | + |
| 134 | + class Meta: |
| 135 | + model = Message |
| 136 | + fields = ('nomcom', 'to','to_custom','frm','cc','bcc','reply_to','subject','body') |
| 137 | + |
| 138 | + def __init__(self, *args, **kwargs): |
| 139 | + user = kwargs.pop('user') |
| 140 | + super(AnnounceForm, self).__init__(*args, **kwargs) |
| 141 | + self.fields['to'].widget = forms.Select(choices=TO_CHOICES) |
| 142 | + self.fields['to'].help_text = 'Select name OR select Other... and enter email below' |
| 143 | + self.fields['cc'].help_text = 'Use comma separated lists for emails (Cc, Bcc, Reply To)' |
| 144 | + self.fields['frm'].widget = forms.Select(choices=get_from_choices(user)) |
| 145 | + self.fields['frm'].label = 'From' |
| 146 | + self.fields['nomcom'].label = 'NomCom message?' |
| 147 | + |
| 148 | + def clean(self): |
| 149 | + super(AnnounceForm, self).clean() |
| 150 | + data = self.cleaned_data |
| 151 | + if self.errors: |
| 152 | + return self.cleaned_data |
| 153 | + if data['to'] == 'Other...' and not data['to_custom']: |
| 154 | + raise forms.ValidationError('You must enter a "To" email address') |
| 155 | + |
| 156 | + return data |
| 157 | + |
| 158 | + def save(self, *args, **kwargs): |
| 159 | + user = kwargs.pop('user') |
| 160 | + message = super(AnnounceForm, self).save(commit=False) |
| 161 | + message.by = get_person_for_user(user) |
| 162 | + if self.cleaned_data['to'] == 'Other...': |
| 163 | + message.to = self.cleaned_data['to_custom'] |
| 164 | + if kwargs['commit']: |
| 165 | + message.save() |
| 166 | + |
| 167 | + # add nomcom to related groups if checked |
| 168 | + if self.cleaned_data.get('nomcom', False): |
| 169 | + nomcom = current_nomcom() |
| 170 | + message.related_groups.add(nomcom) |
| 171 | + |
| 172 | + return message |
0 commit comments