| 1 |
# Copyright (c) 2016 Catalyst IT Ltd. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain 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, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from django.conf import settings |
| 16 |
from django import forms |
| 17 |
from django import http |
| 18 |
from django.utils.translation import gettext_lazy as _ |
| 19 |
|
| 20 |
from horizon import forms as hforms |
| 21 |
from horizon.utils import functions as utils |
| 22 |
|
| 23 |
from adjutant_ui.api import adjutant |
| 24 |
|
| 25 |
# Username is ignored, use email address instead. |
| 26 |
USERNAME_IS_EMAIL = True |
| 27 |
|
| 28 |
|
| 29 |
class ForgotPasswordForm(hforms.SelfHandlingForm): |
| 30 |
username = forms.CharField( |
| 31 |
max_length=255, label=_("User Name"), |
| 32 |
widget=forms.TextInput(attrs={"autofocus": "autofocus"})) |
| 33 |
email = forms.EmailField(label=_("Email")) |
| 34 |
|
| 35 |
def __init__(self, *args, **kwargs): |
| 36 |
super(ForgotPasswordForm, self).__init__(*args, **kwargs) |
| 37 |
if (hasattr(settings, 'USERNAME_IS_EMAIL') and |
| 38 |
getattr(settings, 'USERNAME_IS_EMAIL')): |
| 39 |
self.fields.pop('username') |
| 40 |
self.fields['email'].widget = forms.TextInput( |
| 41 |
attrs={"autofocus": "autofocus"}) |
| 42 |
|
| 43 |
def clean(self, *args, **kwargs): |
| 44 |
# validate username and email? |
| 45 |
return super(ForgotPasswordForm, self).clean(*args, **kwargs) |
| 46 |
|
| 47 |
def handle(self, request, data): |
| 48 |
try: |
| 49 |
submit_response = adjutant.forgotpassword_submit( |
| 50 |
request, data) |
| 51 |
if submit_response.ok: |
| 52 |
return True |
| 53 |
except Exception: |
| 54 |
pass |
| 55 |
|
| 56 |
# Send the user back to the login page. |
| 57 |
msg = _("The password reset service is currently unavailable. " |
| 58 |
"Please try again later.") |
| 59 |
response = http.HttpResponseRedirect(settings.LOGOUT_URL) |
| 60 |
utils.add_logout_reason(self.request, response, msg) |
| 61 |
return response |