def user_data(self, handler, instance): if handler.request.method == "POST": data = load_json(handler.request.POST.get('data', "{}")) for user in data.get('existing', []): state = user.get('state', '') if state == 'deleted': try: User.objects.get(pk=user['id']).delete() except (ValueError, User.DoesNotExist): pass elif state in ("added", "modified"): try: u = User.objects.get(pk=user['id']) u.username = user.get('username').strip() u.first_name = user.get('firstname', '') u.last_name = user.get('lastname', '') u.email = user.get('email', '') ## active? except (ValueError, User.DoesNotExist): u = User(username=user.get('username').strip(), first_name=user.get('firstname', ''), last_name=user.get('lastname', ''), email=user.get('email', '')) u.save() password = user.get('password', '').strip() if password: u.set_password(password) u.roles.all().delete() for role, isset in user.get('roles', {}).iteritems(): if isset: WheelRole(role=Role(role), user=u).save() u.save() ## roles users = User.objects.all() data = {} data['existing'] = [dict(id=u.id, username=u.username, firstname=u.first_name, lastname=u.last_name, email=u.email, active=u.is_active, superuser=u.is_superuser, roles=dict((role.role.id, True) for role in u.roles.all()) ) for u in users] data['roles'] = [dict(id=role.id, name=role.name, description=role.description) for role in Role.all()] return data
def auth(self, handler, request, action): ## ## If post, handle/reset perm changes if request.method == "POST": existing = RolePermission.assignments(self.instance) assignments = request.POST.getlist('assignment') for e in existing: if "{0}/{1}".format(e.permission, e.role) not in assignments: e.delete() for assignment in assignments: perm, role = assignment.split('/', 1) RolePermission.assign(self.instance, Role(role), Permission(perm)).save() ctx = {'spoke':self} roles = Role.all() permissions = [] ## order roles, permissions (alphabetically?) for perm in Permission.all(): d = dict(perm=perm, roles=[]) perms_per_role = RolePermission.assignments( self.instance).filter( permission=perm.id, ).values_list('role', flat=True) r = [] for role in roles: r.append(dict(role=role, checked=role.id in perms_per_role)) d['roles'] = r permissions.append(d) ctx['roles'] = roles ctx['permissions'] = permissions return handler.template("wheelcms_axle/edit_permissions.html", **ctx)
def test_all(self): """ retrieve all created permissions """ r1 = Role.create("r1") r2 = Role.create("r2") r3 = Role.create("r2") # duplicate! assert set(Role.all()) == set((r1, r2))
def test_all_empty(self): """ empty role collection """ assert len(Role.all()) == 0
def test_equality(self): """ ordinary comparison. Will succeed since it will be the same objects """ assert Role.create("foo") == Role.create("foo")
def test_uniqueness_differ(self): """ Role with different identifier are not equal """ assert Role.create("foo") != Role("bar")
def test_uniqueness_attrs(self): """ Different instances with same identifier are identical, even if other attributes differ """ assert Role.create("foo", "bar", "bla") is \ Role.create("foo", "this", "that")
def test_uniqueness_id(self): """ Different instances with same identifier are identical """ assert Role.create("foo") is Role.create("foo")
def test_create(self): """ Role are implicitly created """ assert Role.create("foo") is not None
def test_unicode(self): assert isinstance(Role.create("foo").__unicode__(), unicode)
def Role(id, name="", description=""): return droleRole.create(id, name, description)