3
0
Fork 0

fix: add basic views for raw Permissions

This commit is contained in:
Lance Edgar 2024-12-12 19:24:36 -06:00
parent 3cad7f1b13
commit e952e1031e
6 changed files with 149 additions and 2 deletions

View file

@ -382,6 +382,30 @@ class PersonRef(ObjectRef):
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):
"""
Custom schema type for a

View file

@ -168,6 +168,11 @@ class MenuHandler(GenericHandler):
'route': 'roles',
'perm': 'roles.list',
},
{
'title': "Permissions",
'route': 'permissions',
'perm': 'permissions.list',
},
{'type': 'sep'},
{
'title': "App Info",

View file

@ -583,6 +583,7 @@
const WholePage = {
template: '#whole-page-template',
mixins: [WuttaRequestMixin],
computed: {},
mounted() {

View file

@ -24,11 +24,11 @@
Views for roles
"""
from wuttjamaican.db.model import Role
from wuttjamaican.db.model import Role, Permission
from wuttaweb.views import MasterView
from wuttaweb.db import Session
from wuttaweb.forms import widgets
from wuttaweb.forms.schema import UserRefs, Permissions
from wuttaweb.forms.schema import UserRefs, Permissions, RoleRef
class RoleView(MasterView):
@ -273,12 +273,74 @@ class RoleView(MasterView):
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):
base = globals()
RoleView = kwargs.get('RoleView', base['RoleView'])
RoleView.defaults(config)
PermissionView = kwargs.get('PermissionView', base['PermissionView'])
PermissionView.defaults(config)
def includeme(config):
defaults(config)

View file

@ -255,6 +255,30 @@ class TestPersonRef(WebTestCase):
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):
def test_sort_query(self):

View file

@ -7,6 +7,7 @@ from sqlalchemy import orm
import colander
from wuttaweb.views import roles as mod
from wuttaweb.forms.schema import RoleRef
from tests.util import WebTestCase
@ -240,3 +241,33 @@ class TestRoleView(WebTestCase):
self.session.commit()
self.assertIs(role, blokes)
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)