@view_defaults( context=UserResource, renderer='json',)class UserController(ControllerBase): @view_config(request_method='GET', permission='view') def get(self): user = self.context.entity return user
@view_config(request_method='PUT', permission='update') def put(self): # update user here return user
@view_defaults( context=UserResource, renderer='json',)class UserController(ControllerBase):
@view_config(request_method='OPTIONS', permission='options') def options(self): return Response(b'', headerlist=[ ('Access-Control-Allow-Origin', 'http://localhost:8080'), ('Access-Control-Allow-Methods', 'POST'), ('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization'), ])
# other code goes here
@view_config(request_method='PUT', permission='update', cors_allowed_origin='http://foobar.com') def put(self): # update user here return userdef define_cors_views(config):
for view in config introspectable views:
some rules to determine what kind of preflight view to add config.add_view # add the preflight request handler for the view
config.add_before_commit(define_cors_views)Kind of able to do what I want, but still, it's not really ideal, as it applies to the whole app. Sometimes, I want to have fine-grand control over CORS for different endpoints.For example, I may can define something like@view_config(request_method='PUT', permission='update', cors_allowed_origin='http://foobar.com')def put(self):# update user herereturn user
see the "cors_allow_origin" I added to view_config for this put method. There are also other things I can add.