def post(self, *args, **kwargs): """ Check old and new password match """ form = UserChangePasswordForm(self.request.POST, user=self.request.user) if form.is_valid(): form.save() return redirect('index') else: context = {} context['form'] = UserChangePasswordForm(self.request.POST, user=self.request.user) return render(self.request, self.template_name, context)
class UserSettings(TemplateView): template_name = 'users/settings.html' @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): action = request.POST.get('action') self.profile_settings_form = ProfileSettingsForm( (request.POST if action == 'profile' else None), (request.FILES if action == 'profile' else None), prefix='profile', instance=request.user ) self.user_change_password = UserChangePasswordForm(request.user, (request.POST if action == 'password' else None), prefix='password') self.user_change_email = UserChangeEmailForm(request.user, (request.POST if action == 'email' else None), prefix='email') return super(UserSettings, self).dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(UserSettings, self).get_context_data(**kwargs) context['profile_settings_form'] = self.profile_settings_form context['user_change_password'] = self.user_change_password context['user_change_email'] = self.user_change_email return context def post(self, request, *args, **kwargs): if self.profile_settings_form.is_valid(): self.profile_settings_form.save() messages.success(request, _(u'Дані успішно змінені та збережені')) return redirect(request.path) elif self.user_change_password.is_valid(): self.user_change_password.save() request.user.backend = request.session[BACKEND_SESSION_KEY] login(request, request.user) messages.success(request, _(u'Пароль успішно змінений')) return redirect(request.path) elif self.user_change_email.is_valid(): self.user_change_email.save() messages.success(request, _(u'Email успішно змінений та збережений')) return redirect(request.path) return self.get(request, *args, **kwargs)