Add page properties view

This commit is contained in:
Skia 2015-11-24 14:01:10 +01:00
parent 7b093f6fbd
commit 259182c1c4
10 changed files with 180 additions and 15 deletions

View File

@ -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

View File

@ -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': ()},
),
]

View File

@ -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'),
),
]

View File

@ -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),
),
]

View File

@ -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

View File

@ -11,5 +11,10 @@ Edit {{ profile.get_display_name }}
<h3>Edit user</h3>
<p><a href="{% url 'core:user_profile' profile.id %}">Back to profile</a></p>
<p>You're editing the profile of <strong>{{ profile.get_display_name }}</strong></p>
<form action="{% url 'core:user_edit' profile.id %}" method="post">
{% csrf_token %}
{{ user_form }}
<p><input type="submit" value="Save!" /></p>
</form>
{% endif %}
{% endblock %}

View File

@ -16,6 +16,7 @@
<p><a href="{% url 'core:page_list' %}">Back to list</a></p>
{% if user.is_superuser %}
<p><a href="{% url 'core:page_edit' page.get_full_name %}">Edit</a></p>
<p><a href="{% url 'core:page_prop' page.get_full_name %}">Prop</a></p>
{% endif %}
<p>You're seeing the page <strong>{{ page.get_display_name }}</strong></p>
<h3>{{ page.title }}</h3>
@ -29,12 +30,19 @@
</ul>
{% elif new_page %}
<h2>{{ title }}</h2>
<p><a href="{% url 'core:page_edit' new_page %}">Create it?</a></p>
{% elif page_form %}
<p><a href="{% url 'core:page_prop' new_page %}">Create it?</a></p>
{% elif page_prop %}
<h2>Page properties</h2>
<form action="{% url 'core:page_prop' page_name=page.get_full_name %}" method="post">
{% csrf_token %}
{{ page_prop }}
<p><input type="submit" value="Save!" /></p>
</form>
{% elif page_edit %}
<h2>Edit page</h2>
<form action="{% url 'core:page_edit' page_name=page.get_full_name %}" method="post">
{% csrf_token %}
{{ page_form }}
{{ page_edit }}
<p><input type="submit" value="Save!" /></p>
</form>
{% else %}

View File

@ -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

View File

@ -13,5 +13,6 @@ urlpatterns = [
url(r'^page/$', views.page, name='page_list'),
url(r'^page/(?P<page_name>[a-z0-9/]*)/$', views.page, name='page'),
url(r'^page/(?P<page_name>[a-z0-9/]*)/edit$', views.page_edit, name='page_edit'),
url(r'^page/(?P<page_name>[a-z0-9/]*)/prop$', views.page_prop, name='page_prop'),
]

View File

@ -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)