changeset 1376:1b94a5ab155f

pages (blog/view): Full-Text Search is used when `search` query argument is present the new `LiberviaPage.handleSearch` method implements FTS in PubSub based features.
author Goffi <goffi@goffi.org>
date Fri, 11 Dec 2020 17:35:06 +0100
parents e943b0c8eec5
children 46ce79eac754
files libervia/pages/blog/view/page_meta.py libervia/server/pages.py
diffstat 2 files changed, 28 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/pages/blog/view/page_meta.py	Fri Dec 11 17:35:06 2020 +0100
+++ b/libervia/pages/blog/view/page_meta.py	Fri Dec 11 17:35:06 2020 +0100
@@ -161,9 +161,15 @@
 
 async def prepare_render(self, request):
     data = self.getRData(request)
+    template_data = request.template_data
     page_max = data.get("page_max", 10)
     # if the comments are not explicitly hidden, we show them
-    service, node, item_id, show_comments = data.get('service', ''), data.get('node', ''), data.get('item'), data.get('show_comments', True)
+    service, node, item_id, show_comments = (
+        data.get('service', ''),
+        data.get('node', ''),
+        data.get('item'),
+        data.get('show_comments', True)
+    )
     profile = self.getProfile(request)
     if profile is None:
         profile = C.SERVICE_PROFILE
@@ -178,7 +184,8 @@
         extra = self.getPubsubExtra(request, page_max=page_max)
         tag = data.get('tag')
         if tag:
-            extra['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
+            extra[f'mam_filter_{C.MAM_FILTER_CATEGORY}'] = tag
+        self.handleSearch(request, extra)
 
     ## main data ##
     # we get data from backend/XMPP here
@@ -186,7 +193,6 @@
 
     ## navigation ##
     # no let's fill service, node and pagination URLs
-    template_data = request.template_data
     if 'service' not in template_data:
         template_data['service'] = service
     if 'node' not in template_data:
--- a/libervia/server/pages.py	Fri Dec 11 17:35:06 2020 +0100
+++ b/libervia/server/pages.py	Fri Dec 11 17:35:06 2020 +0100
@@ -1596,6 +1596,25 @@
             request.data = {}
             return request.data
 
+    def handleSearch(self, request, extra):
+        """Manage Full-Text Search
+
+        Check if "search" query argument is present, and add MAM filter for it if
+        necessary.
+        If used, the "search" variable will also be available in template data, thus
+        frontend can display some information about it.
+        """
+        search = self.getPostedData(request, 'search', raise_on_missing=False)
+        if search is not None:
+            search = search.strip()
+            if search:
+                try:
+                    extra[f'mam_filter_{self.host.ns_map["fulltextmam"]}'] = search
+                except KeyError:
+                    log.warning(_("Full-text search is not available"))
+                else:
+                    request.template_data['search'] = search
+
     def _checkAccess(self, request):
         """Check access according to self.access