def test_unassign_user(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, admin, george, developer, _, _ = self._get_foo_site_objs( ) self.client.login(username='******', password='******') response = self.client.post( '/admin/cmsroles/usersetup/?site=%s' % foo_site.pk, { # management form u'user-roles-MAX_NUM_FORMS': [u''], u'user-roles-TOTAL_FORMS': [u'2'], u'user-roles-INITIAL_FORMS': [u'2'], # joe remains an admin u'user-roles-0-user': [unicode(joe.pk)], u'user-roles-0-role': [unicode(admin.pk)], # george remains a developer u'user-roles-1-user': [unicode(george.pk)], u'user-roles-1-role': [unicode(developer.pk)], # but robin gets removed !! u'next': [u'continue'] }) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict( (u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 2) self.assertEqual(user_pks_to_role_pks[joe.pk], admin.pk) self.assertEqual(user_pks_to_role_pks[george.pk], developer.pk)
def test_assign_new_user(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, admin, george, developer, robin, editor = self._get_foo_site_objs() criss = User.objects.get(username='******') self.client.login(username='******', password='******') response = self.client.post('/admin/cmsroles/usersetup/?site=%s' % foo_site.pk, { # management form u'user-roles-MAX_NUM_FORMS': [u''], u'user-roles-TOTAL_FORMS': [u'4'], u'user-roles-INITIAL_FORMS': [u'4'], # joe remains an admin u'user-roles-0-user': [unicode(joe.pk)], u'user-roles-0-role': [unicode(admin.pk)], # george remains a developer u'user-roles-1-user': [unicode(george.pk)], u'user-roles-1-role': [unicode(developer.pk)], # robin remains an editor u'user-roles-2-user': [unicode(robin.pk)], u'user-roles-2-role': [unicode(editor.pk)], # but we also add criss to foo_site u'user-roles-3-user': [unicode(criss.pk)], u'user-roles-3-role': [unicode(admin.pk)], u'next': [u'continue']} ) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict((u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 4) self.assertEqual(user_pks_to_role_pks[joe.pk], admin.pk) self.assertEqual(user_pks_to_role_pks[george.pk], developer.pk) self.assertEqual(user_pks_to_role_pks[robin.pk], editor.pk) self.assertEqual(user_pks_to_role_pks[criss.pk], admin.pk)
def test_change_user_pages(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, _, george, developer, robin, editor = self._get_foo_site_objs() master_page = self._create_pages(foo_site) news_page = Page.objects.get(title_set__title='news', parent=master_page) writer = Role.objects.get(name='writer') self.client.login(username='******', password='******') response = self.client.post('/admin/cmsroles/usersetup/?site=%s' % foo_site.pk, { # management form u'user-roles-MAX_NUM_FORMS': [u''], u'user-roles-TOTAL_FORMS': [u'1'], u'user-roles-INITIAL_FORMS': [u'1'], # make jow a writer u'user-roles-0-user': [unicode(joe.pk)], u'user-roles-0-role': [unicode(writer.pk)], (u'user-%d-MAX_NUM_FORMS' % joe.pk): u'', (u'user-%d-TOTAL_FORMS' % joe.pk): u'1', (u'user-%d-INITIAL_FORMS' % joe.pk): u'1', # and give him access to the news page (u'user-%d-0-page' % joe.pk): u'%d' % news_page.pk, u'next': [u'continue']} ) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict((u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 1) self.assertEqual(user_pks_to_role_pks[joe.pk], writer.pk) page_perms = writer.get_user_page_perms(joe, foo_site) self.assertEqual(len(page_perms), 1) perm_to_news = page_perms[0] self.assertEqual(perm_to_news.page, news_page)
def test_unassign_user(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, admin, george, developer, _, _ = self._get_foo_site_objs() self.client.login(username='******', password='******') response = self.client.post('/admin/cmsroles/usersetup/?site=%s' % foo_site.pk, { # management form u'user-roles-MAX_NUM_FORMS': [u''], u'user-roles-TOTAL_FORMS': [u'2'], u'user-roles-INITIAL_FORMS': [u'2'], # joe remains an admin u'user-roles-0-user': [unicode(joe.pk)], u'user-roles-0-role': [unicode(admin.pk)], # george remains a developer u'user-roles-1-user': [unicode(george.pk)], u'user-roles-1-role': [unicode(developer.pk)], # but robin gets removed !! u'next': [u'continue']} ) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict((u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 2) self.assertEqual(user_pks_to_role_pks[joe.pk], admin.pk) self.assertEqual(user_pks_to_role_pks[george.pk], developer.pk)
def test_change_roles(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, _, george, developer, robin, editor = self._get_foo_site_objs() self.client.login(username="******", password="******") response = self.client.post( "/admin/cmsroles/usersetup/?site=%s" % foo_site.pk, { # management form u"user-roles-MAX_NUM_FORMS": [u""], u"user-roles-TOTAL_FORMS": [u"3"], u"user-roles-INITIAL_FORMS": [u"3"], # change joe to a developer u"user-roles-0-user": [unicode(joe.pk)], u"user-roles-0-role": [unicode(developer.pk)], # george to an editor u"user-roles-1-user": [unicode(george.pk)], u"user-roles-1-role": [unicode(editor.pk)], # robin stays the same u"user-roles-2-user": [unicode(robin.pk)], u"user-roles-2-role": [unicode(editor.pk)], u"next": [u"continue"], }, ) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict((u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 3) self.assertEqual(user_pks_to_role_pks[joe.pk], developer.pk) self.assertEqual(user_pks_to_role_pks[george.pk], editor.pk) self.assertEqual(user_pks_to_role_pks[robin.pk], editor.pk)
def user_setup(request): site_pk = _get_site_pk(request) current_site, administered_sites = _get_user_sites(request.user, site_pk) UserFormSet = formset_factory(UserForm, formset=BaseUserFormSet, extra=1) assigned_users = get_site_users(current_site) PageFormSet = formset_factory( _get_page_form_class(current_site), formset=BasePageFormSet, extra=1) page_formsets = {} if request.method == 'POST': user_formset = UserFormSet(request.POST, request.FILES, prefix='user-roles') user_pages = {} if user_formset.is_valid(): submitted_users = {} page_formsets_have_errors = False for user_form in user_formset.forms: user = user_form.cleaned_data.get('user', None) role = user_form.cleaned_data.get('role', None) if user is None and role is None: continue submitted_users[user] = role if not role.is_site_wide and _formset_available(request, user): page_formset = PageFormSet( request.POST, request.FILES, prefix=_get_page_formset_prefix(user)) if page_formset.is_valid(): user_pages[user] = _get_user_pages(page_formset) else: page_formsets[unicode(user.pk)] = page_formset page_formsets_have_errors = True if not page_formsets_have_errors: _update_site_users(request, current_site, assigned_users, submitted_users, user_pages) return _get_redirect(request, site_pk) else: initial_data = [ {'user': user, 'role': role, 'current_site': current_site} for user, role in assigned_users.iteritems()] user_formset = UserFormSet(initial=initial_data, prefix='user-roles') all_roles = Role.objects.all() role_pk_to_site_wide = dict((role.pk, role.is_site_wide) for role in all_roles) # so that the empty form template doesn't have an 'assign pages' link role_pk_to_site_wide[None] = True context = { 'opts': {'app_label': 'cmsroles'}, 'app_label': 'Cmsroles', 'administered_sites': administered_sites, 'current_site': current_site, 'user_formset': user_formset, 'page_formsets': page_formsets, 'user': request.user, 'role_pk_to_site_wide_js': [ (role.pk, 'true' if role.is_site_wide else 'false') for role in all_roles], 'role_pk_to_site_wide': role_pk_to_site_wide} return render_to_response('admin/cmsroles/user_setup.html', context, context_instance=RequestContext(request))
def test_change_user_pages(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, _, george, developer, robin, editor = self._get_foo_site_objs( ) master_page = self._create_pages(foo_site) news_page = Page.objects.get(title_set__title='news', parent=master_page) writer = Role.objects.get(name='writer') self.client.login(username='******', password='******') response = self.client.post( '/admin/cmsroles/usersetup/?site=%s' % foo_site.pk, { # management form u'user-roles-MAX_NUM_FORMS': [u''], u'user-roles-TOTAL_FORMS': [u'1'], u'user-roles-INITIAL_FORMS': [u'1'], # make jow a writer u'user-roles-0-user': [unicode(joe.pk)], u'user-roles-0-role': [unicode(writer.pk)], (u'user-%d-MAX_NUM_FORMS' % joe.pk): u'', (u'user-%d-TOTAL_FORMS' % joe.pk): u'1', (u'user-%d-INITIAL_FORMS' % joe.pk): u'1', # and give him access to the news page (u'user-%d-0-page' % joe.pk): u'%d' % news_page.pk, u'next': [u'continue'] }) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict( (u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 1) self.assertEqual(user_pks_to_role_pks[joe.pk], writer.pk) page_perms = writer.get_user_page_perms(joe, foo_site) self.assertEqual(len(page_perms), 1) perm_to_news = page_perms[0] self.assertEqual(perm_to_news.page, news_page)
def test_assign_new_user(self): self._create_simple_setup() # users assigned to foo.site.com: # joe: site admin, george: developer, robin: editor foo_site, joe, admin, george, developer, robin, editor = self._get_foo_site_objs( ) criss = User.objects.get(username='******') self.client.login(username='******', password='******') response = self.client.post( '/admin/cmsroles/usersetup/?site=%s' % foo_site.pk, { # management form u'user-roles-MAX_NUM_FORMS': [u''], u'user-roles-TOTAL_FORMS': [u'4'], u'user-roles-INITIAL_FORMS': [u'4'], # joe remains an admin u'user-roles-0-user': [unicode(joe.pk)], u'user-roles-0-role': [unicode(admin.pk)], # george remains a developer u'user-roles-1-user': [unicode(george.pk)], u'user-roles-1-role': [unicode(developer.pk)], # robin remains an editor u'user-roles-2-user': [unicode(robin.pk)], u'user-roles-2-role': [unicode(editor.pk)], # but we also add criss to foo_site u'user-roles-3-user': [unicode(criss.pk)], u'user-roles-3-role': [unicode(admin.pk)], u'next': [u'continue'] }) self.assertEqual(response.status_code, 302) users_to_roles = get_site_users(foo_site) user_pks_to_role_pks = dict( (u.pk, r.pk) for u, r in users_to_roles.iteritems()) self.assertEqual(len(user_pks_to_role_pks), 4) self.assertEqual(user_pks_to_role_pks[joe.pk], admin.pk) self.assertEqual(user_pks_to_role_pks[george.pk], developer.pk) self.assertEqual(user_pks_to_role_pks[robin.pk], editor.pk) self.assertEqual(user_pks_to_role_pks[criss.pk], admin.pk)