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): if hasattr(obj, "is_owned_by") and obj.is_owned_by(self):
return True return True
if hasattr(obj, "owner_group") and self.is_in_group(obj.owner_group.name): 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']): if self.is_superuser or self.is_in_group(settings.SITH_GROUPS['root']['name']):
return True return True
return False return False

View File

@ -5,6 +5,8 @@
{{ page.get_display_name() }} {{ page.get_display_name() }}
{% elif page_list %} {% elif page_list %}
Page list Page list
{% elif new_page %}
Create page
{% else %} {% else %}
Not found Not found
{% endif %} {% endif %}
@ -16,6 +18,6 @@
{% endblock %} {% endblock %}
{% else %} {% else %}
<h2>Page does not exist</h2> <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 %} {% endif %}
{% endblock %} {% endblock %}

View File

@ -3,10 +3,10 @@
{% block page %} {% block page %}
<h3>Page</h3> <h3>Page</h3>
<p><a href="{{ url('core:page_list') }}">Back to list</a></p> <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> <p><a href="{{ url('core:page_edit', page_name=page.get_full_name()) }}">Edit</a></p>
{% endif %} {% 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> <p><a href="{{ url('core:page_prop', page_name=page.get_full_name()) }}">Prop</a></p>
{% endif %} {% endif %}
<p>You're seeing the page <strong>{{ page.get_display_name() }}</strong> - <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 Should create a page correctly
""" """
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), { self.client.post(reverse('core:page_new'), {
'parent': '', 'parent': '',
'name': 'guy', 'name': 'guy',
'owner_group': 1, 'owner_group': 1,
@ -194,12 +194,12 @@ class PageHandlingTest(TestCase):
""" """
Should create a page correctly Should create a page correctly
""" """
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), { self.client.post(reverse('core:page_new'), {
'parent': '', 'parent': '',
'name': 'guy', 'name': 'guy',
'owner_group': '1', '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', 'parent': '1',
'name': 'bibou', 'name': 'bibou',
'owner_group': '1', 'owner_group': '1',
@ -235,14 +235,13 @@ class PageHandlingTest(TestCase):
""" """
response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'})) response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
self.assertTrue(response.status_code == 200) 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): def test_create_page_markdown_safe(self):
""" """
Should format the markdown and escape html correctly 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': '', 'parent': '',
'name': 'guy', 'name': 'guy',
'owner_group': '1', 'owner_group': '1',

View File

@ -31,6 +31,7 @@ urlpatterns = [
# Page views # Page views
url(r'^page/$', PageListView.as_view(), name='page_list'), 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/-_]*)/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/-_]*)/prop$', PagePropView.as_view(), name='page_prop'),
url(r'^page/(?P<page_name>[a-z0-9/-_]*)/hist$', PageHistView.as_view(), name='page_hist'), 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 # This file contains all the views that concern the page model
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import ListView, DetailView 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.contrib.auth.decorators import login_required, permission_required
from django.utils.decorators import method_decorator 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.models import Page, PageRev, LockError
from core.views.forms import PagePropForm from core.views.forms import PagePropForm
@ -29,11 +31,6 @@ class PageView(CanViewMixin, DetailView):
context = super(PageView, self).get_context_data(**kwargs) context = super(PageView, self).get_context_data(**kwargs)
if "page" not in context.keys(): if "page" not in context.keys():
context['new_page'] = self.kwargs['page_name'] 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 return context
class PageHistView(CanViewMixin, DetailView): class PageHistView(CanViewMixin, DetailView):
@ -66,36 +63,67 @@ class PageRevView(CanViewMixin, DetailView):
context['new_page'] = self.kwargs['page_name'] context['new_page'] = self.kwargs['page_name']
return context return context
class PagePropView(CanEditPropMixin, UpdateView): class PageCreateView(CanEditPropMixin, CreateView):
model = Page 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' template_name = 'core/page_prop.jinja'
def get_object(self): def get_initial(self):
page_name = self.kwargs['page_name'] init = {}
p = Page.get_page_by_full_name(page_name) if 'page' in self.request.GET.keys():
# Create the page if it does not exists page_name = self.request.GET['page']
if p == None:
parent_name = '/'.join(page_name.split('/')[:-1]) parent_name = '/'.join(page_name.split('/')[:-1])
name = page_name.split('/')[-1] parent = Page.get_page_by_full_name(parent_name)
if parent_name == "": if parent is not None:
p = Page(name=name) init['parent'] = parent.id
else: init['name'] = page_name.split('/')[-1]
parent = Page.get_page_by_full_name(parent_name) return init
p = Page(name=name, parent=parent)
self.page = p 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: try:
self.page.set_lock_recursive(self.request.user) self.page.set_lock_recursive(self.request.user)
except LockError as e: except LockError as e:
raise e raise e
return self.page 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): class PageEditView(CanEditMixin, UpdateView):
model = PageRev model = PageRev
fields = ['title', 'content',] fields = ['title', 'content',]