9a956083f6db194f6132fc0afb3ce6e96e59832a
[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.auth import update_session_auth_hash
5 from django.contrib import messages
6 from django.shortcuts import render
7 from django.utils.translation import ugettext as _
8 from django.views.decorators.http import require_POST
9 from accounts.forms import UserBasicForm, UserPasswordForm
10 from .models import Service
11
12 @login_required
13 def account_profile(request, basic_form=None, pass_form=None):
14     return render(request, "account/profile.html", {
15         "basic_form": basic_form or UserBasicForm(instance=request.user),
16         "pass_form": pass_form or UserPasswordForm(),
17         "services": Service.objects.all(),
18     })
19
20
21 @require_POST
22 @login_required
23 def account_change_basic_profile(request):
24     form = UserBasicForm(request.POST, instance=request.user)
25
26     if form.is_valid():
27         form.save()
28         messages.add_message(request, messages.INFO, _("Profile has been changed."))
29         return http.HttpResponseRedirect('/accounts/')
30
31     return account_profile(request, basic_form=form)
32
33 @require_POST
34 @login_required
35 def account_change_password(request):
36     form = UserPasswordForm(request.POST)
37
38     if form.is_valid():
39         request.user.set_password(form.cleaned_data['new_password'])
40         request.user.save()
41         update_session_auth_hash(request, request.user)
42
43         messages.add_message(request, messages.INFO, _("Password has been changed."))
44         return http.HttpResponseRedirect('/accounts/')
45
46     return account_profile(request, pass_form=form)