diff options
author | Andrew Godwin | 2022-12-12 00:54:51 -0700 |
---|---|---|
committer | Andrew Godwin | 2022-12-12 11:56:49 -0700 |
commit | 35a45f1c55fba69d690929c9420df565e7c5efcc (patch) | |
tree | 0969cf0b9b6c2dec04ec16d0d7444ebff744e377 /api | |
parent | 7f02d51ba04a533391a2c09b5f780fc8b0193ef7 (diff) | |
download | takahe-35a45f1c55fba69d690929c9420df565e7c5efcc.tar.gz takahe-35a45f1c55fba69d690929c9420df565e7c5efcc.tar.bz2 takahe-35a45f1c55fba69d690929c9420df565e7c5efcc.zip |
A few more fixes and a bad test
Diffstat (limited to 'api')
-rw-r--r-- | api/pagination.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/api/pagination.py b/api/pagination.py index 0539ae8..6bd02c9 100644 --- a/api/pagination.py +++ b/api/pagination.py @@ -1,3 +1,6 @@ +from django.db import models + + class MastodonPaginator: """ Paginates in the Mastodon style (max_id, min_id, etc) @@ -5,7 +8,7 @@ class MastodonPaginator: def __init__( self, - anchor_model, + anchor_model: type[models.Model], sort_attribute: str = "created", default_limit: int = 20, max_limit: int = 40, @@ -24,19 +27,28 @@ class MastodonPaginator: limit: int | None, ): if max_id: - anchor = self.anchor_model.objects.get(pk=max_id) + try: + anchor = self.anchor_model.objects.get(pk=max_id) + except self.anchor_model.DoesNotExist: + return [] queryset = queryset.filter( **{self.sort_attribute + "__lt": getattr(anchor, self.sort_attribute)} ) if since_id: - anchor = self.anchor_model.objects.get(pk=since_id) + try: + anchor = self.anchor_model.objects.get(pk=since_id) + except self.anchor_model.DoesNotExist: + return [] queryset = queryset.filter( **{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)} ) if min_id: # Min ID requires items _immediately_ newer than specified, so we # invert the ordering to accomodate - anchor = self.anchor_model.objects.get(pk=min_id) + try: + anchor = self.anchor_model.objects.get(pk=min_id) + except self.anchor_model.DoesNotExist: + return [] queryset = queryset.filter( **{self.sort_attribute + "__gt": getattr(anchor, self.sort_attribute)} ).order_by(self.sort_attribute) |