X-Git-Url: https://git.mdrn.pl/cas.git/blobdiff_plain/2c9debf2812dfbd4ff59348559ae57cd49858e8b..40ba7c29763bfe39154f8b6444bf8b3b4ad0c56c:/src/accounts/views.py diff --git a/src/accounts/views.py b/src/accounts/views.py index df8365a..2b049e4 100644 --- a/src/accounts/views.py +++ b/src/accounts/views.py @@ -1,42 +1,19 @@ -# Create your views here. -from django import http -from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib import messages -from django.shortcuts import render -from django.utils.translation import ugettext as __ -from django.views.decorators.http import require_POST -from accounts.forms import UserBasicForm, UserPasswordForm +from django.utils.translation import ugettext as _ +from django.views.generic.edit import UpdateView +from accounts.forms import UserBasicForm -@login_required -def account_profile(request, basic_form=None, pass_form=None): - return render(request, "account/profile.html", { - "basic_form": basic_form or UserBasicForm(instance=request.user), - "pass_form": pass_form or UserPasswordForm(), - }) +class ProfileView(LoginRequiredMixin, UpdateView): + form_class = UserBasicForm + template_name = "account/profile.html" + success_url = '/accounts/' -@require_POST -@login_required -def account_change_basic_profile(request): - form = UserBasicForm(request.POST, instance=request.user) + def get_object(self): + return self.request.user - if form.is_valid(): - form.save() - messages.add_message(request, messages.INFO, __("Profile has been changed.")) - return http.HttpResponseRedirect('/accounts/') - - return account_profile(request, basic_form=form) - -@require_POST -@login_required -def account_change_password(request): - form = UserPasswordForm(request.POST) - - if form.is_valid(): - request.user.set_password(form.cleaned_data['new_password']) - request.user.save() - - messages.add_message(request, messages.INFO, __("Password has been changed.")) - return http.HttpResponseRedirect('/accounts/') - - return account_profile(request, pass_form=form) + def form_valid(self, form): + retval = super().form_valid(form) + messages.add_message(self.request, messages.INFO, _("Profile has been changed.")) + return retval