Permalink
Browse files

Moved helper methods from permmod to base

Cleaned up managers a bit
  • Loading branch information...
1 parent 0f59b80 commit 8e0965c3363f4e86d71eb9bf50dc9e78e38ea166 Jonas Obrist committed Dec 30, 2010
Showing with 171 additions and 193 deletions.
  1. +0 −7 cms/models/managers.py
  2. +171 −0 cms/tests/base.py
  3. +0 −186 cms/tests/permmod.py
View
@@ -457,10 +457,7 @@ def __get_id_list(self, user, site, attr):
qs.order_by('page__tree_id', 'page__level', 'page__lft')
# default is denny...
page_id_allow_list = []
- desc_list = []
for permission in qs:
- desc_list.append((permission.page.get_descendants(include_self=True), permission.page.pk, 'grant %i' % permission.grant_on))
-
is_allowed = getattr(permission, attr)
if is_allowed:
@@ -471,10 +468,6 @@ def __get_id_list(self, user, site, attr):
page_id_allow_list.extend(permission.page.get_children().values_list('id', flat=True))
elif permission.grant_on & MASK_DESCENDANTS:
page_id_allow_list.extend(permission.page.get_descendants().values_list('id', flat=True))
-
- # print 'descendants for pages in permissions for (' + str(user) + ') : ' + str(desc_list) # no pages with permissions has descendants???
- # store value in cache
-
#set_permission_cache(user, attr, page_id_allow_list)
return page_id_allow_list
View
@@ -1,6 +1,10 @@
from cms.models import Title, Page
+from cms.models.moderatormodels import ACCESS_PAGE_AND_DESCENDANTS
+from cms.models.permissionmodels import PagePermission
+from cms.models.pluginmodel import CMSPlugin
from cms.utils.permissions import _thread_locals
from django.conf import settings
+from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.exceptions import ObjectDoesNotExist
from django.core.handlers.wsgi import WSGIRequest
@@ -236,7 +240,174 @@ def get_request(self, path=None):
request.user = self.user
request.LANGUAGE_CODE = settings.LANGUAGES[0][0]
return request
+
+ def create_page_user(self, username, password=None,
+ can_add_page=True, can_change_page=True, can_delete_page=True,
+ can_recover_page=True, can_add_pageuser=True, can_change_pageuser=True,
+ can_delete_pageuser=True, can_add_pagepermission=True,
+ can_change_pagepermission=True, can_delete_pagepermission=True,
+ grant_all=False):
+ """Helper function for creating page user, through form on:
+ /admin/cms/pageuser/add/
+
+ Returns created user.
+ """
+ if grant_all:
+ return self.create_page_user(username, password,
+ True, True, True, True, True, True, True, True, True, True)
+
+ if password is None:
+ password=username
+
+ data = {
+ 'username': username,
+ 'password1': password,
+ 'password2': password,
+ 'can_add_page': can_add_page,
+ 'can_change_page': can_change_page,
+ 'can_delete_page': can_delete_page,
+ 'can_recover_page': can_recover_page,
+ 'can_add_pageuser': can_add_pageuser,
+ 'can_change_pageuser': can_change_pageuser,
+ 'can_delete_pageuser': can_delete_pageuser,
+ 'can_add_pagepermission': can_add_pagepermission,
+ 'can_change_pagepermission': can_change_pagepermission,
+ 'can_delete_pagepermission': can_delete_pagepermission,
+ }
+ response = self.client.post('/admin/cms/pageuser/add/', data)
+ self.assertEqual(response.status_code, 302)
+
+ return User.objects.get(username=username)
+
+ def assign_user_to_page(self, page, user, grant_on=ACCESS_PAGE_AND_DESCENDANTS,
+ can_add=False, can_change=False, can_delete=False,
+ can_change_advanced_settings=False, can_publish=False,
+ can_change_permissions=False, can_move_page=False, can_moderate=False,
+ grant_all=False):
+ """Assigns given user to page, and gives him requested permissions.
+
+ Note: this is not happening over frontend, maybe a test for this in
+ future will be nice.
+ """
+ if grant_all:
+ return self.assign_user_to_page(page, user, grant_on,
+ True, True, True, True, True, True, True, True)
+
+ # just check if the current logged in user even can change the page and
+ # see the permission inline
+ response = self.client.get(URL_CMS_PAGE_CHANGE % page.id)
+ self.assertEqual(response.status_code, 200)
+
+ data = {
+ 'can_add': can_add,
+ 'can_change': can_change,
+ 'can_delete': can_delete,
+ 'can_change_advanced_settings': can_change_advanced_settings,
+ 'can_publish': can_publish,
+ 'can_change_permissions': can_change_permissions,
+ 'can_move_page': can_move_page,
+ 'can_moderate': can_moderate,
+ }
+
+ page_permission = PagePermission(page=page, user=user, grant_on=grant_on, **data)
+ page_permission.save()
+ return page_permission
+
+ def add_plugin(self, user, page=None):
+ if page:
+ slave_page = page
+ else:
+ slave_page = self.slave_page
+
+ post_data = {
+ 'language': 'en',
+ 'placeholder': slave_page.placeholders.get(slot__iexact='Right-Column').pk,
+ 'plugin_type': 'TextPlugin'
+ }
+ self.login_user(user)
+ url = URL_CMS_PAGE + "%d/add-plugin/" % slave_page.pk
+ response = self.client.post(url, post_data)
+
+ cmsplugin_set = CMSPlugin.objects.filter(placeholder__in=slave_page.placeholders.all())
+ self.assertEqual(cmsplugin_set.count(), 1)
+ plugin_id = cmsplugin_set[0].id
+ self.assertEqual(response.content, str(plugin_id))
+ return plugin_id
+
+ def publish_page(self, page, approve=False, user=None, published_check=True):
+ if user:
+ self.login_user(user)
+
+ if published_check and not approve:
+ # must have public object now
+ self.assertFalse(page.publisher_public)
+ self.assertFalse(page.published)
+
+ # publish / approve page by master
+ response = self.client.post(URL_CMS_PAGE + "%d/change-status/" % page.pk, {1 :1})
+ self.assertEqual(response.status_code, 200)
+
+ if not approve:
+ page = self.reload_page(page)
+ if published_check:
+ # must have public object now
+ self.assertTrue(page.publisher_public, "Page '%s' has no publisher_public" % page)
+ # and public object must be published
+ self.assertTrue(page.publisher_public.published)
+ return page
+
+ # approve
+ page = self.approve_page(page)
+ if published_check:
+ # must have public object now
+ self.assertTrue(page.publisher_public, "Page '%s' has no publisher_public" % page)
+ # and public object must be published
+ self.assertTrue(page.publisher_public.published)
+
+ return page
+
+ def approve_page(self, page):
+ response = self.client.get(URL_CMS_PAGE + "%d/approve/" % page.pk)
+ self.assertRedirects(response, URL_CMS_PAGE)
+ # reload page
+ return self.reload_page(page)
+
+ def check_published_page_attributes(self, page):
+ public_page = page.publisher_public
+
+ if page.parent:
+ self.assertEqual(page.parent_id, public_page.parent.publisher_draft.id)
+
+ self.assertEqual(page.level, public_page.level)
+
+ # TODO: add check for siblings
+
+ draft_siblings = list(page.get_siblings(True). \
+ filter(publisher_is_draft=True).order_by('tree_id', 'parent', 'lft'))
+ public_siblings = list(public_page.get_siblings(True). \
+ filter(publisher_is_draft=False).order_by('tree_id', 'parent', 'lft'))
+ skip = 0
+ for i, sibling in enumerate(draft_siblings):
+ if not sibling.publisher_public_id:
+ skip += 1
+ continue
+ self.assertEqual(sibling.id, public_siblings[i - skip].publisher_draft.id)
+
+ def request_moderation(self, page, level):
+ """Assign current logged in user to the moderators / change moderation
+
+ Args:
+ page: Page on which moderation should be changed
+
+ level <0, 7>: Level of moderation,
+ 1 - moderate page
+ 2 - moderate children
+ 4 - moderate descendants
+ + conbinations
+ """
+ response = self.client.post("/admin/cms/page/%d/change-moderation/" % page.id, {'moderate': level})
+ self.assertEquals(response.status_code, 200)
def failUnlessWarns(self, category, message, f, *args, **kwargs):
warningsShown = []
Oops, something went wrong.

0 comments on commit 8e0965c

Please sign in to comment.