From 259182c1c4c2d390a88f7fe28a634cdc7e3e6820 Mon Sep 17 00:00:00 2001 From: Skia Date: Tue, 24 Nov 2015 14:01:10 +0100 Subject: [PATCH] Add page properties view --- core/forms.py | 36 ++++++++++++++++--- core/migrations/0003_auto_20151124_1021.py | 18 ++++++++++ core/migrations/0004_auto_20151124_1033.py | 30 ++++++++++++++++ core/migrations/0005_auto_20151124_1219.py | 29 ++++++++++++++++ core/models.py | 10 ++++-- core/templates/core/edit_user.html | 5 +++ core/templates/core/page.html | 14 ++++++-- core/tests.py | 12 +++++++ core/urls.py | 1 + core/views.py | 40 +++++++++++++++++++--- 10 files changed, 180 insertions(+), 15 deletions(-) create mode 100644 core/migrations/0003_auto_20151124_1021.py create mode 100644 core/migrations/0004_auto_20151124_1033.py create mode 100644 core/migrations/0005_auto_20151124_1219.py diff --git a/core/forms.py b/core/forms.py index 0d1ca5b7..08d59629 100644 --- a/core/forms.py +++ b/core/forms.py @@ -1,4 +1,4 @@ -from django.contrib.auth.forms import UserCreationForm, AuthenticationForm +from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, UserChangeForm from django import forms from django.contrib.auth import logout, login, authenticate import logging @@ -42,21 +42,47 @@ class LoginForm(AuthenticationForm): params={'username': self.username_field.verbose_name}, ) -class PageForm(forms.ModelForm): +class EditUserForm(UserChangeForm): + error_css_class = 'error' + required_css_class = 'required' + class Meta: + model = User + fields = ('first_name', 'last_name', 'email', 'date_of_birth', 'groups', 'user_permissions') + + +class PagePropForm(forms.ModelForm): error_css_class = 'error' required_css_class = 'required' parent = forms.ModelChoiceField(queryset=Page.objects.all()) def __init__(self, *args, **kwargs): - super(PageForm, self).__init__(*args, **kwargs) + super(PagePropForm, self).__init__(*args, **kwargs) self.fields['parent'].required = False class Meta: model = Page - fields = ['parent', 'name', 'title', 'content', ] + fields = ['parent', 'name', 'owner_group', 'edit_group', 'view_group'] def save(self, commit=True): - page = super(PageForm, self).save(commit=False) + page = super(PagePropForm, self).save(commit=False) + if commit: + page.save() + return page + + +class PageEditForm(forms.ModelForm): + error_css_class = 'error' + required_css_class = 'required' + + def __init__(self, *args, **kwargs): + super(PageEditForm, self).__init__(*args, **kwargs) + + class Meta: + model = Page + fields = ['title', 'content', ] + + def save(self, commit=True): + page = super(PageEditForm, self).save(commit=False) if commit: page.save() return page diff --git a/core/migrations/0003_auto_20151124_1021.py b/core/migrations/0003_auto_20151124_1021.py new file mode 100644 index 00000000..c840a05a --- /dev/null +++ b/core/migrations/0003_auto_20151124_1021.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0002_page_full_name'), + ] + + operations = [ + migrations.AlterModelOptions( + name='page', + options={'permissions': ()}, + ), + ] diff --git a/core/migrations/0004_auto_20151124_1033.py b/core/migrations/0004_auto_20151124_1033.py new file mode 100644 index 00000000..ea360dba --- /dev/null +++ b/core/migrations/0004_auto_20151124_1033.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0006_require_contenttypes_0002'), + ('core', '0003_auto_20151124_1021'), + ] + + operations = [ + migrations.AddField( + model_name='page', + name='edit_group', + field=models.ForeignKey(null=True, to='auth.Group', related_name='editable_pages'), + ), + migrations.AddField( + model_name='page', + name='owner_group', + field=models.ForeignKey(null=True, to='auth.Group', related_name='owned_pages'), + ), + migrations.AddField( + model_name='page', + name='view_group', + field=models.ForeignKey(null=True, to='auth.Group', related_name='viewable_pages'), + ), + ] diff --git a/core/migrations/0005_auto_20151124_1219.py b/core/migrations/0005_auto_20151124_1219.py new file mode 100644 index 00000000..248ff8b0 --- /dev/null +++ b/core/migrations/0005_auto_20151124_1219.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0004_auto_20151124_1033'), + ] + + operations = [ + migrations.AlterField( + model_name='page', + name='edit_group', + field=models.ForeignKey(related_name='editable_pages', to='auth.Group', default=1), + ), + migrations.AlterField( + model_name='page', + name='owner_group', + field=models.ForeignKey(related_name='owned_pages', to='auth.Group', default=1), + ), + migrations.AlterField( + model_name='page', + name='view_group', + field=models.ForeignKey(related_name='viewable_pages', to='auth.Group', default=1), + ), + ] diff --git a/core/models.py b/core/models.py index 1cb113c4..975121db 100644 --- a/core/models.py +++ b/core/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager +from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager, Group from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from django.core import validators @@ -130,11 +130,15 @@ class Page(models.Model): # playing with a Page object, use get_full_name() instead! full_name = models.CharField(_('page name'), max_length=255, blank=True) + owner_group = models.ForeignKey(Group, related_name="owned_pages", default=1) + edit_group = models.ForeignKey(Group, related_name="editable_pages", default=1) + view_group = models.ForeignKey(Group, related_name="viewable_pages", default=1) + class Meta: unique_together = ('name', 'parent') permissions = ( - ("can_edit", "Can edit the page"), - ("can_view", "Can view the page"), + #("can_edit", "Can edit the page"), + #("can_view", "Can view the page"), ) @staticmethod diff --git a/core/templates/core/edit_user.html b/core/templates/core/edit_user.html index 99790740..16845557 100644 --- a/core/templates/core/edit_user.html +++ b/core/templates/core/edit_user.html @@ -11,5 +11,10 @@ Edit {{ profile.get_display_name }}

