Commit 00f306dc by Kálmán Viktor

dashboard: change password on profile page

parent ddffdf96
...@@ -3,6 +3,7 @@ from datetime import timedelta ...@@ -3,6 +3,7 @@ from datetime import timedelta
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth.forms import ( from django.contrib.auth.forms import (
AuthenticationForm, PasswordResetForm, SetPasswordForm, AuthenticationForm, PasswordResetForm, SetPasswordForm,
PasswordChangeForm,
) )
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
...@@ -1013,9 +1014,20 @@ class MyProfileForm(forms.ModelForm): ...@@ -1013,9 +1014,20 @@ class MyProfileForm(forms.ModelForm):
def helper(self): def helper(self):
helper = FormHelper() helper = FormHelper()
helper.layout = Layout('preferred_language', ) helper.layout = Layout('preferred_language', )
helper.add_input(Submit("submit", _("Save"))) helper.add_input(Submit("submit", _("Change language")))
return helper return helper
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
value = super(MyProfileForm, self).save(*args, **kwargs) value = super(MyProfileForm, self).save(*args, **kwargs)
return value return value
class CirclePasswordChangeForm(PasswordChangeForm):
@property
def helper(self):
helper = FormHelper()
helper.add_input(Submit("submit", _("Change password"),
css_class="btn btn-primary",
css_id="submit-password-button"))
return helper
...@@ -14,7 +14,20 @@ ...@@ -14,7 +14,20 @@
<h3 class="no-margin"><i class="icon-desktop"></i> {% trans "My profile" %}</h3> <h3 class="no-margin"><i class="icon-desktop"></i> {% trans "My profile" %}</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
{% crispy form %} <div class="row">
<div class="col-sm-4" style="margin-bottom: 50px;">
<fieldset>
<legend>{% trans "Password change" %}</legend>
{% crispy forms.change_password %}
</fieldset>
</div>
<div class="col-sm-offset-5 col-sm-3">
<fieldset>
<legend>{% trans "Language selection" %}</legend>
{% crispy forms.change_language %}
</fieldset>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -38,6 +38,7 @@ from braces.views import ( ...@@ -38,6 +38,7 @@ from braces.views import (
from .forms import ( from .forms import (
CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm, CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm,
NodeForm, TemplateForm, TraitForm, VmCustomizeForm, NodeForm, TemplateForm, TraitForm, VmCustomizeForm,
CirclePasswordChangeForm
) )
from .tables import (NodeListTable, NodeVmListTable, from .tables import (NodeListTable, NodeVmListTable,
TemplateListTable, LeaseListTable, GroupListTable,) TemplateListTable, LeaseListTable, GroupListTable,)
...@@ -2126,9 +2127,17 @@ class DiskAddView(TemplateView): ...@@ -2126,9 +2127,17 @@ class DiskAddView(TemplateView):
class MyPreferencesView(UpdateView): class MyPreferencesView(UpdateView):
model = Profile model = Profile
form_class = MyProfileForm
def get_context_data(self, *args, **kwargs):
context = super(MyPreferencesView, self).get_context_data(*args,
**kwargs)
context['forms'] = {
'change_password': CirclePasswordChangeForm(
user=self.request.user),
'change_language': MyProfileForm(instance=self.get_object()),
}
return context
def get_object(self, queryset=None): def get_object(self, queryset=None):
if self.request.user.is_anonymous(): if self.request.user.is_anonymous():
...@@ -2138,10 +2147,36 @@ class MyPreferencesView(UpdateView): ...@@ -2138,10 +2147,36 @@ class MyPreferencesView(UpdateView):
except Profile.DoesNotExist: except Profile.DoesNotExist:
raise Http404(_("You don't have a profile.")) raise Http404(_("You don't have a profile."))
def form_valid(self, form): def post(self, request, *args, **kwargs):
response = super(MyPreferencesView, self).form_valid(form) self.ojbect = self.get_object()
set_language_cookie(self.request, response) redirect_response = HttpResponseRedirect(
return response reverse("dashboard.views.profile"))
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.save()
else:
form = CirclePasswordChangeForm(user=request.user,
data=request.POST)
if form.is_valid():
form.save()
if form.is_valid():
return redirect_response
else:
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): 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