Permalink
Browse files

CMSTestCase.create_page_user does no longer use the test client

  • Loading branch information...
1 parent c5281e9 commit bc5900fc09dba654879d3cec3aa36c00176e48c7 Jonas Obrist committed Dec 30, 2010
Showing with 47 additions and 38 deletions.
  1. +26 −25 cms/admin/forms.py
  2. +21 −13 cms/tests/base.py
View
@@ -20,7 +20,31 @@
from django.utils.translation import ugettext_lazy as _, get_language
from menus.menu_pool import menu_pool
+def get_permission_acessor(obj):
+ if isinstance(obj, PageUser):
+ rel_name = 'user_permissions'
+ else:
+ rel_name = 'permissions'
+ return getattr(obj, rel_name)
+def save_permissions(data, obj):
+ models = ((Page, 'page'), (PageUser, 'pageuser'), (PageUserGroup, 'pageuser'), (PagePermission, 'pagepermission'))
+
+ if not obj.pk:
+ # save obj, otherwise we can't assign permissions to him
+ obj.save()
+ permission_acessor = get_permission_acessor(obj)
+
+ for model, name in models:
+ content_type = ContentType.objects.get_for_model(model)
+ for t in ('add', 'change', 'delete'):
+ # add permission `t` to model `model`
+ codename = getattr(model._meta, 'get_%s_permission' % t)()
+ permission = Permission.objects.get(content_type=content_type, codename=codename)
+ if data.get('can_%s_%s' % (t, name), None):
+ permission_acessor.add(permission)
+ else:
+ permission_acessor.remove(permission)
class PageAddForm(forms.ModelForm):
@@ -262,31 +286,8 @@ def populate_initials(self, obj):
initials['can_%s_%s' % (t, name)] = codename in permissions
return initials
- def permission_acessor(self, obj):
- if isinstance(obj, PageUser):
- rel_name = 'user_permissions'
- else:
- rel_name = 'permissions'
- return getattr(obj, rel_name)
-
def save_permissions(self, obj):
- models = ((Page, 'page'), (PageUser, 'pageuser'), (PageUserGroup, 'pageuser'), (PagePermission, 'pagepermission'))
-
- if not obj.pk:
- # save obj, otherwise we can't assign permissions to him
- obj.save()
- permission_acessor = self.permission_acessor(obj)
-
- for model, name in models:
- content_type = ContentType.objects.get_for_model(model)
- for t in ('add', 'change', 'delete'):
- # add permission `t` to model `model`
- codename = getattr(model._meta, 'get_%s_permission' % t)()
- permission = Permission.objects.get(content_type=content_type, codename=codename)
- if self.cleaned_data.get('can_%s_%s' % (t, name), None):
- permission_acessor.add(permission)
- else:
- permission_acessor.remove(permission)
+ save_permissions(self.cleaned_data, obj)
class PageUserForm(UserCreationForm, GenericCmsPermissionForm):
@@ -357,7 +358,7 @@ def save(self, commit=True):
if commit:
user.save()
- self.save_permissions(user)
+ save_permissions(self.cleaned_data, user)
if self.cleaned_data['notify_user']:
mail_page_user_change(user, created, self.cleaned_data['password1'])
View
@@ -1,6 +1,7 @@
+from cms.admin.forms import save_permissions
from cms.models import Title, Page
from cms.models.moderatormodels import ACCESS_PAGE_AND_DESCENDANTS
-from cms.models.permissionmodels import PagePermission
+from cms.models.permissionmodels import PagePermission, PageUser
from cms.models.pluginmodel import CMSPlugin
from cms.plugin_pool import plugin_pool
from cms.plugins.text.models import Text
@@ -250,7 +251,8 @@ def create_page_user(self, username, password=None,
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:
+ """
+ Helper function for creating page user, through form on:
/admin/cms/pageuser/add/
Returns created user.
@@ -261,26 +263,32 @@ def create_page_user(self, username, password=None,
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,
+ '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)
+ try:
+ user = User.objects.get(username=username)
+ except User.DoesNotExist:
+ user = User.objects.create_user(username, 'username@django-cms.org', password)
+ user.is_staff = True
+ user.is_active = True
+ page_user = PageUser(created_by=self.user)
+ for field in [f.name for f in User._meta.local_fields]:
+ setattr(page_user, field, getattr(user, field))
+ user.save()
+ page_user.save()
+ save_permissions(data, page_user)
+ return user
def assign_user_to_page(self, page, user, grant_on=ACCESS_PAGE_AND_DESCENDANTS,
can_add=False, can_change=False, can_delete=False,

0 comments on commit bc5900f

Please sign in to comment.