Edit user

Back to profile

You're editing the profile of {{ profile.get_display_name }}

+
+ {% csrf_token %} + {{ user_form }} +

+
{% endif %} {% endblock %} diff --git a/core/templates/core/page.html b/core/templates/core/page.html index 6068061e..b0f9d413 100644 --- a/core/templates/core/page.html +++ b/core/templates/core/page.html @@ -16,6 +16,7 @@

Back to list

{% if user.is_superuser %}

Edit

+

Prop

{% endif %}

You're seeing the page {{ page.get_display_name }}

{{ page.title }}

@@ -29,12 +30,19 @@ {% elif new_page %}

{{ title }}

-

Create it?

- {% elif page_form %} +

Create it?

+ {% elif page_prop %} +

Page properties

+
+ {% csrf_token %} + {{ page_prop }} +

+
+ {% elif page_edit %}

Edit page

{% csrf_token %} - {{ page_form }} + {{ page_edit }}

{% else %} diff --git a/core/tests.py b/core/tests.py index d03917fa..dcecb009 100644 --- a/core/tests.py +++ b/core/tests.py @@ -1,10 +1,17 @@ from django.test import SimpleTestCase, Client from django.core.urlresolvers import reverse +from django.contrib.auth.models import Group from .models import User from .forms import RegisteringForm, LoginForm class UserRegistrationTest(SimpleTestCase): + def setUp(self): + try: + Group.objects.create(name="root") + except: + pass + def test_register_user_form_ok(self): """ Should register a user correctly @@ -209,3 +216,8 @@ class UserRegistrationTest(SimpleTestCase): response = c.get(reverse('core:page', kwargs={'page_name': 'swagg'})) self.assertTrue(response.status_code == 200) self.assertTrue('PAGE_NOT_FOUND' in str(response.content)) + +#TODO: many tests on the pages: +# - renaming a page +# - changing a page's parent --> check that page's children's full_name +# - changing the different groups of the page diff --git a/core/urls.py b/core/urls.py index 7fc5f0f2..a33f540b 100644 --- a/core/urls.py +++ b/core/urls.py @@ -13,5 +13,6 @@ urlpatterns = [ url(r'^page/$', views.page, name='page_list'), url(r'^page/(?P[a-z0-9/]*)/$', views.page, name='page'), url(r'^page/(?P[a-z0-9/]*)/edit$', views.page_edit, name='page_edit'), + url(r'^page/(?P[a-z0-9/]*)/prop$', views.page_prop, name='page_prop'), ] diff --git a/core/views.py b/core/views.py index a50679bc..0dba995b 100644 --- a/core/views.py +++ b/core/views.py @@ -4,7 +4,7 @@ from django.contrib.auth import logout as auth_logout from django.db import models from .models import User, Page -from .forms import RegisteringForm, LoginForm, PageForm +from .forms import RegisteringForm, LoginForm, EditUserForm, PageEditForm, PagePropForm import logging @@ -90,6 +90,7 @@ def user_edit(request, user_id=None): user_id = int(user_id) if request.user.is_authenticated() and (request.user.pk == user_id or request.user.is_superuser): context['profile'] = get_object_or_404(User, pk=user_id) + context['user_form'] = EditUserForm(instance=context['profile']).as_p() return render(request, "core/edit_user.html", context) return user(request, user_id) @@ -130,7 +131,7 @@ def page_edit(request, page_name=None): p = Page(name=name, parent=parent) # Saving page if request.method == 'POST': - f = PageForm(request.POST, instance=p) + f = PageEditForm(request.POST, instance=p) if f.is_valid(): f.save() context['tests'] = "PAGE_SAVED" @@ -139,8 +140,39 @@ def page_edit(request, page_name=None): # Default: display the edit form without change else: context['tests'] = "POST_NOT_RECEIVED" - f = PageForm(instance=p) + f = PageEditForm(instance=p) context['page'] = p - context['page_form'] = f.as_p() + context['page_edit'] = f.as_p() return render(request, 'core/page.html', context) +def page_prop(request, page_name=None): + """ + page_prop view, able to change a page's properties + """ + context = {'title': 'Page properties', + 'page_name': page_name} + p = Page.get_page_by_full_name(page_name) + # New page + if p == None: + parent_name = '/'.join(page_name.split('/')[:-1]) + name = page_name.split('/')[-1] + if parent_name == "": + p = Page(name=name) + else: + parent = Page.get_page_by_full_name(parent_name) + p = Page(name=name, parent=parent) + # Saving page + if request.method == 'POST': + f = PagePropForm(request.POST, instance=p) + if f.is_valid(): + f.save() + context['tests'] = "PAGE_SAVED" + else: + context['tests'] = "PAGE_NOT_SAVED" + # Default: display the edit form without change + else: + context['tests'] = "POST_NOT_RECEIVED" + f = PagePropForm(instance=p) + context['page'] = p + context['page_prop'] = f.as_p() + return render(request, 'core/page.html', context)