Add basic support for editing page help info

site admin should be able to point help wherever they want
This commit is contained in:
Lance Edgar 2022-12-24 14:56:12 -06:00
parent ed54092268
commit 9fe9983bf9
8 changed files with 315 additions and 28 deletions

View file

@ -287,6 +287,8 @@ class CommonView(View):
# permissions
config.add_tailbone_permission_group('common', "(common)", overwrite=False)
config.add_tailbone_permission('common', 'common.edit_help',
"Edit help info for *any* page")
# home
config.add_route('home', '/')

View file

@ -2267,6 +2267,16 @@ class MasterView(View):
so if you like you can return a different help URL depending on which
type of CRUD view is in effect, etc.
"""
model = self.model
route_prefix = self.get_route_prefix()
# nb. self.Session may differ, so use tailbone.db.Session
info = Session.query(model.TailbonePageHelp)\
.filter(model.TailbonePageHelp.route_prefix == route_prefix)\
.first()
if info and info.help_url:
return info.help_url
if self.help_url:
return self.help_url
@ -2275,6 +2285,54 @@ class MasterView(View):
return global_help_url(self.rattail_config)
def get_help_markdown(self):
"""
Return the markdown help text for current page, if defined.
"""
model = self.model
route_prefix = self.get_route_prefix()
# nb. self.Session may differ, so use tailbone.db.Session
info = Session.query(model.TailbonePageHelp)\
.filter(model.TailbonePageHelp.route_prefix == route_prefix)\
.first()
if info and info.markdown_text:
return info.markdown_text
def edit_help(self):
if (not self.has_perm('edit_help')
and not self.request.has_perm('common.edit_help')):
raise self.forbidden()
model = self.model
route_prefix = self.get_route_prefix()
schema = colander.Schema()
schema.add(colander.SchemaNode(colander.String(),
name='help_url',
missing=None))
schema.add(colander.SchemaNode(colander.String(),
name='markdown_text',
missing=None))
factory = self.get_form_factory()
form = factory(schema=schema, request=self.request)
if not form.validate(newstyle=True):
return {'error': "Form did not validate"}
# nb. self.Session may differ, so use tailbone.db.Session
info = Session.query(model.TailbonePageHelp)\
.filter(model.TailbonePageHelp.route_prefix == route_prefix)\
.first()
if not info:
info = model.TailbonePageHelp(route_prefix=route_prefix)
Session.add(info)
info.help_url = form.validated['help_url']
info.markdown_text = form.validated['markdown_text']
return {'ok': True}
def render_to_response(self, template, data, **kwargs):
"""
Return a response with the given template rendered with the given data.
@ -2296,6 +2354,9 @@ class MasterView(View):
'action_url': self.get_action_url,
'grid_index': self.grid_index,
'help_url': self.get_help_url(),
'help_markdown': self.get_help_markdown(),
'can_edit_help': (self.has_perm('edit_help')
or self.request.has_perm('common.edit_help')),
'quickie': None,
}
@ -4761,6 +4822,17 @@ class MasterView(View):
'prevent_cache_for_index_views',
default=True)
# edit help info
config.add_tailbone_permission(permission_prefix,
'{}.edit_help'.format(permission_prefix),
"Edit help info for {}".format(model_title_plural))
config.add_route('{}.edit_help'.format(route_prefix),
'{}/edit-help'.format(url_prefix),
request_method='POST')
config.add_view(cls, attr='edit_help',
route_name='{}.edit_help'.format(route_prefix),
renderer='json')
# list/search
if cls.listable:
config.add_tailbone_permission(permission_prefix, '{}.list'.format(permission_prefix),