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