fix: add basic views for raw Permissions
This commit is contained in:
parent
3cad7f1b13
commit
e952e1031e
|
@ -382,6 +382,30 @@ class PersonRef(ObjectRef):
|
||||||
return self.request.route_url('people.view', uuid=person.uuid)
|
return self.request.route_url('people.view', uuid=person.uuid)
|
||||||
|
|
||||||
|
|
||||||
|
class RoleRef(ObjectRef):
|
||||||
|
"""
|
||||||
|
Custom schema type for a
|
||||||
|
:class:`~wuttjamaican:wuttjamaican.db.model.auth.Role` reference
|
||||||
|
field.
|
||||||
|
|
||||||
|
This is a subclass of :class:`ObjectRef`.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def model_class(self):
|
||||||
|
""" """
|
||||||
|
model = self.app.model
|
||||||
|
return model.Role
|
||||||
|
|
||||||
|
def sort_query(self, query):
|
||||||
|
""" """
|
||||||
|
return query.order_by(self.model_class.name)
|
||||||
|
|
||||||
|
def get_object_url(self, role):
|
||||||
|
""" """
|
||||||
|
return self.request.route_url('roles.view', uuid=role.uuid)
|
||||||
|
|
||||||
|
|
||||||
class UserRef(ObjectRef):
|
class UserRef(ObjectRef):
|
||||||
"""
|
"""
|
||||||
Custom schema type for a
|
Custom schema type for a
|
||||||
|
|
|
@ -168,6 +168,11 @@ class MenuHandler(GenericHandler):
|
||||||
'route': 'roles',
|
'route': 'roles',
|
||||||
'perm': 'roles.list',
|
'perm': 'roles.list',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
'title': "Permissions",
|
||||||
|
'route': 'permissions',
|
||||||
|
'perm': 'permissions.list',
|
||||||
|
},
|
||||||
{'type': 'sep'},
|
{'type': 'sep'},
|
||||||
{
|
{
|
||||||
'title': "App Info",
|
'title': "App Info",
|
||||||
|
|
|
@ -583,6 +583,7 @@
|
||||||
|
|
||||||
const WholePage = {
|
const WholePage = {
|
||||||
template: '#whole-page-template',
|
template: '#whole-page-template',
|
||||||
|
mixins: [WuttaRequestMixin],
|
||||||
computed: {},
|
computed: {},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
|
|
|
@ -24,11 +24,11 @@
|
||||||
Views for roles
|
Views for roles
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from wuttjamaican.db.model import Role
|
from wuttjamaican.db.model import Role, Permission
|
||||||
from wuttaweb.views import MasterView
|
from wuttaweb.views import MasterView
|
||||||
from wuttaweb.db import Session
|
from wuttaweb.db import Session
|
||||||
from wuttaweb.forms import widgets
|
from wuttaweb.forms import widgets
|
||||||
from wuttaweb.forms.schema import UserRefs, Permissions
|
from wuttaweb.forms.schema import UserRefs, Permissions, RoleRef
|
||||||
|
|
||||||
|
|
||||||
class RoleView(MasterView):
|
class RoleView(MasterView):
|
||||||
|
@ -273,12 +273,74 @@ class RoleView(MasterView):
|
||||||
f"Edit the Built-in {model_title_plural}")
|
f"Edit the Built-in {model_title_plural}")
|
||||||
|
|
||||||
|
|
||||||
|
class PermissionView(MasterView):
|
||||||
|
"""
|
||||||
|
Master view for permissions.
|
||||||
|
|
||||||
|
Default route prefix is ``permissions``.
|
||||||
|
|
||||||
|
Notable URLs provided by this class:
|
||||||
|
|
||||||
|
* ``/permissions/``
|
||||||
|
* ``/permissions/XXX``
|
||||||
|
* ``/permissions/XXX/delete``
|
||||||
|
"""
|
||||||
|
model_class = Permission
|
||||||
|
creatable = False
|
||||||
|
editable = False
|
||||||
|
|
||||||
|
grid_columns = [
|
||||||
|
'role',
|
||||||
|
'permission',
|
||||||
|
]
|
||||||
|
|
||||||
|
sort_defaults = 'role'
|
||||||
|
|
||||||
|
form_fields = [
|
||||||
|
'role',
|
||||||
|
'permission',
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_query(self, **kwargs):
|
||||||
|
""" """
|
||||||
|
query = super().get_query(**kwargs)
|
||||||
|
model = self.app.model
|
||||||
|
|
||||||
|
# always join on Role
|
||||||
|
query = query.join(model.Role)
|
||||||
|
|
||||||
|
return query
|
||||||
|
|
||||||
|
def configure_grid(self, g):
|
||||||
|
""" """
|
||||||
|
super().configure_grid(g)
|
||||||
|
model = self.app.model
|
||||||
|
|
||||||
|
# role
|
||||||
|
g.set_sorter('role', model.Role.name)
|
||||||
|
g.set_filter('role', model.Role.name, label="Role Name")
|
||||||
|
g.set_link('role')
|
||||||
|
|
||||||
|
# permission
|
||||||
|
g.set_link('permission')
|
||||||
|
|
||||||
|
def configure_form(self, f):
|
||||||
|
""" """
|
||||||
|
super().configure_form(f)
|
||||||
|
|
||||||
|
# role
|
||||||
|
f.set_node('role', RoleRef(self.request))
|
||||||
|
|
||||||
|
|
||||||
def defaults(config, **kwargs):
|
def defaults(config, **kwargs):
|
||||||
base = globals()
|
base = globals()
|
||||||
|
|
||||||
RoleView = kwargs.get('RoleView', base['RoleView'])
|
RoleView = kwargs.get('RoleView', base['RoleView'])
|
||||||
RoleView.defaults(config)
|
RoleView.defaults(config)
|
||||||
|
|
||||||
|
PermissionView = kwargs.get('PermissionView', base['PermissionView'])
|
||||||
|
PermissionView.defaults(config)
|
||||||
|
|
||||||
|
|
||||||
def includeme(config):
|
def includeme(config):
|
||||||
defaults(config)
|
defaults(config)
|
||||||
|
|
|
@ -255,6 +255,30 @@ class TestPersonRef(WebTestCase):
|
||||||
self.assertIn(f'/people/{person.uuid}', url)
|
self.assertIn(f'/people/{person.uuid}', url)
|
||||||
|
|
||||||
|
|
||||||
|
class TestRoleRef(WebTestCase):
|
||||||
|
|
||||||
|
def test_sort_query(self):
|
||||||
|
typ = mod.RoleRef(self.request, session=self.session)
|
||||||
|
query = typ.get_query()
|
||||||
|
self.assertIsInstance(query, orm.Query)
|
||||||
|
sorted_query = typ.sort_query(query)
|
||||||
|
self.assertIsInstance(sorted_query, orm.Query)
|
||||||
|
self.assertIsNot(sorted_query, query)
|
||||||
|
|
||||||
|
def test_get_object_url(self):
|
||||||
|
self.pyramid_config.add_route('roles.view', '/roles/{uuid}')
|
||||||
|
model = self.app.model
|
||||||
|
typ = mod.RoleRef(self.request, session=self.session)
|
||||||
|
|
||||||
|
role = model.Role(name='Manager')
|
||||||
|
self.session.add(role)
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
|
url = typ.get_object_url(role)
|
||||||
|
self.assertIsNotNone(url)
|
||||||
|
self.assertIn(f'/roles/{role.uuid}', url)
|
||||||
|
|
||||||
|
|
||||||
class TestUserRef(WebTestCase):
|
class TestUserRef(WebTestCase):
|
||||||
|
|
||||||
def test_sort_query(self):
|
def test_sort_query(self):
|
||||||
|
|
|
@ -7,6 +7,7 @@ from sqlalchemy import orm
|
||||||
import colander
|
import colander
|
||||||
|
|
||||||
from wuttaweb.views import roles as mod
|
from wuttaweb.views import roles as mod
|
||||||
|
from wuttaweb.forms.schema import RoleRef
|
||||||
from tests.util import WebTestCase
|
from tests.util import WebTestCase
|
||||||
|
|
||||||
|
|
||||||
|
@ -240,3 +241,33 @@ class TestRoleView(WebTestCase):
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
self.assertIs(role, blokes)
|
self.assertIs(role, blokes)
|
||||||
self.assertEqual(blokes.permissions, ['widgets.polish', 'widgets.view'])
|
self.assertEqual(blokes.permissions, ['widgets.polish', 'widgets.view'])
|
||||||
|
|
||||||
|
|
||||||
|
class TestPermissionView(WebTestCase):
|
||||||
|
|
||||||
|
def make_view(self):
|
||||||
|
return mod.PermissionView(self.request)
|
||||||
|
|
||||||
|
def test_get_query(self):
|
||||||
|
view = self.make_view()
|
||||||
|
query = view.get_query(session=self.session)
|
||||||
|
self.assertIsInstance(query, orm.Query)
|
||||||
|
|
||||||
|
def test_configure_grid(self):
|
||||||
|
model = self.app.model
|
||||||
|
view = self.make_view()
|
||||||
|
grid = view.make_grid(model_class=model.Permission)
|
||||||
|
self.assertFalse(grid.is_linked('role'))
|
||||||
|
view.configure_grid(grid)
|
||||||
|
self.assertTrue(grid.is_linked('role'))
|
||||||
|
|
||||||
|
def test_configure_form(self):
|
||||||
|
model = self.app.model
|
||||||
|
role = model.Role(name="Foo")
|
||||||
|
perm = model.Permission(role=role, permission='whatever')
|
||||||
|
view = self.make_view()
|
||||||
|
form = view.make_form(model_instance=perm)
|
||||||
|
self.assertIsNone(form.schema)
|
||||||
|
view.configure_form(form)
|
||||||
|
schema = form.get_schema()
|
||||||
|
self.assertIsInstance(schema['role'].typ, RoleRef)
|
||||||
|
|
Loading…
Reference in a new issue