Add basic support for editing field help info

This commit is contained in:
Lance Edgar 2022-12-24 21:46:02 -06:00
parent 9fe9983bf9
commit 3befdc09e3
6 changed files with 218 additions and 7 deletions

View file

@ -2333,6 +2333,40 @@ class MasterView(View):
info.markdown_text = form.validated['markdown_text']
return {'ok': True}
def edit_field_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='field_name'))
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.TailboneFieldInfo)\
.filter(model.TailboneFieldInfo.route_prefix == route_prefix)\
.filter(model.TailboneFieldInfo.field_name == form.validated['field_name'])\
.first()
if not info:
info = model.TailboneFieldInfo(route_prefix=route_prefix,
field_name=form.validated['field_name'])
Session.add(info)
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.
@ -3944,6 +3978,7 @@ class MasterView(View):
Return a dictionary of kwargs to be passed to the factory when creating
new form instances.
"""
route_prefix = self.get_route_prefix()
defaults = {
'request': self.request,
'readonly': self.viewing,
@ -3951,12 +3986,21 @@ class MasterView(View):
'action_url': self.request.current_route_url(_query=None),
'use_buefy': self.get_use_buefy(),
'assume_local_times': self.has_local_times,
'route_prefix': route_prefix,
'can_edit_help': (self.has_perm('edit_help')
or self.request.has_perm('common.edit_help')),
}
if defaults['can_edit_help']:
defaults['edit_help_url'] = self.request.route_url(
'{}.edit_field_help'.format(route_prefix))
if self.creating:
kwargs.setdefault('cancel_url', self.get_index_url())
else:
instance = kwargs['model_instance']
kwargs.setdefault('cancel_url', self.get_action_url('view', instance))
defaults.update(kwargs)
return defaults
@ -4832,6 +4876,12 @@ class MasterView(View):
config.add_view(cls, attr='edit_help',
route_name='{}.edit_help'.format(route_prefix),
renderer='json')
config.add_route('{}.edit_field_help'.format(route_prefix),
'{}/edit-field-help'.format(url_prefix),
request_method='POST')
config.add_view(cls, attr='edit_field_help',
route_name='{}.edit_field_help'.format(route_prefix),
renderer='json')
# list/search
if cls.listable: