|
5 | 5 | from django.core.urlresolvers import reverse |
6 | 6 | from django.conf import settings |
7 | 7 |
|
| 8 | +from ietf.utils import unaccent |
8 | 9 | from ietf.ietfauth.decorators import has_role |
9 | 10 | from ietf.utils import fields as custom_fields |
10 | 11 | from ietf.group.models import Group, Role |
11 | | -from ietf.name.models import RoleName |
12 | | -from ietf.person.models import Email |
13 | | -from ietf.nomcom.models import NomCom |
| 12 | +from ietf.name.models import RoleName, FeedbackType |
| 13 | +from ietf.person.models import Email, Person |
| 14 | +from ietf.nomcom.models import NomCom, Nomination, Nominee, NomineePosition, \ |
| 15 | + Position, Feedback |
14 | 16 |
|
15 | 17 |
|
16 | 18 | ROLODEX_URL = getattr(settings, 'ROLODEX_URL', None) |
@@ -148,3 +150,59 @@ class EditPublicKeyForm(forms.ModelForm): |
148 | 150 | class Meta: |
149 | 151 | model = NomCom |
150 | 152 | fields = ('public_key',) |
| 153 | + |
| 154 | + |
| 155 | +class NominateForm(forms.ModelForm): |
| 156 | + comments = forms.CharField(label='Comments', widget=forms.Textarea()) |
| 157 | + |
| 158 | + def __init__(self, *args, **kwargs): |
| 159 | + self.nomcom = kwargs.pop('nomcom', None) |
| 160 | + self.user = kwargs.pop('user', None) |
| 161 | + super(NominateForm, self).__init__(*args, **kwargs) |
| 162 | + if self.nomcom: |
| 163 | + self.fields['position'].queryset = Position.objects.filter(nomcom=self.nomcom) |
| 164 | + |
| 165 | + def save(self, commit=True): |
| 166 | + # Create nomination |
| 167 | + nomination = super(NominateForm, self).save(commit=False) |
| 168 | + candidate_email = self.cleaned_data['candidate_email'] |
| 169 | + candidate_name = self.cleaned_data['candidate_name'] |
| 170 | + position = self.cleaned_data['position'] |
| 171 | + comments = self.cleaned_data['comments'] |
| 172 | + |
| 173 | + # Create person and email if candidate email does't exist and send email |
| 174 | + email, created = Email.objects.get_or_create(address=candidate_email) |
| 175 | + if created: |
| 176 | + email.person = Person.objects.create(name=candidate_name, |
| 177 | + ascii=unaccent.asciify(candidate_name), |
| 178 | + address=candidate_email) |
| 179 | + email.save() |
| 180 | + |
| 181 | + # Add the nomination for a particular position |
| 182 | + nominee, created = Nominee.objects.get_or_create(email=email) |
| 183 | + NomineePosition.objects.get_or_create(position=position, nominee=nominee) |
| 184 | + |
| 185 | + # Complete nomination data |
| 186 | + author_emails = Email.objects.filter(person__user=self.user) |
| 187 | + author = author_emails and author_emails[0] or None |
| 188 | + feedback = Feedback.objects.create(position=position, |
| 189 | + nominee=nominee, |
| 190 | + comments=comments, |
| 191 | + type=FeedbackType.objects.get(slug='nomina')) |
| 192 | + if author: |
| 193 | + feedback.author = author |
| 194 | + feedback.save() |
| 195 | + |
| 196 | + nomination.nominee = nominee |
| 197 | + nomination.comments = feedback |
| 198 | + |
| 199 | + if commit: |
| 200 | + nomination.save() |
| 201 | + |
| 202 | + # TODO: send mail to chair and secretariat with the new person |
| 203 | + # TODO: send mails about nominations |
| 204 | + return nomination |
| 205 | + |
| 206 | + class Meta: |
| 207 | + model = Nomination |
| 208 | + fields = ('position', 'candidate_name', 'candidate_email', 'candidate_phone') |
0 commit comments