Refactor autocomplete view logic to leverage new "autocompleters"

finally!  this cleans up some view config and AFAIK there is no loss
in functionality etc.
This commit is contained in:
Lance Edgar 2021-09-30 19:26:57 -04:00
parent e0dff55ffa
commit a7f4b2e6ef
12 changed files with 65 additions and 375 deletions

View file

@ -97,6 +97,7 @@ class MasterView(View):
delete_confirm = 'full'
bulk_deletable = False
set_deletable = False
supports_autocomplete = False
supports_set_enabled_toggle = False
populatable = False
mergeable = False
@ -3573,6 +3574,35 @@ class MasterView(View):
return self.after_delete_url
return self.get_index_url()
##############################
# Autocomplete Stuff
##############################
def autocomplete(self):
"""
View which accepts a single ``term`` param, and returns a list
of autocomplete results to match.
"""
app = self.get_rattail_app()
key = self.get_autocompleter_key()
# url may include key, for more specific autocompleter
if 'key' in self.request.matchdict:
key = '{}.{}'.format(key, self.request.matchdict['key'])
autocompleter = app.get_autocompleter(key)
term = self.request.params.get('term', '')
return autocompleter.autocomplete(self.Session(), term)
def get_autocompleter_key(self):
"""
Must return the "key" to be used when locating the
Autocompleter object, for use with autocomplete view.
"""
if hasattr(self, 'autocompleter_key'):
if self.autocompleter_key:
return self.autocompleter_key
return self.get_route_prefix()
##############################
# Associated Rows Stuff
##############################
@ -3965,6 +3995,25 @@ class MasterView(View):
config.add_view(cls, attr='quickie', route_name='{}.quickie'.format(route_prefix),
permission='{}.quickie'.format(permission_prefix))
# autocomplete
if cls.supports_autocomplete:
# default
config.add_route('{}.autocomplete'.format(route_prefix),
'{}/autocomplete'.format(url_prefix))
config.add_view(cls, attr='autocomplete',
route_name='{}.autocomplete'.format(route_prefix),
renderer='json',
permission='{}.list'.format(permission_prefix))
# special
config.add_route('{}.autocomplete_special'.format(route_prefix),
'{}/autocomplete/{{key}}'.format(url_prefix))
config.add_view(cls, attr='autocomplete',
route_name='{}.autocomplete_special'.format(route_prefix),
renderer='json',
permission='{}.list'.format(permission_prefix))
# create
if cls.creatable:
config.add_tailbone_permission(permission_prefix, '{}.create'.format(permission_prefix),