[docs]@dataclassclassPage(Generic[T]):""" Represents one page of items Attributes: data: Items in a page. number: Page number size: Page size total_elements: Number of existing elements. It does not represent number of elements in a page but number of elements in total """data:list[T]number:intsize:inttotal_elements:int
[docs]defget_total_pages(self)->int:""" Get number of total pages based on total elements and page size Returns: Number of existing pages """ifself.size==0:return1returnint(math.ceil(self.total_elements/self.size))
[docs]defhas_next(self)->bool:""" Checks if there is a page after current one Returns: True if next page exists, False otherwise """returnself.number<self.get_total_pages()
[docs]defhas_previous(self)->bool:""" Checks if there is a page before current one. Will be False just for the first page Returns: True if previous page exists, False otherwise """return1<self.number<=self.get_total_pages()
[docs]defnext_page_number(self)->int:""" Gets next page number Returns: Next page number """returnself.number+1
[docs]defprevious_page_number(self)->int:""" Gets previous page number Returns: Previous page number """returnself.number-1
[docs]defis_last(self)->bool:""" Checks if current page is the last one Returns: True if current page is the last one, False otherwise """returnnotself.has_next()
[docs]@dataclassclassPageRequest:"""Page specification Attributes: page: Page number size: Page size """page:int=1size:int=20
[docs]defget_offset(self)->int:""" Gets items offset for current page Returns: Integer representing offset """returnself.size*(self.page-1)