Skip to content

Instantly share code, notes, and snippets.

@mcm
Created April 12, 2015 23:03
Show Gist options
  • Save mcm/18700e5a055aa12e2df3 to your computer and use it in GitHub Desktop.
Save mcm/18700e5a055aa12e2df3 to your computer and use it in GitHub Desktop.
from urllib.parse import parse_qs
import filtration
def validate(req, resp, **kwargs):
...
def access_log(req, resp, **kwargs):
...
def filter_queryset(req):
if "id" not in req.params:
req.context["filter"] = filtration.Expression.parseQsString()
def paginate_queryset(req):
querystring = parse_qs(req.query_string)
queryset = req.context["queryset"]
if "per_page" in querystring:
# Pagination
req.context["pagination"] = {
"page": int(querystring.get("page", 1)),
"per_page": int(querystring.get("per_page", 25)),
}
if "sort_by" in querystring:
req.context["pagination"].update({
"sort_by": querystring["sort_by"],
"order": int(querystring.get("order", -1)),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment