Commit 00f306dc by Kálmán Viktor

dashboard: change password on profile page

parent ddffdf96
......@@ -3,6 +3,7 @@ from datetime import timedelta
from django.contrib.auth.models import User
from django.contrib.auth.forms import (
AuthenticationForm, PasswordResetForm, SetPasswordForm,
from crispy_forms.helper import FormHelper
......@@ -1013,9 +1014,20 @@ class MyProfileForm(forms.ModelForm):
def helper(self):
helper = FormHelper()
helper.layout = Layout('preferred_language', )
helper.add_input(Submit("submit", _("Save")))
helper.add_input(Submit("submit", _("Change language")))
return helper
def save(self, *args, **kwargs):
value = super(MyProfileForm, self).save(*args, **kwargs)
return value
class CirclePasswordChangeForm(PasswordChangeForm):
def helper(self):
helper = FormHelper()
helper.add_input(Submit("submit", _("Change password"),
css_class="btn btn-primary",
return helper
......@@ -14,7 +14,20 @@
<h3 class="no-margin"><i class="icon-desktop"></i> {% trans "My profile" %}</h3>
<div class="panel-body">
{% crispy form %}
<div class="row">
<div class="col-sm-4" style="margin-bottom: 50px;">
<legend>{% trans "Password change" %}</legend>
{% crispy forms.change_password %}
<div class="col-sm-offset-5 col-sm-3">
<legend>{% trans "Language selection" %}</legend>
{% crispy forms.change_language %}
......@@ -38,6 +38,7 @@ from braces.views import (
from .forms import (
CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm,
NodeForm, TemplateForm, TraitForm, VmCustomizeForm,
from .tables import (NodeListTable, NodeVmListTable,
TemplateListTable, LeaseListTable, GroupListTable,)
......@@ -2126,9 +2127,17 @@ class DiskAddView(TemplateView):
class MyPreferencesView(UpdateView):
model = Profile
form_class = MyProfileForm
def get_context_data(self, *args, **kwargs):
context = super(MyPreferencesView, self).get_context_data(*args,
context['forms'] = {
'change_password': CirclePasswordChangeForm(
'change_language': MyProfileForm(instance=self.get_object()),
return context
def get_object(self, queryset=None):
if self.request.user.is_anonymous():
......@@ -2138,10 +2147,36 @@ class MyPreferencesView(UpdateView):
except Profile.DoesNotExist:
raise Http404(_("You don't have a profile."))
def form_valid(self, form):
response = super(MyPreferencesView, self).form_valid(form)
set_language_cookie(self.request, response)
return response
def post(self, request, *args, **kwargs):
self.ojbect = self.get_object()
redirect_response = HttpResponseRedirect(
if "preferred_language" in request.POST:
form = MyProfileForm(request.POST, instance=self.get_object())
if form.is_valid():
lang = form.cleaned_data.get("preferred_language")
set_language_cookie(self.request, redirect_response, lang)
form = CirclePasswordChangeForm(user=request.user,
if form.is_valid():
if form.is_valid():
return redirect_response
return self.get(request, form=form, *args, **kwargs)
def get(self, request, form=None, *args, **kwargs):
# if this is not here, it won't work
self.object = self.get_object()
context = self.get_context_data(*args, **kwargs)
if form is not None:
# a little cheating, users can't post invalid
# language selection forms (without modifying the HTML)
context['forms']['change_password'] = form
return self.render_to_response(context)
def set_language_cookie(request, response, lang=None):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment