Debian Codegraph @

adjutant_ui/content/email/forms.py

raw
1 # Copyright 2013 Centrin Data Systems Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from django.forms import ValidationError
16 from django.utils.translation import gettext_lazy as _
17
18 from horizon import forms
19 from horizon import messages
20
21 from adjutant_ui import api
22
23
24 class EmailForm(forms.SelfHandlingForm):
25 new_email = forms.EmailField(
26 label=_("New email address"),
27 required=True)
28
29 confirm_email = forms.CharField(
30 label=_("Confirm email address"),
31 required=True)
32 no_autocomplete = True
33
34 def clean(self):
35 '''Check to make sure email fields match.'''
36 data = super(forms.Form, self).clean()
37 if data.get('new_email') != data.get('confirm_email', None):
38 raise ValidationError(_('Email addresses do not match.'))
39 return data
40
41 def handle(self, request, data):
42 try:
43 response = api.adjutant.email_update(request, data['new_email'])
44 if response.status_code == 202:
45 msg = _("Confirmation email sent to %s.")
46 messages.success(request, msg % data['new_email'])
47 elif response.status_code == 400:
48 messages.warning(request, _(
49 'Unable to update email. May already be in use.'))
50 else:
51 messages.error(request, _('Failed to update email.'))
52 return True
53 except Exception as e:
54 messages.error(request, _('Failed to update email. %s' % str(e)))
55 return False