Giter Site home page Giter Site logo

falcon-sorting-hook's Introduction

falcon-sorting-hook

image

image

image

image

Updates

A small falcon hook to parse sorting elements from the request.

Usage

The easiest way to use this hook is the following:

class Resource:
    sorting_fields = ("foo", "bar")  # List of fields allowed for sorting

    @falcon.before(SortingHook())
    def on_get(self, req, resp, user):
        # Here req['context']['sort'] is set

The Hook will look in the query parameters for parameters looking like sort=value.

The default sorting order is ascending. To sort in a descending order a minus (-) sign needs to be specified before the value. i.e. sort=-value

It is possible to specify multiple sorting values by separating them with a comma. i.e. sort=-value1,value2

It will create a list in the request context accessible at req.context['sort']. This list consists of tuples where the first element is the name of the field to sort on and the second the order to follow (either ASC or DESC)

i.e. [('foo', 'ASC'), ('bar', 'DESC')].

Configuration options

Allowing fields for sorting

For security reasons, the fields allowed for sorting must be specified in the sorting_fields attribute of the resource.

All the fields not defined in it will be discarded by the hook.

Default sorting order

It is possible to specify a default sorting order by setting the default_sorting attribute in the resource.

This attribute must be a string or a tuple that respects the convention we have for the request parameter.

The attributes specified as the default order must also be part of sorting_fields.

Examples:

default_sorting = '-foo'
# or
default_sorting = ('foo', '-bar')

Hook configuration

One parameter can be passed to the hook:

  • sort_query_key : The name of the key used in the query to sort data. Default: sort.

Example:

@falcon.before(PaginationFromRequestHook(
    sort_query_key='order',
))
def on_get(self, req, resp, user):
    # Get request

falcon-sorting-hook's People

Contributors

darkheir avatar pyup-bot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.