Refactor Can*Mixin

This commit is contained in:
Skia 2016-12-14 18:05:19 +01:00
parent 642249e7fb
commit 81e11b4c33
4 changed files with 51 additions and 64 deletions

View File

@ -772,6 +772,8 @@ class Page(models.Model):
if not locked:
raise NotLocked("The page is not locked and thus can not be saved")
self.full_clean()
if not self.id:
super(Page, self).save(*args, **kwargs) # Save a first time to correctly set _full_name
# This reset the _full_name just before saving to maintain a coherent field quicker for queries than the
# recursive method
# It also update all the children to maintain correct names

View File

@ -3,15 +3,13 @@ from django.core.urlresolvers import reverse
from django.contrib.auth.models import Group
from django.core.management import call_command
from core.models import User, Group
from core.models import User, Group, Page
"""
to run these tests :
python3 manage.py test
"""
class UserRegistrationTest(TestCase):
def setUp(self):
try:
@ -212,27 +210,20 @@ class PageHandlingTest(TestCase):
"""
Should display a page correctly
"""
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {
'parent': '',
'name': 'guy',
'title': 'Guy',
'Content': 'Guyéuyuyé',
})
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy/bibou'}), {
'parent': '1',
'name': 'bibou',
'title': 'Bibou',
'Content':
'Bibibibiblblblblblbouuuuuuuuu',
})
parent = Page(name="guy", owner_group=Group.objects.filter(id=1).first())
parent.save(force_lock=True)
page = Page(name="bibou", owner_group=Group.objects.filter(id=1).first(), parent=parent)
page.save(force_lock=True)
response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
self.assertTrue(response.status_code == 200)
self.assertTrue('<a href="/page/guy/bibou/edit">\\xc3\\x89diter</a>' in str(response.content))
def test_access_page_not_found(self):
"""
Should not display a page correctly
"""
response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
response = self.client.get("/page/swagg/")
self.assertTrue(response.status_code == 200)
self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))

View File

@ -1,3 +1,4 @@
import types
from django.shortcuts import render
from django.http import HttpResponseForbidden, HttpResponseNotFound
@ -57,19 +58,20 @@ class CanEditPropMixin(View):
object's owner_group
"""
def dispatch(self, request, *arg, **kwargs):
res = super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
if res.__class__.status_code == 302:
return res
if hasattr(self, 'object'):
obj = self.object
elif hasattr(self, 'object_list'):
obj = self.object_list[0] if self.object_list else None
if can_edit_prop(obj, self.request.user):
return res
try: # Always unlock when 403
self.object.unset_lock()
try:
self.object = self.get_object()
if can_edit_prop(self.object, request.user):
return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
except: pass
raise PermissionDenied
# If we get here, it's a ListView
l_id = [o.id for o in self.get_queryset() if can_edit_prop(o, request.user)]
if not l_id:
raise PermissionDenied
self._get_queryset = self.get_queryset
def get_qs(self2):
return self2._get_queryset().filter(id__in=l_id)
self.get_queryset = types.MethodType(get_qs, self)
return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
class CanEditMixin(View):
"""
@ -77,19 +79,20 @@ class CanEditMixin(View):
object
"""
def dispatch(self, request, *arg, **kwargs):
res = super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
if res.__class__.status_code == 302:
return res
if hasattr(self, 'object'):
obj = self.object
elif hasattr(self, 'object_list'):
obj = self.object_list[0] if self.object_list else None
if can_edit(obj, self.request.user):
return res
try: # Always unlock when 403
self.object.unset_lock()
try:
self.object = self.get_object()
if can_edit(self.object, request.user):
return super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
except: pass
raise PermissionDenied
# If we get here, it's a ListView
l_id = [o.id for o in self.get_queryset() if can_edit(o, request.user)]
if not l_id:
raise PermissionDenied
self._get_queryset = self.get_queryset
def get_qs(self2):
return self2._get_queryset().filter(id__in=l_id)
self.get_queryset = types.MethodType(get_qs, self)
return super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
class CanViewMixin(View):
"""
@ -97,30 +100,20 @@ class CanViewMixin(View):
the object
"""
def dispatch(self, request, *arg, **kwargs):
res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
if res.__class__.status_code == 302:
return res
if hasattr(self, 'object'):
obj = self.object
elif hasattr(self, 'object_list'):
obj = self.object_list[0] if self.object_list else None
if can_view(obj, self.request.user):
return res
try: # Always unlock when 403
self.object.unset_lock()
try:
self.object = self.get_object()
if can_view(self.object, request.user):
return super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
except: pass
raise PermissionDenied
def get_context_data(self, **kwargs):
context = super(CanViewMixin, self).get_context_data(**kwargs)
if hasattr(self, 'object_list'):
ba_list = list(self.object_list)
l = []
for ba in ba_list:
if self.request.user.can_view(ba):
l.append(ba)
context['object_list'] = l
return context
# If we get here, it's a ListView
l_id = [o.id for o in self.get_queryset() if can_view(o, request.user)]
if not l_id:
raise PermissionDenied
self._get_queryset = self.get_queryset
def get_qs(self2):
return self2._get_queryset().filter(id__in=l_id)
self.get_queryset = types.MethodType(get_qs, self)
return super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
class TabedViewMixin(View):
"""

View File

@ -87,7 +87,8 @@ class PageCreateView(CanCreateMixin, CreateView):
def form_valid(self, form):
form.instance.set_lock(self.request.user)
return super(PageCreateView, self).form_valid(form)
ret = super(PageCreateView, self).form_valid(form)
return ret
class PagePropView(CanEditPropMixin, UpdateView):
model = Page