@@ -637,20 +637,22 @@ def get_next_link(self):
637
637
if not self .has_next :
638
638
return None
639
639
640
- if self .cursor and self .cursor .reverse and self .cursor .offset != 0 :
640
+ if self .page and self . cursor and self .cursor .reverse and self .cursor .offset != 0 :
641
641
# If we're reversing direction and we have an offset cursor
642
642
# then we cannot use the first position we find as a marker.
643
643
compare = self ._get_position_from_instance (self .page [- 1 ], self .ordering )
644
644
else :
645
645
compare = self .next_position
646
646
offset = 0
647
647
648
+ has_item_with_unique_position = False
648
649
for item in reversed (self .page ):
649
650
position = self ._get_position_from_instance (item , self .ordering )
650
651
if position != compare :
651
652
# The item in this position and the item following it
652
653
# have different positions. We can use this position as
653
654
# our marker.
655
+ has_item_with_unique_position = True
654
656
break
655
657
656
658
# The item in this position has the same position as the item
@@ -659,7 +661,7 @@ def get_next_link(self):
659
661
compare = position
660
662
offset += 1
661
663
662
- else :
664
+ if self . page and not has_item_with_unique_position :
663
665
# There were no unique positions in the page.
664
666
if not self .has_previous :
665
667
# We are on the first page.
@@ -678,27 +680,32 @@ def get_next_link(self):
678
680
offset = self .cursor .offset + self .page_size
679
681
position = self .previous_position
680
682
683
+ if not self .page :
684
+ position = self .next_position
685
+
681
686
cursor = Cursor (offset = offset , reverse = False , position = position )
682
687
return self .encode_cursor (cursor )
683
688
684
689
def get_previous_link (self ):
685
690
if not self .has_previous :
686
691
return None
687
692
688
- if self .cursor and not self .cursor .reverse and self .cursor .offset != 0 :
693
+ if self .page and self . cursor and not self .cursor .reverse and self .cursor .offset != 0 :
689
694
# If we're reversing direction and we have an offset cursor
690
695
# then we cannot use the first position we find as a marker.
691
696
compare = self ._get_position_from_instance (self .page [0 ], self .ordering )
692
697
else :
693
698
compare = self .previous_position
694
699
offset = 0
695
700
701
+ has_item_with_unique_position = False
696
702
for item in self .page :
697
703
position = self ._get_position_from_instance (item , self .ordering )
698
704
if position != compare :
699
705
# The item in this position and the item following it
700
706
# have different positions. We can use this position as
701
707
# our marker.
708
+ has_item_with_unique_position = True
702
709
break
703
710
704
711
# The item in this position has the same position as the item
@@ -707,7 +714,7 @@ def get_previous_link(self):
707
714
compare = position
708
715
offset += 1
709
716
710
- else :
717
+ if self . page and not has_item_with_unique_position :
711
718
# There were no unique positions in the page.
712
719
if not self .has_next :
713
720
# We are on the final page.
@@ -726,6 +733,9 @@ def get_previous_link(self):
726
733
offset = 0
727
734
position = self .next_position
728
735
736
+ if not self .page :
737
<
EDBE
/td>+ position = self .previous_position
738
+
729
739
cursor = Cursor (offset = offset , reverse = True , position = position )
730
740
return self .encode_cursor (cursor )
731
741
0 commit comments