The idea is not bad, it might make sense to add an "extra_headers" option
to the CRUD, but as there are already the _before and _after methods for
such things it might be also a good idea to just document this and let
people solve similar issues by relying on them?

I'm just afraid that otherwise me might end up adding a crud config option
for everything that might be asked for when they can easily be solved by
hooks, BaseController.__call__ and _before/_after

My two cents, I'm open to suggestions.

On Sat, Jan 3, 2015 at 3:32 PM, Clive Jevons <[email protected]>
wrote:

> Hi all,
>
> Just thought I'd share my solution (work-in-progress) to enabling CORS
> <http://www.w3.org/TR/cors/> REST access to a resource setup in
> TurboGears using the EasyCrudRestController.
>
> ... I started with the recipe from the CookBook tutorial on rapid REST
> API prototyping
> <http://turbogears.readthedocs.org/en/latest/cookbook/Crud/restapi.html>.
> The relevant changes are highlighted in bold below:
>
> The TG controller:
>
> # -*- coding: utf-8 -*-
> """MenuEntry REST API controller module"""
>
> # turbogears imports
> from tg import expose, response
>
> # project specific imports
> from menumanagerapi.lib.base import BaseController
> from tgext.crud import EasyCrudRestController
> from menumanagerapi.model import DBSession, metadata, MenuEntry
>
> class MenuEntriesController(EasyCrudRestController):
>     pagination = False
>     model = MenuEntry
>
>
>
>
>
>
>
>
>
> *def _after(self, *args, **kw):
> response.headerlist.append(('Access-Control-Allow-Origin', '*'))
> response.headerlist.append(('Access-Control-Allow-Methods', 'GET, POST,
> PUT, DELETE'))
> response.headerlist.append(('Access-Control-Allow-Headers',
> 'Content-Type'))     @expose('json')     def options(self, menuEntryId,
> *args, **kw):         menuEntry =
> DBSession.query(MenuEntry).get(menuEntryId)         return dict(value =
> menuEntry)*
>
>
> In Angular:
> menuManagerApp.factory('MenuEntries', ['$resource', function($resource) {
>   return $resource('http://localhost:8080/menuentry/:id.json', {'id':
> '@id'}, {
>         query: {
>           method: 'GET',
>           isArray: true,
>           transformResponse: function (data) {
>             var data = angular.fromJson(data);
>             return data.value_list;
>           }
>         },
>         save: {
>           method: 'POST',
>           transformResponse: function(data) {
>             var data = angular.fromJson(data);
>             return data.value;
>           }
>         },
> *        update*
>
>
>
>
>
> *: {          method: 'PUT',          transformResponse: function(data) {
>           var data = angular.fromJson(data);            return data.value;
>         }        }*
>      });
> }]);
>
>
> ... currently only testes running on my local machine from two different
> servers (Angular app running from Apache at localhost and the TurboGears
> app running on localhost:8080), but so far listing, deleting and updating
> are working with no problems.
>
> Maybe this is something that could be rolled into the
> EasyCrudRestController by default?
>
> I'm not sure whether the options method in the controller actually *has*
> to return the data item, but returning an empty dictionary resulted in an
> error. I'll try and work out a cleaner solution to this soon (maybe
> re-using the get_one, or finding out what the minimal set of data is that I
> need to return ...).
>
> Cheers,
> C
>
>  --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/turbogears.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

Reply via email to