Skip to content

Instantly share code, notes, and snippets.

@osharim
Forked from robhudson/gist:3848832
Created July 30, 2014 23:00
Show Gist options
  • Select an option

  • Save osharim/2e6e77939faca66ca33f to your computer and use it in GitHub Desktop.

Select an option

Save osharim/2e6e77939faca66ca33f to your computer and use it in GitHub Desktop.

Revisions

  1. @robhudson robhudson revised this gist Jan 16, 2013. 1 changed file with 27 additions and 3 deletions.
    30 changes: 27 additions & 3 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,30 @@
    class CORSResource(ModelResource):

    class CORSResource(object):
    """
    Adds CORS headers to resources that subclass this.
    """
    def create_response(self, *args, **kwargs):
    response = super(CORSResource, self).create_response(*args, **kwargs)
    response['Access-Control-Allow-Origin'] = '*'
    return response
    response['Access-Control-Allow-Headers'] = 'Content-Type'
    return response

    def method_check(self, request, allowed=None):
    if allowed is None:
    allowed = []

    request_method = request.method.lower()
    allows = ','.join(map(str.upper, allowed))

    if request_method == 'options':
    response = HttpResponse(allows)
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Headers'] = 'Content-Type'
    response['Allow'] = allows
    raise ImmediateHttpResponse(response=response)

    if not request_method in allowed:
    response = http.HttpMethodNotAllowed(allows)
    response['Allow'] = allows
    raise ImmediateHttpResponse(response=response)

    return request_method
  2. @robhudson robhudson created this gist Oct 7, 2012.
    6 changes: 6 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class CORSResource(ModelResource):

    def create_response(self, *args, **kwargs):
    response = super(CORSResource, self).create_response(*args, **kwargs)
    response['Access-Control-Allow-Origin'] = '*'
    return response