Browse Source

Fix some pagination issues

Getty Ritter 4 years ago
parent
commit
f4d3bdcf79
2 changed files with 12 additions and 15 deletions
  1. 1 1
      lc/app.py
  2. 11 14
      lc/model.py

+ 1 - 1
lc/app.py

@@ -220,7 +220,7 @@ class EditLink(Endpoint):
 class GetTaggedLinks(Endpoint):
     def html(self, user: str, tag: str):
         u = m.User.by_slug(user)
-        pg = int(flask.request.args.get("page", 0))
+        pg = int(flask.request.args.get("page", 1))
         t = u.get_tag(tag)
         links, pages = t.get_links(as_user=self.user, page=pg)
         tags = u.get_related_tags(t)

+ 11 - 14
lc/model.py

@@ -84,14 +84,12 @@ class User(Model):
     def get_links(
         self, as_user: Optional["User"], page: int
     ) -> Tuple[List[v.Link], v.Pagination]:
-        links = (
-            Link.select()
-            .where((Link.user == self) & ((self == as_user) | (Link.private == False)))
-            .order_by(-Link.created)
-            .paginate(page, c.per_page)
+        query = Link.select().where(
+            (Link.user == self) & ((self == as_user) | (Link.private == False))
         )
+        links = query.order_by(-Link.created).paginate(page, c.per_page)
         link_views = [l.to_view(as_user) for l in links]
-        pagination = v.Pagination.from_total(page, Link.select().count())
+        pagination = v.Pagination.from_total(page, query.count())
         return link_views, pagination
 
     def get_link(self, link_id: int) -> "Link":
@@ -269,20 +267,19 @@ class Tag(Model):
     def get_links(
         self, as_user: Optional[User], page: int
     ) -> Tuple[List[Link], v.Pagination]:
-        links = [
-            ht.link.to_view(as_user)
-            for ht in HasTag.select()
+        query = (
+            HasTag.select()
             .join(Link)
             .where(
                 (HasTag.tag == self)
                 & ((HasTag.link.user == as_user) | (HasTag.link.private == False))
             )
-            .order_by(-Link.created)
-            .paginate(page, c.per_page)
-        ]
-        pagination = v.Pagination.from_total(
-            page, HasTag.select().where((HasTag.tag == self)).count(),
         )
+        links = [
+            ht.link.to_view(as_user)
+            for ht in query.order_by(-Link.created).paginate(page, c.per_page)
+        ]
+        pagination = v.Pagination.from_total(page, query.count())
         return links, pagination
 
     def get_family(self) -> Iterator["Tag"]: