3
0
Fork 0

Compare commits

..

2 commits

Author SHA1 Message Date
Lance Edgar 1e3647d27b fix: add index_title_controls() def block for base template
for customization purposes
2025-01-24 19:02:53 -06:00
Lance Edgar 194d798c0c fix: add make_users_grid() method for RoleView
per newer convention
2025-01-24 19:00:46 -06:00
3 changed files with 67 additions and 10 deletions

View file

@ -248,12 +248,7 @@
% else: % else:
<h1 class="title">${index_title}</h1> <h1 class="title">${index_title}</h1>
% endif % endif
% if master and master.creatable and not master.creating and master.has_perm('create'): ${self.index_title_controls()}
<wutta-button once type="is-primary"
tag="a" href="${url(f'{route_prefix}.create')}"
icon-left="plus"
label="Create New" />
% endif
% endif % endif
</div> </div>
</div> </div>
@ -349,6 +344,15 @@
</footer> </footer>
</%def> </%def>
<%def name="index_title_controls()">
% if master and master.creatable and not master.creating and master.has_perm('create'):
<wutta-button once type="is-primary"
tag="a" href="${url(f'{route_prefix}.create')}"
icon-left="plus"
label="Create New" />
% endif
</%def>
<%def name="render_vue_template_whole_page()"> <%def name="render_vue_template_whole_page()">
<script type="text/x-template" id="whole-page-template"> <script type="text/x-template" id="whole-page-template">

View file

@ -28,7 +28,7 @@ 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, RoleRef from wuttaweb.forms.schema import Permissions, RoleRef
class RoleView(MasterView): class RoleView(MasterView):
@ -123,9 +123,7 @@ class RoleView(MasterView):
# users # users
if not (self.creating or self.editing): if not (self.creating or self.editing):
f.append('users') f.append('users')
f.set_readonly('users') f.set_grid('users', self.make_users_grid(role))
f.set_node('users', UserRefs(self.request))
f.set_default('users', [u.uuid for u in role.users])
# permissions # permissions
f.append('permissions') f.append('permissions')
@ -134,6 +132,39 @@ class RoleView(MasterView):
if not self.creating: if not self.creating:
f.set_default('permissions', list(role.permissions)) f.set_default('permissions', list(role.permissions))
def make_users_grid(self, role):
"""
Make and return the grid for the Users field.
This grid is shown for the Users field when viewing a Role.
:returns: Fully configured :class:`~wuttaweb.grids.base.Grid`
instance.
"""
model = self.app.model
route_prefix = self.get_route_prefix()
grid = self.make_grid(key=f'{route_prefix}.view.users',
model_class=model.User,
data=role.users,
columns=[
'username',
'person',
'active',
])
if self.request.has_perm('users.view'):
url = lambda user, i: self.request.route_url('users.view', uuid=user.uuid)
grid.add_action('view', icon='eye', url=url)
grid.set_link('person')
grid.set_link('username')
if self.request.has_perm('users.edit'):
url = lambda user, i: self.request.route_url('users.edit', uuid=user.uuid)
grid.add_action('edit', url=url)
return grid
def unique_name(self, node, value): def unique_name(self, node, value):
""" """ """ """
model = self.app.model model = self.app.model

View file

@ -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.grids import Grid
from wuttaweb.forms.schema import RoleRef from wuttaweb.forms.schema import RoleRef
from wuttaweb.testing import WebTestCase from wuttaweb.testing import WebTestCase
@ -93,6 +94,27 @@ class TestRoleView(WebTestCase):
view.configure_form(form) view.configure_form(form)
self.assertIsNotNone(form.validators['name']) self.assertIsNotNone(form.validators['name'])
def test_make_users_grid(self):
model = self.app.model
view = self.make_view()
role = model.Role(name="Manager")
# basic
grid = view.make_users_grid(role)
self.assertIsInstance(grid, Grid)
self.assertFalse(grid.linked_columns)
self.assertFalse(grid.actions)
# view + edit actions
with patch.object(self.request, 'is_root', new=True):
grid = view.make_users_grid(role)
self.assertIsInstance(grid, Grid)
self.assertIn('person', grid.linked_columns)
self.assertIn('username', grid.linked_columns)
self.assertEqual(len(grid.actions), 2)
self.assertEqual(grid.actions[0].key, 'view')
self.assertEqual(grid.actions[1].key, 'edit')
def test_unique_name(self): def test_unique_name(self):
model = self.app.model model = self.app.model
view = self.make_view() view = self.make_view()