Commit 46104fe6 by Bach Dániel

Merge branch 'feature-profile' into 'master'

Change password

Users can change their own password.
parents a9cb4a8f 7c3fe6eb
......@@ -5,6 +5,7 @@ from os.path import abspath, basename, dirname, join, normpath, isfile
from sys import path
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _
from json import loads
......@@ -93,7 +94,13 @@ except:
TIME_ZONE = get_env_variable('DJANGO_TIME_ZONE', default=systz)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = get_env_variable("DJANGO_LANGUAGE_CODE", "en")
# https://docs.djangoproject.com/en/dev/ref/settings/#languages
LANGUAGES = (
('en', _('English')),
('hu', _('Hungarian')),
)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1
......
# -*- coding: utf-8 -*-
from django import contrib
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from dashboard.models import Profile
class ProfileInline(contrib.admin.TabularInline):
model = Profile
UserAdmin.inlines = (ProfileInline, )
contrib.admin.site.unregister(User)
contrib.admin.site.register(User, UserAdmin)
......@@ -5,6 +5,7 @@ from datetime import timedelta
from django.contrib.auth.models import User
from django.contrib.auth.forms import (
AuthenticationForm, PasswordResetForm, SetPasswordForm,
PasswordChangeForm,
)
from crispy_forms.helper import FormHelper
......@@ -1015,9 +1016,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):
@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 @@
<h3 class="no-margin"><i class="icon-desktop"></i> {% trans "My profile" %}</h3>
</div>
<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>
......
......@@ -5,6 +5,7 @@ from django.contrib.auth.models import User, Group
from django.core.exceptions import SuspiciousOperation
from django.core.urlresolvers import reverse
from django.contrib.auth.models import Permission
from django.contrib.auth import authenticate
from vm.models import Instance, InstanceTemplate, Lease, Node, Trait
from vm.operations import WakeUpOperation
......@@ -991,3 +992,68 @@ class IndexViewTest(LoginMixin, TestCase):
self.u1.profile.notify("urgent", "dashboard/test_message.txt", )
response = c.get("/dashboard/")
self.assertEqual(response.context['NEW_NOTIFICATIONS_COUNT'], 1)
class ProfileViewTest(LoginMixin, TestCase):
def setUp(self):
self.u1 = User.objects.create(username='user1')
self.u1.set_password('password')
self.u1.save()
self.p1 = Profile.objects.create(user=self.u1)
self.p1.save()
def test_permitted_language_change(self):
c = Client()
self.login(c, "user1")
old_language_cookie_value = c.cookies['django_language'].value
old_language_db_value = self.u1.profile.preferred_language
response = c.post("/dashboard/profile/", {
'preferred_language': "hu",
})
self.assertEqual(response.status_code, 302)
self.assertNotEqual(old_language_cookie_value,
c.cookies['django_language'].value)
self.assertNotEqual(old_language_db_value,
User.objects.get(
username="user1").profile.preferred_language)
def test_permitted_valid_password_change(self):
c = Client()
self.login(c, "user1")
c.post("/dashboard/profile/", {
'old_password': "password",
'new_password1': "asd",
'new_password2': "asd",
})
self.assertIsNone(authenticate(username="user1", password="password"))
self.assertIsNotNone(authenticate(username="user1", password="asd"))
def test_permitted_invalid_password_changes(self):
c = Client()
self.login(c, "user1")
# wrong current password
c.post("/dashboard/profile/", {
'old_password': "password1",
'new_password1': "asd",
'new_password2': "asd",
})
self.assertIsNotNone(authenticate(username="user1",
password="password"))
self.assertIsNone(authenticate(username="user1", password="asd"))
# wrong pw confirmation
c.post("/dashboard/profile/", {
'old_password': "password",
'new_password1': "asd",
'new_password2': "asd1",
})
self.assertIsNotNone(authenticate(username="user1",
password="password"))
self.assertIsNone(authenticate(username="user1", password="asd"))
......@@ -37,6 +37,7 @@ from braces.views import (
from .forms import (
CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm,
NodeForm, TemplateForm, TraitForm, VmCustomizeForm,
CirclePasswordChangeForm
)
from .tables import (NodeListTable, NodeVmListTable,
TemplateListTable, LeaseListTable, GroupListTable,)
......@@ -2124,9 +2125,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,
**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):
if self.request.user.is_anonymous():
......@@ -2136,10 +2145,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(
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):
......
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