Skip to content

Commit

Permalink
Merge pull request #535 from inflrscns/account_settings
Browse files Browse the repository at this point in the history
account settings page
  • Loading branch information
benjaoming committed May 5, 2016
2 parents 6538381 + c620709 commit 1877b7b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 15 deletions.
19 changes: 18 additions & 1 deletion wiki/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def clean_title(self):

def clean(self):
"""Validates form data by checking for the following
No new revisions have been created since user attempted to edit
No new revisions have been created since user attempted to edit
Revision title or content has changed
"""
cd = self.cleaned_data
Expand Down Expand Up @@ -633,3 +633,20 @@ def clean(self):
class Meta:
model = User
fields = ("username", "email")

class UserUpdateForm(forms.ModelForm):
password1 = forms.CharField(label="New password", widget=forms.PasswordInput(), required=False)
password2 = forms.CharField(label="Confirm password", widget=forms.PasswordInput(), required=False)

def clean(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')

if password1 and password1 != password2:
raise forms.ValidationError(_("Passwords don't match"))

return self.cleaned_data

class Meta:
model = User
fields = ['email']
14 changes: 14 additions & 0 deletions wiki/templates/wiki/accounts/account_settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "wiki/base.html" %}
{% load i18n wiki_tags sekizai_tags %}

{% block wiki_pagetitle %}{% trans "Account Settings" %}{% endblock %}

{% block wiki_contents %}
<h1 class="page-header">{% trans "Account Settings" %}</h1>
<form class="form-horizontal" action="" method="post">{% csrf_token %}
{% wiki_form form %}
<button type="submit" name="save_changes" class="btn btn-primary btn-lg">
{% trans "Update" %}
</button>
</form>
{% endblock %}
8 changes: 8 additions & 0 deletions wiki/templates/wiki/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
{% trans "Log out" %}
</a>
</li>
{% if "ACCOUNT_HANDLING"|wiki_settings %}
<li>
<a href="{% url 'wiki:profile_update' %}">
<i class="fa fa-gear"></i>
{% trans "Account Settings" %}
</a>
</li>
{% endif %}
{% if user.is_superuser %}
<li>
<a href="{% url 'wiki:deleted_list' %}">
Expand Down
4 changes: 4 additions & 0 deletions wiki/templatetags/wiki_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ def plugin_enabled(plugin_name):
'wiki.plugins.attachments'
"""
return plugin_name in django_settings.INSTALLED_APPS

@register.filter
def wiki_settings(name):
return getattr(settings, name, "")
2 changes: 1 addition & 1 deletion wiki/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from wiki.plugins.attachments.models import Attachment


class ArticlManagerTests(ArticleWebTestBase):
class ArticleManagerTests(ArticleWebTestBase):

def test_queryset_methods_directly_on_manager(self):

Expand Down
13 changes: 13 additions & 0 deletions wiki/tests/test_template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,16 @@ class PluginEnabled(TemplateTestCase):
def test_true(self):
output = self.render({})
self.assertIn('It is enabled', output)


class WikiSettings(TemplateTestCase):

template = """
{% load wiki_tags %}
{% if "ACCOUNT_HANDLING"|wiki_settings %}It is enabled{% endif %}
"""

@wiki_override_settings(WIKI_ACCOUNT_HANDLING=lambda *args: True)
def test_setting(self):
output = self.render({})
self.assertIn('It is enabled', output)
17 changes: 17 additions & 0 deletions wiki/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pprint

from django.contrib.auth import authenticate

from .base import ArticleWebTestBase, WebTestBase

from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -301,6 +303,7 @@ def test_empty_query_string(self):
response = c.get(reverse('wiki:search'), {'q': ''})
self.assertFalse(response.context['articles'])


class DeletedListViewTest(ArticleWebTestBase):

def test_deleted_articles_list(self):
Expand Down Expand Up @@ -328,3 +331,17 @@ def test_deleted_articles_list(self):

response = c.get(reverse('wiki:deleted_list'))
self.assertContains(response, 'Delete Me')


class UpdateProfileViewTest(ArticleWebTestBase):

def test_update_profile(self):
c = self.c

response = c.post(reverse('wiki:profile_update'),
{"email":"test@test.com", "password1":"newPass", "password2":"newPass"}, follow=True)

test_auth = authenticate(username='admin', password='newPass')

self.assertNotEqual(test_auth, None)
self.assertEqual(test_auth.email, 'test@test.com')
29 changes: 18 additions & 11 deletions wiki/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WikiURLPatterns(object):
signup_view_class = accounts.Signup
login_view_class = accounts.Login
logout_view_class = accounts.Logout
profile_update_view_class = accounts.Update

# deleted list view
deleted_list_view_class = deleted_list.DeletedListView
Expand Down Expand Up @@ -89,17 +90,23 @@ def get_deleted_list_urls(self):
return urlpatterns

def get_accounts_urls(self):
urlpatterns = [
url('^_accounts/sign-up/$',
self.signup_view_class.as_view(),
name='signup'),
url('^_accounts/logout/$',
self.logout_view_class.as_view(),
name='logout'),
url('^_accounts/login/$',
self.login_view_class.as_view(),
name='login'),
]
if settings.ACCOUNT_HANDLING:
urlpatterns = [
url('^_accounts/sign-up/$',
self.signup_view_class.as_view(),
name='signup'),
url('^_accounts/logout/$',
self.logout_view_class.as_view(),
name='logout'),
url('^_accounts/login/$',
self.login_view_class.as_view(),
name='login'),
url('^_accounts/settings/$',
self.profile_update_view_class.as_view(),
name='profile_update'),
]
else:
urlpatterns = []
return urlpatterns

def get_revision_urls(self):
Expand Down
21 changes: 19 additions & 2 deletions wiki/views/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
from django.contrib.auth import logout as auth_logout, login as auth_login
from django.contrib.auth.forms import AuthenticationForm
from django.core.urlresolvers import reverse
from django.shortcuts import redirect, render_to_response
from django.shortcuts import redirect, render_to_response, get_object_or_404
from django.template.context import RequestContext
from django.utils.translation import ugettext as _
from django.views.generic.base import View
from django.views.generic.edit import CreateView, FormView
from django.views.generic.edit import CreateView, FormView, UpdateView

from wiki import forms
from wiki.conf import settings
Expand Down Expand Up @@ -115,3 +115,20 @@ def form_valid(self, form, *args, **kwargs):
if not self.referer:
return redirect("wiki:root")
return redirect(self.referer)

class Update(UpdateView):
model = User
form_class = forms.UserUpdateForm
template_name = "wiki/accounts/account_settings.html"
success_url = "/_accounts/settings/"

def get_object(self, queryset=None):
return get_object_or_404(self.model, pk=self.request.user.pk)

def form_valid(self, form):
pw = form.cleaned_data["password1"]
if pw is not "":
self.object.set_password(pw)
self.object.save()
messages.info(self.request, _("Account info saved!"))
return super(Update, self).form_valid(form)

0 comments on commit 1877b7b

Please sign in to comment.