2018-12-10 15:58:35 +00:00
|
|
|
"""passbook core user views"""
|
|
|
|
from django.contrib import messages
|
2019-02-23 19:56:41 +00:00
|
|
|
from django.contrib.auth import logout, update_session_auth_hash
|
2019-02-27 14:09:05 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2019-02-26 14:40:58 +00:00
|
|
|
from django.forms.utils import ErrorList
|
2019-02-23 19:56:41 +00:00
|
|
|
from django.shortcuts import redirect, reverse
|
2019-02-27 14:09:05 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-12-10 15:58:35 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2019-02-23 19:56:41 +00:00
|
|
|
from django.views.generic import DeleteView, FormView, UpdateView
|
2018-12-10 15:58:35 +00:00
|
|
|
|
2019-02-26 14:40:58 +00:00
|
|
|
from passbook.core.exceptions import PasswordPolicyInvalid
|
2019-02-23 19:56:41 +00:00
|
|
|
from passbook.core.forms.users import PasswordChangeForm, UserDetailForm
|
|
|
|
from passbook.lib.config import CONFIG
|
2018-12-10 15:58:35 +00:00
|
|
|
|
|
|
|
|
2019-02-27 14:09:05 +00:00
|
|
|
class UserSettingsView(SuccessMessageMixin, UpdateView):
|
2018-12-10 15:58:35 +00:00
|
|
|
"""Update User settings"""
|
2019-02-26 09:57:05 +00:00
|
|
|
|
2018-12-10 15:58:35 +00:00
|
|
|
template_name = 'user/settings.html'
|
|
|
|
form_class = UserDetailForm
|
|
|
|
|
2019-02-27 14:09:05 +00:00
|
|
|
success_message = _('Successfully updated user.')
|
|
|
|
success_url = reverse_lazy('passbook_core:user-settings')
|
|
|
|
|
2018-12-10 15:58:35 +00:00
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
class UserDeleteView(DeleteView):
|
|
|
|
"""Delete user account"""
|
|
|
|
|
|
|
|
template_name = 'generic/delete.html'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
messages.success(self.request, _('Successfully deleted user.'))
|
|
|
|
logout(self.request)
|
|
|
|
return reverse('passbook_core:auth-login')
|
2019-02-23 19:56:41 +00:00
|
|
|
|
|
|
|
class UserChangePasswordView(FormView):
|
|
|
|
"""View for users to update their password"""
|
|
|
|
|
|
|
|
form_class = PasswordChangeForm
|
|
|
|
template_name = 'login/form_with_user.html'
|
|
|
|
|
|
|
|
def form_valid(self, form: PasswordChangeForm):
|
2019-02-26 14:40:58 +00:00
|
|
|
try:
|
|
|
|
self.request.user.set_password(form.cleaned_data.get('password'))
|
|
|
|
self.request.user.save()
|
|
|
|
update_session_auth_hash(self.request, self.request.user)
|
|
|
|
messages.success(self.request, _('Successfully changed password'))
|
|
|
|
except PasswordPolicyInvalid as exc:
|
|
|
|
# Manually inject error into form
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
errors = form._errors.setdefault("password_repeat", ErrorList(''))
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
errors = form._errors.setdefault("password", ErrorList())
|
|
|
|
for error in exc.messages:
|
|
|
|
errors.append(error)
|
|
|
|
return self.form_invalid(form)
|
2019-02-23 19:56:41 +00:00
|
|
|
return redirect('passbook_core:overview')
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs['config'] = CONFIG.get('passbook')
|
|
|
|
kwargs['is_login'] = True
|
|
|
|
kwargs['title'] = _('Change Password')
|
|
|
|
kwargs['primary_action'] = _('Change')
|
|
|
|
return super().get_context_data(**kwargs)
|