From: Rico Hoppe Date: Mon, 26 Jun 2023 16:12:41 +0000 (+0000) Subject: blog: added a working search bar X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d871047197288a6df1c554f4b7dd1c435d0dfb42;p=ipfire.org.git blog: added a working search bar Signed-off-by: Rico Hoppe --- diff --git a/src/backend/blog.py b/src/backend/blog.py index a2209842..43f3706f 100644 --- a/src/backend/blog.py +++ b/src/backend/blog.py @@ -81,12 +81,14 @@ class Blog(misc.Object): ORDER BY COALESCE(updated_at, created_at) DESC LIMIT %s", limit) def search(self, query, limit=None): - return self._get_posts("SELECT blog.* FROM blog \ + posts = self._get_posts("SELECT blog.* FROM blog \ LEFT JOIN blog_search_index search_index ON blog.id = search_index.post_id \ WHERE search_index.document @@ websearch_to_tsquery('english', %s) \ ORDER BY ts_rank(search_index.document, websearch_to_tsquery('english', %s)) DESC \ LIMIT %s", query, query, limit) + return list(posts) + def has_had_recent_activity(self, **kwargs): t = datetime.timedelta(**kwargs) diff --git a/src/templates/blog/index.html b/src/templates/blog/index.html index 9393a96a..82d41172 100644 --- a/src/templates/blog/index.html +++ b/src/templates/blog/index.html @@ -22,6 +22,12 @@

{{ _("IPFire Blog") }}

+ + {% if q %} +
+ {{ _("Search Results for '%s'") % q }} +
+ {% end %} @@ -48,20 +54,29 @@ {% end %} + {% if q and not posts %} +
+ {{ _("No Results Found For '%s'") % q }} +
+ {% end %} + {% module BlogList(posts, relative=True) %}
-
+
-

- - -

-

+
+ + + + +
-
+
{% module BlogHistoryNavigation() %} diff --git a/src/web/blog.py b/src/web/blog.py index b4374dc8..45a06e54 100644 --- a/src/web/blog.py +++ b/src/web/blog.py @@ -10,16 +10,23 @@ from . import ui_modules class IndexHandler(base.BaseHandler): def get(self): - posts = self.backend.blog.get_newest(limit=10) + latest_post = None - # Allow this to be cached for 5 minutes - if not self.current_user: - self.set_expires(300) + # Fetch the search query + q = self.get_argument("q", None) + + # If the user is searching, perform the search + if q: + posts = self.backend.blog.search(q) + + # Otherwise fetch the latest posts + else: + posts = self.backend.blog.get_newest(limit=10) - # Extract the latest post - latest_post = posts.pop(0) + # Extract the latest post + latest_post = posts.pop(0) - self.render("blog/index.html", posts=posts, latest_post=latest_post) + self.render("blog/index.html", q=q, posts=posts, latest_post=latest_post) class AuthorHandler(base.BaseHandler):