jfinkels/flask-restless — pull request #238 by dedalusj (2013-08-19 11:08:38+00:00)
Title: Pass include and exclude lists to preprocessor and extract field_params from the request to personalise response

I was writing an API and I wanted a little more control over the fields included in the response.

Currently Flask-Restless let you specify a global list of fields to include or exclude from the response. I wanted however the ability to differentiate the fields included in the get single from the get many for example.
At the moment the best way to achieve this is to remove the unwanted fields in the postprocessor. However this means that the app has to make queries and jsonify unwanted fields or relation which is not ideal for performance. Moreover there is no way to add particular fields in the postprocessor.

My solution was:
1) At the moment the API is created, global versions of the include and exclude lists are created as well.
2) For every request whose output can be personalised, local copies of the include and exclude lists are created and passed to the preprocessors
3) Here the user has the ability to remove or add fields to the lists in-place
4) The response is generated from this user modified copies of the include and exclude lists

This in principle allow the user to personalise the response of the API for every method that accepts a preprocessor. It also has the advantages of keeping the current way to specify include and exclude fields unchanged which is good for coarse-grained tuning of the API.

I have also added parsing of an extra "fields" data in the get requests. This opens the possibility for the user of the API to specify which fields to include in the response from the HTTP request.
For example this request:
```curl -G -H "Content-type: application/json" -d "fields=[\"name\"]" http://127.0.0.1:5000/api/computer```
returns only the name field for every entry.

I'm relatively new to python so I'm not sure I implemented this in the best way. Feel free to pull it apart.