]> git.ipfire.org Git - ipfire.org.git/commitdiff
blog: added a working search bar
authorRico Hoppe <rico.hoppe@ipfire.org>
Mon, 26 Jun 2023 16:12:41 +0000 (16:12 +0000)
committerRico Hoppe <rico.hoppe@ipfire.org>
Mon, 26 Jun 2023 16:12:41 +0000 (16:12 +0000)
Signed-off-by: Rico Hoppe <rico.hoppe@ipfire.org>
src/backend/blog.py
src/templates/blog/index.html
src/web/blog.py

index a22098428a26cf6977634ac6471ffc77cae1cc29..43f3706fa0698618b9e2b307ae7b2a86d7805964 100644 (file)
@@ -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)
 
index 9393a96a0427fba7fed6b41bb8e3ff7b24fdfb45..82d411725ca56b41dc518806045ac7eaa001e106 100644 (file)
                                </nav>
 
                                <h1 class="title is-1">{{ _("IPFire Blog") }}</h1>
+
+                               {% if q %}
+                                       <h6 class="subtitle is-5">
+                                               {{ _("Search Results for '%s'") % q }}
+                                       </h6>
+                               {% end %}
                        </div>
                </div>
        </section>
                                                </div>
                                        {% end %}
 
+                                       {% if q and not posts %}
+                                               <div class="notification">
+                                                       {{ _("No Results Found For '%s'") % q }}
+                                               </div>
+                                       {% end %}
+
                                        {% module BlogList(posts, relative=True) %}
                                </div>
 
                                <div class="column is-4">
                                        <div class="block">
-                                               <div class="container">
+                                               <form method="GET" action="">
                                                        <div class="field">
-                                                               <p class="control">
-                                                                       <form method="GET" action="/search">
-                                                                               <input class="input is-primary" type="text" name="q" placeholder="Search...">
-                                                                       </form>
-                                                               </p>
+                                                               <div class="control has-icons-left">
+                                                                       <input class="input is-medium" type="text"
+                                                                               name="q" {% if q %}value="{{ q }}"{% end %}
+                                                                               placeholder="{{ _("Search...") }}">
+                                                                       <span class="icon is-small is-left">
+                                                                               <i class="fas fa-search"></i>
+                                                                       </span>
+                                                               </div>
                                                        </div>
-                                               </div>
+                                               </form>
                                        </div>
 
                                        {% module BlogHistoryNavigation() %}
index b4374dc88bfba6fdb3953d55e83ecacb661b10b5..45a06e5486a6810aab05b541b36ba9b8827bb024 100644 (file)
@@ -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):