0

I'm trying to implement server side filtering using KendoUI grid and Flask-Restless web service.

My Datasource object looks like this:

        var myDataSource = new kendo.data.DataSource({
        transport: {
            read: {
              type: 'GET',
              url: "http://localhost:5000/api/geo",
              contentType: "application/json",
              dataType: "json",
            },
            parameterMap: function(data, type) {
              if (type == "read") {
                // console.log(data.filter);
                return {
                  page: data.page,
                  q: data.filter
                }
              }
            },
        },
        schema: {
          data: 'objects',
          total: "num_results", 
        },
        serverPaging: true,
        serverFiltering: true,
        pageSize: 100,
    }); 

On the server side, I want to intercept the filtering parameters, modify them appropriately to fit the Flask-Restless query object format and pass them to the REST endpoint. However, if using GET request the flask.request.args object looks like a flattened-out dictionary which is tedious to parse. The following code within the preprocessor:

 for key, value in flask.request.args.iteritems():
    print '%s : %s' % (key, value)

produces:

q[filters][0][value] : 106
q[filters][1][field] : county_id
q[filters][1][operator] : eq
q[filters][1][value] : 107
q[filters][0][operator] : eq
q[filters][0][field] : county_id
q[logic] : and

I guess I have two questions: 1. Is it possible to use POST request for reading data from Flask-Restless endpoint? 2. if answer to #1 is yes: how to pass the data.filter object from the Kendo DataSource to the Flask-Restless web service?

I do have access to the flask.reguest.args (in case of GET request) inside the preprocessor however, the args structure represents nested objects as string keys like: 'q[filters][0][value]' which is difficult to parse. I hope there is a better approach.

Thanks!

Matej
  • 932
  • 4
  • 14
  • 22

1 Answers1

1

EDIT 2 -

This will partially format your data from an $.ajax type request for use with a GET request in Flask-Restless. You can use this for a GET (as you use in your example above) and you don't need to use a POST. Hopefully this helps, if not then you will need to provide some more data or attempts to help steer in the right direction. Also, the manipulation still needs a few steps (i.e. logic) for transformation. I thought that would be good exercise to complete...

If you print flask.request.args you should get something like this:

import pprint
pprint(q)

# returns this
{'filters': [{'field': 'county_id', 'operator': 'eq', 'value': 106},
         {'field': 'county_id', 'operator': 'eq', 'value': 107}],'logic':'and'}

Then you just need to manipulate it to the correct format for Restless Search Queries (as per spec) using some functionality of built-ins.

# Python 3

new_filter = {}
for key, value in q.items():
    if key == 'filters':
        new_filter[key] = []
        for item in q[key]:
            rename_data = {
                     'name': item['field'] ,
                     'op':   item['operator'],
                     'val':  item['value']
                   }
            new_filter[key].append(rename_data)
     else:
         new_filter[key] = value

returns ...

{'filters': [{'name': 'county_id', 'op': 'eq', 'val': 106},
         {'name': 'county_id', 'op': 'eq', 'val': 107}],
'logic': 'and'}

which can then be passed as a query...

Community
  • 1
  • 1
siegerts
  • 461
  • 4
  • 11
  • I don't see how this could be applicable in my scenario. My request arguments come directly from the widget and the server-side framework is Flask-Restless. I want to use post request to pass query parameters for reading, not updating the data. – Matej Sep 22 '15 at 12:54
  • @Matej Apologies, I updated for *Restless* and not *Restful*. Looks like the Search Format syntax may be the most flexible approach using GET via Flask-Restless. – siegerts Sep 22 '15 at 14:05
  • Thanks for your response. I edited the question in order to be clearer. The issue is not with manually building the filters and sending a request but bringing filters over from kendo.DataSource and manipulating them inside the Flask-Restless preprocessor.. Can you perhaps answer either of the two questions above? – Matej Sep 22 '15 at 16:50
  • @Matej I updated the answer *Edit 2* based on your response. – siegerts Sep 22 '15 at 21:04
  • Edit - 2 is an attempt in the right direction. However, flask.request.args is an ImmutableDict: ImmutableMultiDict([('q[filters][2][filters][0][operator]', u'eq'), ('q[filters][2][filters][0][value]', u'106')... How did you get the output you are showing above in the first code block of Edit-2? Please erase the rest of the response and leave only Edit-2 part for only that part is relevant to the question. Thanks. – Matej Sep 23 '15 at 00:23
  • @Matej , I removed the other content. Also, I would try `print(flask.request.args.items()` ... I just recreated the q dict from the info that you provided `q[filters][0][value] : 106`, etc. Once you get the dict or the items then you can recreate the filters. – siegerts Sep 23 '15 at 02:23