Browse Source

Merge branch 'gr/text-search' of getty/lament-configuration into master

getty 1 year ago
parent
commit
399559311a
5 changed files with 58 additions and 0 deletions
  1. 18 0
      lc/app.py
  2. 16 0
      lc/model.py
  3. 11 0
      static/lc.js
  4. 8 0
      static/main.css
  5. 5 0
      templates/main.mustache

+ 18 - 0
lc/app.py

@@ -235,6 +235,24 @@ class GetTaggedLinks(Endpoint):
         )
 
 
+@endpoint("/u/<string:user>/search/<string:needle>")
+class GetStringSearch(Endpoint):
+    def html(self, user: str, needle: str):
+        u = m.User.by_slug(user)
+        pg = int(flask.request.args.get("page", 1))
+        links, pages = u.get_string_search(needle=needle, as_user=self.user, page=pg)
+        tags = u.get_tags()
+        linklist = v.LinkList(links=links, pages=pages, tags=tags, user=user)
+        return render(
+            "main",
+            v.Page(
+                title=f"search for '{needle}'",
+                content=render("linklist", linklist),
+                user=self.user,
+            ),
+        )
+
+
 @endpoint("/u/<string:user>/import")
 class PinboardImport(Endpoint):
     def html(self, user: str):

+ 16 - 0
lc/model.py

@@ -179,6 +179,22 @@ class User(Model):
         )
         return sorted((t.tag.to_view() for t in query), key=lambda t: t.name,)
 
+    def get_string_search(
+        self, needle: str, as_user: Optional["User"], page: int
+    ) -> Tuple[List[v.Link], v.Pagination]:
+        """
+        Find all links that contain the string `needle` somewhere in their URL or title
+        """
+        query = Link.select().where(
+            (Link.user == self)
+            & ((self == as_user) | (Link.private == False))
+            & (Link.name.contains(needle) | Link.description.contains(needle))
+        )
+        links = query.order_by(-Link.created).paginate(page, c.app.per_page)
+        link_views = [l.to_view(as_user) for l in links]
+        pagination = v.Pagination.from_total(page, query.count())
+        return link_views, pagination
+
 
 class Link(Model):
     """

+ 11 - 0
static/lc.js

@@ -51,4 +51,15 @@ $(document).ready(() => {
                 .catch(err => window.location.href = url);
         });
     }
+
+    let searchText = $("#search_text");
+    console.log(`search text: ${searchText}`);
+    searchText.on('keypress', function (e) {
+        if (e.which == 13) {
+            let user = searchText.data('user');
+            let search = searchText.val();
+            window.location.href = `/u/${user}/search/${search}`
+            return false;
+        }
+    });
 });

+ 8 - 0
static/main.css

@@ -221,3 +221,11 @@ form > div {
     margin-left: 1em;
     padding-left: 0.3em;
 }
+
+.search {
+    padding: 0em 2em;
+}
+
+#search_text {
+    width: 20em;
+}

+ 5 - 0
templates/main.mustache

@@ -21,6 +21,11 @@
         {{/user}}
       </div>
       <div class="right">
+        {{#user}}
+        <div class="search">
+          <input data-user="{{name}}" type="text" id="search_text"/>
+        </div>
+        {{/user}}
         <div class="srclink">
           <a href="https://git.infinitenegativeutility.com/getty/lament-configuration">source</a>
         </div>