Skip to content

Commit

Permalink
Add new dispatch_hook method transform_stored_query_data, also...
Browse files Browse the repository at this point in the history
... use the new function to transform aranea/xxx to xxx in URL and
stored queries
  • Loading branch information
tomachalek committed Aug 1, 2023
1 parent b5bc6b6 commit de9e587
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/action/model/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,11 @@ async def _restore_prev_query_params(self, req_args: Union[RequestArgsProxy, JSO
True if query params have been loaded else False (which is still not an error)
"""
url_q = req_args.getlist('q')[:]
with plugins.runtime.QUERY_PERSISTENCE as query_persistence:
with plugins.runtime.QUERY_PERSISTENCE as query_persistence, plugins.runtime.DISPATCH_HOOK as dh:
if len(url_q) > 0 and query_persistence.is_valid_id(url_q[0]):
self._q_code = url_q[0][1:]
self._active_q_data = await query_persistence.open(self._q_code)
aqdata = await query_persistence.open(self._q_code)
self._active_q_data = await dh.transform_stored_query_data(aqdata)
# !!! must create a copy here otherwise _q_data (as prev query)
# will be rewritten by self.args.q !!!
if self._active_q_data is not None:
Expand Down
11 changes: 11 additions & 0 deletions lib/plugin_types/dispatch_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ async def post_dispatch(self, plugin_ctx, methodname, action_metadata):
methodname -- processed action method
action_metadata -- action metadata (added by @inject)
"""

async def transform_stored_query_data(self, data):
"""
When KonText processes a URL with a query ID, it always restores required
internal arguments representing the URL using the loaded values. This
function transforms values immediately after they are loaded,
i.e. before they are applied elsewhere.
It can be used e.g. to solve legacy values stored in query persistence database.
"""
return data
17 changes: 16 additions & 1 deletion lib/plugins/ucnk_dispatch_hook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from dataclasses import dataclass

import plugins
from action.errors import ServiceUnavailableException
from action.errors import ServiceUnavailableException, ImmediateRedirectException
from action.plugin.ctx import PluginCtx
from action.props import ActionProps
from dataclasses_json import LetterCase, dataclass_json
Expand Down Expand Up @@ -73,8 +73,23 @@ async def _check_client(self, plugin_ctx: PluginCtx):
await self._db.hash_del(self.bot_clients_key, client_ip)

async def pre_dispatch(self, plugin_ctx, action_props: ActionProps, request):
arg = request.args.get('corpname', [''])[0]
if arg.startswith('aranea/'):
raise ImmediateRedirectException(plugin_ctx.updated_current_url(dict(corpname=arg[len('aranea/'):])))
await self._check_client(plugin_ctx)

async def transform_stored_query_data(self, data):
if 'corpora' in data:
normalized_corpora = []
for corp in data['corpora']:
if corp.startswith('aranea/'):
normalized_corpora.append(corp[len('aranea/'):])
else:
normalized_corpora.append(corp)
data['corpora'] = normalized_corpora
return data



@inject(plugins.runtime.DB)
def create_instance(conf, db: KeyValueStorage):
Expand Down

0 comments on commit de9e587

Please sign in to comment.