Add /products API endpoint, enable basic filter support for API views
This commit is contained in:
parent
c1c37aad85
commit
c41a7303df
2 changed files with 91 additions and 1 deletions
|
@ -26,6 +26,8 @@ Tailbone Web API - Master View
|
|||
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import json
|
||||
|
||||
from rattail.config import parse_bool
|
||||
|
||||
from tailbone.api import APIView, api
|
||||
|
@ -72,6 +74,13 @@ class APIMasterView(APIView):
|
|||
return cls.collection_key
|
||||
return '{}s'.format(cls.get_object_key())
|
||||
|
||||
def make_filter_spec(self):
|
||||
if not self.request.GET.has_key('filters'):
|
||||
return []
|
||||
|
||||
filters = json.loads(self.request.GET.getone('filters'))
|
||||
return filters
|
||||
|
||||
def make_sort_spec(self):
|
||||
|
||||
# these params are based on 'vuetable-2'
|
||||
|
@ -171,12 +180,17 @@ class APIMasterView(APIView):
|
|||
return int(page), int(limit)
|
||||
|
||||
def _collection_get(self):
|
||||
from sqlalchemy_filters import apply_sort, apply_pagination
|
||||
from sqlalchemy_filters import apply_filters, apply_sort, apply_pagination
|
||||
|
||||
cls = self.get_model_class()
|
||||
query = self.Session.query(cls)
|
||||
context = {}
|
||||
|
||||
# maybe filter query
|
||||
filter_spec = self.make_filter_spec()
|
||||
if filter_spec:
|
||||
query = apply_filters(query, filter_spec)
|
||||
|
||||
# maybe sort query
|
||||
sort_spec = self.make_sort_spec()
|
||||
if sort_spec:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue