mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-29 20:44:24 +00:00
Refactor Can*Mixin
This commit is contained in:
parent
642249e7fb
commit
81e11b4c33
@ -772,6 +772,8 @@ class Page(models.Model):
|
|||||||
if not locked:
|
if not locked:
|
||||||
raise NotLocked("The page is not locked and thus can not be saved")
|
raise NotLocked("The page is not locked and thus can not be saved")
|
||||||
self.full_clean()
|
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
|
# This reset the _full_name just before saving to maintain a coherent field quicker for queries than the
|
||||||
# recursive method
|
# recursive method
|
||||||
# It also update all the children to maintain correct names
|
# It also update all the children to maintain correct names
|
||||||
|
@ -3,15 +3,13 @@ from django.core.urlresolvers import reverse
|
|||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
|
|
||||||
from core.models import User, Group
|
from core.models import User, Group, Page
|
||||||
|
|
||||||
"""
|
"""
|
||||||
to run these tests :
|
to run these tests :
|
||||||
python3 manage.py test
|
python3 manage.py test
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class UserRegistrationTest(TestCase):
|
class UserRegistrationTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
try:
|
try:
|
||||||
@ -212,27 +210,20 @@ class PageHandlingTest(TestCase):
|
|||||||
"""
|
"""
|
||||||
Should display a page correctly
|
Should display a page correctly
|
||||||
"""
|
"""
|
||||||
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {
|
parent = Page(name="guy", owner_group=Group.objects.filter(id=1).first())
|
||||||
'parent': '',
|
parent.save(force_lock=True)
|
||||||
'name': 'guy',
|
page = Page(name="bibou", owner_group=Group.objects.filter(id=1).first(), parent=parent)
|
||||||
'title': 'Guy',
|
page.save(force_lock=True)
|
||||||
'Content': 'Guyéuyuyé',
|
|
||||||
})
|
|
||||||
self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy/bibou'}), {
|
|
||||||
'parent': '1',
|
|
||||||
'name': 'bibou',
|
|
||||||
'title': 'Bibou',
|
|
||||||
'Content':
|
|
||||||
'Bibibibiblblblblblbouuuuuuuuu',
|
|
||||||
})
|
|
||||||
response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
|
response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
|
||||||
self.assertTrue(response.status_code == 200)
|
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):
|
def test_access_page_not_found(self):
|
||||||
"""
|
"""
|
||||||
Should not display a page correctly
|
Should not display a page correctly
|
||||||
"""
|
"""
|
||||||
response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
|
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(response.status_code == 200)
|
||||||
self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
|
self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import types
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponseForbidden, HttpResponseNotFound
|
from django.http import HttpResponseForbidden, HttpResponseNotFound
|
||||||
@ -57,19 +58,20 @@ class CanEditPropMixin(View):
|
|||||||
object's owner_group
|
object's owner_group
|
||||||
"""
|
"""
|
||||||
def dispatch(self, request, *arg, **kwargs):
|
def dispatch(self, request, *arg, **kwargs):
|
||||||
res = super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
|
try:
|
||||||
if res.__class__.status_code == 302:
|
self.object = self.get_object()
|
||||||
return res
|
if can_edit_prop(self.object, request.user):
|
||||||
if hasattr(self, 'object'):
|
return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
|
||||||
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()
|
|
||||||
except: pass
|
except: pass
|
||||||
|
# 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
|
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):
|
class CanEditMixin(View):
|
||||||
"""
|
"""
|
||||||
@ -77,19 +79,20 @@ class CanEditMixin(View):
|
|||||||
object
|
object
|
||||||
"""
|
"""
|
||||||
def dispatch(self, request, *arg, **kwargs):
|
def dispatch(self, request, *arg, **kwargs):
|
||||||
res = super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
|
try:
|
||||||
if res.__class__.status_code == 302:
|
self.object = self.get_object()
|
||||||
return res
|
if can_edit(self.object, request.user):
|
||||||
if hasattr(self, 'object'):
|
return super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
|
||||||
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()
|
|
||||||
except: pass
|
except: pass
|
||||||
|
# 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
|
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):
|
class CanViewMixin(View):
|
||||||
"""
|
"""
|
||||||
@ -97,30 +100,20 @@ class CanViewMixin(View):
|
|||||||
the object
|
the object
|
||||||
"""
|
"""
|
||||||
def dispatch(self, request, *arg, **kwargs):
|
def dispatch(self, request, *arg, **kwargs):
|
||||||
res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
|
try:
|
||||||
if res.__class__.status_code == 302:
|
self.object = self.get_object()
|
||||||
return res
|
if can_view(self.object, request.user):
|
||||||
if hasattr(self, 'object'):
|
return super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
|
||||||
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()
|
|
||||||
except: pass
|
except: pass
|
||||||
|
# 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
|
raise PermissionDenied
|
||||||
|
self._get_queryset = self.get_queryset
|
||||||
def get_context_data(self, **kwargs):
|
def get_qs(self2):
|
||||||
context = super(CanViewMixin, self).get_context_data(**kwargs)
|
return self2._get_queryset().filter(id__in=l_id)
|
||||||
if hasattr(self, 'object_list'):
|
self.get_queryset = types.MethodType(get_qs, self)
|
||||||
ba_list = list(self.object_list)
|
return super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
|
||||||
l = []
|
|
||||||
for ba in ba_list:
|
|
||||||
if self.request.user.can_view(ba):
|
|
||||||
l.append(ba)
|
|
||||||
context['object_list'] = l
|
|
||||||
return context
|
|
||||||
|
|
||||||
class TabedViewMixin(View):
|
class TabedViewMixin(View):
|
||||||
"""
|
"""
|
||||||
|
@ -87,7 +87,8 @@ class PageCreateView(CanCreateMixin, CreateView):
|
|||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
form.instance.set_lock(self.request.user)
|
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):
|
class PagePropView(CanEditPropMixin, UpdateView):
|
||||||
model = Page
|
model = Page
|
||||||
|
Loading…
Reference in New Issue
Block a user