Flask-HAL icon indicating copy to clipboard operation
Flask-HAL copied to clipboard

Allow predefined method to add pagination links

Open radeklos opened this issue 10 years ago • 1 comments

Add method to Document to add paginated link - next and prev. Something like those:

doc = Document() 
doc.next('/page/1").prev('/page/0")
doc = Document(pagination=PagePagination(...)) 
doc = Document(pagination=OffsetPagination(...)) 

radeklos avatar Aug 11 '15 15:08 radeklos

We should think about how we achieve this. Pagination and Errors as defined in our own API spec go beyond the standard HAL specification so I'm not 100% sure they should live in Flask-HAL.

It maybe better to have a private library that provides decorators or utilities to add that stuff to the document without needing to extend the core functionality of Flask-HAL and it is flexible enough to do that since we just return documents, so we could have a pagination decorator that looks something like this:

from functools import wraps

def paginated():
    @wraps(function)
    def wrapper(self, *args, **kwargs):
        document, code, headers = function(self, *args, **kwargs)
        document.data['_pagination'] = {....}
        document.links.append(Link('next', ....)

        return document, code, headers

    return wrapper

@paginated()
def view(...):
    return Document()

krak3n avatar Aug 12 '15 10:08 krak3n