df8365a2e9d2e3e1695b12c660f06c76073bbbd3
[cas.git] / src / accounts / views.py
1 # Create your views here.
2 from django import http
3 from django.contrib.auth.decorators import login_required
4 from django.contrib import messages
5 from django.shortcuts import render
6 from django.utils.translation import ugettext as __
7 from django.views.decorators.http import require_POST
8 from accounts.forms import UserBasicForm, UserPasswordForm
9
10 @login_required
11 def account_profile(request, basic_form=None, pass_form=None):
12     return render(request, "account/profile.html", {
13         "basic_form": basic_form or UserBasicForm(instance=request.user),
14         "pass_form": pass_form or UserPasswordForm(),
15     })
16
17
18 @require_POST
19 @login_required
20 def account_change_basic_profile(request):
21     form = UserBasicForm(request.POST, instance=request.user)
22
23     if form.is_valid():
24         form.save()
25         messages.add_message(request, messages.INFO, __("Profile has been changed."))
26         return http.HttpResponseRedirect('/accounts/')
27
28     return account_profile(request, basic_form=form)
29
30 @require_POST
31 @login_required
32 def account_change_password(request):
33     form = UserPasswordForm(request.POST)
34
35     if form.is_valid():
36         request.user.set_password(form.cleaned_data['new_password'])
37         request.user.save()
38
39         messages.add_message(request, messages.INFO, __("Password has been changed."))
40         return http.HttpResponseRedirect('/accounts/')
41
42     return account_profile(request, pass_form=form)