Refactor page handling

This commit is contained in:
Skia 2016-05-31 13:00:24 +02:00
parent 8da149c979
commit 356a2d2683
6 changed files with 66 additions and 36 deletions

View File

@ -213,7 +213,7 @@ class User(AbstractBaseUser, PermissionsMixin):
if hasattr(obj, "is_owned_by") and obj.is_owned_by(self):
return True
if hasattr(obj, "owner_group") and self.is_in_group(obj.owner_group.name):
return False
return True
if self.is_superuser or self.is_in_group(settings.SITH_GROUPS['root']['name']):
return True
return False

View File

@ -5,6 +5,8 @@
{{ page.get_display_name() }}
{% elif page_list %}
Page list
{% elif new_page %}
Create page
{% else %}
Not found
{% endif %}
@ -16,6 +18,6 @@
{% endblock %}
{% else %}
<h2>Page does not exist</h2>
<p><a href="{{ url('core:page_prop', page_name=new_page) }}">Create it?</a></p>
<p><a href="{{ url('core:page_new') }}?page={{ request.resolver_match.kwargs['page_name'] }}">Create it?</a></p>
{% endif %}
{% endblock %}

View File

@ -3,10 +3,10 @@
{% block page %}
<h3>Page</h3>
<p><a href="{{ url('core:page_list') }}">Back to list</a></p>
{% if can_edit %}
{% if can_edit(page, user) %}
<p><a href="{{ url('core:page_edit', page_name=page.get_full_name()) }}">Edit</a></p>
{% endif %}
{% if can_edit_prop %}
{% if can_edit_prop(page, user) %}
<p><a href="{{ url('core:page_prop', page_name=page.get_full_name()) }}">Prop</a></p>
{% endif %}
<p>You're seeing the page <strong>{{ page.get_display_name() }}</strong> -

View File

@ -181,7 +181,7 @@ class PageHandlingTest(TestCase):
"""
Should create a page correctly
"""
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {
self.client.post(reverse('core:page_new'), {
'parent': '',
'name': 'guy',
'owner_group': 1,
@ -194,12 +194,12 @@ class PageHandlingTest(TestCase):
"""
Should create a page correctly
"""
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {
self.client.post(reverse('core:page_new'), {
'parent': '',
'name': 'guy',
'owner_group': '1',
})
response = self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy/bibou'}), {
response = self.client.post(reverse('core:page_new'), {
'parent': '1',
'name': 'bibou',
'owner_group': '1',
@ -235,14 +235,13 @@ class PageHandlingTest(TestCase):
"""
response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
self.assertTrue(response.status_code == 200)
self.assertTrue('<a href="/page/swagg/prop">Create it?</a>' in str(response.content))
self.assertTrue('<a href="/page/create?page=swagg">Create it?</a>' in str(response.content))
def test_create_page_markdown_safe(self):
"""
Should format the markdown and escape html correctly
"""
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {
self.client.post(reverse('core:page_new'), {
'parent': '',
'name': 'guy',
'owner_group': '1',

View File

@ -31,6 +31,7 @@ urlpatterns = [
# Page views
url(r'^page/$', PageListView.as_view(), name='page_list'),
url(r'^page/create$', PageCreateView.as_view(), name='page_new'),
url(r'^page/(?P<page_name>[a-z0-9/-_]*)/edit$', PageEditView.as_view(), name='page_edit'),
url(r'^page/(?P<page_name>[a-z0-9/-_]*)/prop$', PagePropView.as_view(), name='page_prop'),
url(r'^page/(?P<page_name>[a-z0-9/-_]*)/hist$', PageHistView.as_view(), name='page_hist'),

View File

@ -1,9 +1,11 @@
# This file contains all the views that concern the page model
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView
from django.views.generic.edit import UpdateView, CreateView
from django.contrib.auth.decorators import login_required, permission_required
from django.utils.decorators import method_decorator
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
from core.models import Page, PageRev, LockError
from core.views.forms import PagePropForm
@ -29,11 +31,6 @@ class PageView(CanViewMixin, DetailView):
context = super(PageView, self).get_context_data(**kwargs)
if "page" not in context.keys():
context['new_page'] = self.kwargs['page_name']
if self.request.user.is_owner(self.object):
context['can_edit_prop'] = True
if self.request.user.can_edit(self.object):
context['can_edit'] = True
return context
class PageHistView(CanViewMixin, DetailView):
@ -66,36 +63,67 @@ class PageRevView(CanViewMixin, DetailView):
context['new_page'] = self.kwargs['page_name']
return context
class PagePropView(CanEditPropMixin, UpdateView):
class PageCreateView(CanEditPropMixin, CreateView):
model = Page
form_class = PagePropForm
form_class = modelform_factory(Page,
fields = ['parent', 'name', 'owner_group', 'edit_groups', 'view_groups', ],
widgets={
'edit_groups':CheckboxSelectMultiple,
'view_groups':CheckboxSelectMultiple,
})
template_name = 'core/page_prop.jinja'
def get_object(self):
page_name = self.kwargs['page_name']
p = Page.get_page_by_full_name(page_name)
# Create the page if it does not exists
if p == None:
def get_initial(self):
init = {}
if 'page' in self.request.GET.keys():
page_name = self.request.GET['page']
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)
self.page = p
if parent is not None:
init['parent'] = parent.id
init['name'] = page_name.split('/')[-1]
return init
def get_context_data(self, **kwargs):
context = super(PageCreateView, self).get_context_data(**kwargs)
print(context)
context['new_page'] = True
return context
def form_valid(self, form):
form.instance.set_lock(self.request.user)
return super(PageCreateView, self).form_valid(form)
class PagePropView(CanEditPropMixin, UpdateView):
model = Page
form_class = modelform_factory(Page,
fields = ['parent', 'name', 'owner_group', 'edit_groups', 'view_groups', ],
widgets={
'edit_groups':CheckboxSelectMultiple,
'view_groups':CheckboxSelectMultiple,
})
template_name = 'core/page_prop.jinja'
slug_field = '_full_name'
slug_url_kwarg = 'page_name'
def get_object(self):
o = super(PagePropView, self).get_object()
# Create the page if it does not exists
#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)
self.page = o
try:
self.page.set_lock_recursive(self.request.user)
except LockError as e:
raise e
return self.page
def get_context_data(self, **kwargs):
context = super(PagePropView, self).get_context_data(**kwargs)
if "page" not in context.keys():
context['new_page'] = self.kwargs['page_name']
return context
class PageEditView(CanEditMixin, UpdateView):
model = PageRev
fields = ['title', 'content',]