Overhaul the "find by perm" feature a bit

use GET instead of POST on form submit, so can more easily share URL
for a particular result

also get rid of WTForms dependency!  sheesh

results table is still not pretty but..feeling lazy
This commit is contained in:
Lance Edgar 2023-03-25 13:03:47 -05:00
parent 45b8d9fb84
commit e96f8844e2
3 changed files with 52 additions and 65 deletions

View file

@ -29,7 +29,6 @@ import copy
from rattail.core import Object
from rattail.util import OrderedDict
import wtforms
from webhelpers2.html import HTML
from tailbone.db import Session
@ -54,40 +53,32 @@ class PrincipalMasterView(MasterView):
"""
View for finding all users who have been granted a given permission
"""
permissions = copy.deepcopy(self.request.registry.settings.get('tailbone_permissions', {}))
permissions = copy.deepcopy(
self.request.registry.settings.get('tailbone_permissions', {}))
# sort groups, and permissions for each group, for UI's sake
sorted_perms = sorted(permissions.items(), key=self.perm_sortkey)
for key, group in sorted_perms:
group['perms'] = sorted(group['perms'].items(), key=self.perm_sortkey)
# group options are stable, permission options may depend on submitted group
group_choices = [(gkey, group['label']) for gkey, group in sorted_perms]
permission_choices = [('_any_', "(any)")]
if self.request.method == 'POST':
if self.request.POST.get('permission_group') in permissions:
permission_choices.extend([
(pkey, perm['label'])
for pkey, perm in permissions[self.request.POST['permission_group']]['perms']
])
class PermissionForm(wtforms.Form):
permission_group = wtforms.SelectField(choices=group_choices)
permission = wtforms.SelectField(choices=permission_choices)
# if both field values are in query string, do lookup
principals = None
form = PermissionForm(self.request.POST)
if self.request.method == 'POST' and form.validate():
permission = form.permission.data
principals = self.find_principals_with_permission(self.Session(), permission)
permission_group = self.request.GET.get('permission_group')
permission = self.request.GET.get('permission')
if permission_group and permission:
principals = self.find_principals_with_permission(self.Session(),
permission)
else: # otherwise clear both values
permission_group = None
permission = None
context = {'form': form, 'permissions': sorted_perms, 'principals': principals}
context = {'permissions': sorted_perms, 'principals': principals}
perms = self.get_buefy_perms_data(sorted_perms)
context['buefy_perms'] = perms
context['buefy_sorted_groups'] = list(perms)
context['selected_group'] = self.request.POST.get('permission_group', 'common')
context['selected_permission'] = self.request.POST.get('permission', None)
context['selected_group'] = permission_group or 'common'
context['selected_permission'] = permission
return self.render_to_response('find_by_perm', context)
@ -123,8 +114,11 @@ class PrincipalMasterView(MasterView):
model_title_plural = cls.get_model_title_plural()
# find principal by permission
config.add_route('{}.find_by_perm'.format(route_prefix), '{}/find-by-perm'.format(url_prefix))
config.add_view(cls, attr='find_by_perm', route_name='{}.find_by_perm'.format(route_prefix),
config.add_route('{}.find_by_perm'.format(route_prefix),
'{}/find-by-perm'.format(url_prefix),
request_method='GET')
config.add_view(cls, attr='find_by_perm',
route_name='{}.find_by_perm'.format(route_prefix),
permission='{}.find_by_perm'.format(permission_prefix))
config.add_tailbone_permission(permission_prefix, '{}.find_by_perm'.format(permission_prefix),
"Find all {} with permission X".format(model_title_plural))