Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

CORS middleware #7

Open
wants to merge 50 commits into from

4 participants

@edsu

Hi, On Chris Adam's suggestion I thought I'd contribute some middleware for serving up JSON data with CORS header support, so that people can use JSON data from JavaScript that is loaded from elsewhere. I did supply a test, but I'm not entirely sure how to get the sugar test suite to run :-)

Nagy Viktor and others added some commits
Nagy Viktor Adden in_list filter to be used as {% if value|in_list:list %} ... f0d1f8a
@acdha acdha Switched from deprecated md5 to hashlib 9105bdb
@grengojbo grengojbo Added decorators ajax_request, render_to and template tag media 27503a4
@acdha acdha Added a basic Query String editing tag
{% qs_alter %} can be used to add or delete variables from a query
string:

{% qs_alter request.GET foo=bar baaz=quux delete:corge %}
4ca9dab
@acdha acdha querystring_tags: graceful handling of non-existent parameters c3d5d82
@acdha acdha Added string replacement tags
re_sub and replace filters providing regexp and string literal
replacements
1db5f4b
@acdha acdha Added string replacement tags
re_sub and replace filters providing regexp and string literal
replacements
364f9d1
Chris Adams Merge branch 'master' of github.com:acdha/django-sugar 126bb4e
@acdha acdha Added a simple pagination control for template authors
This allows template authors to paginate any iterable they happen to
have available
4e6bdab
@acdha acdha Add a basic context processor which exposes common settings 274d6a9
@acdha acdha Merge branch 'master' of git://github.com/montylounge/django-sugar
Conflicts:
	sugar/cache/utils.py
	sugar/templatetags/in_list.py
	sugar/templatetags/querystring_tags.py
	sugar/templatetags/sugar_pagination.py
	sugar/templatetags/sugar_template_utils.py
	sugar/templatetags/text_tags.py
	sugar/views/exceptions.py
	sugar/views/helpers.py
bafdf0b
@acdha acdha Merge branch 'master' of github.com:acdha/django-sugar 26ba5d3
@acdha acdha Caching: add decorators, middleware
decorators contains equivalents to django.views.decorators.cache.cache_control and never_cache which accept string names so you can easily wrap urlpatterns rather than modifying the source view.

middlware contains HTTPCacheControlMiddleware which provides a simple way to set the default HTTP Cache-Control header without all of the extra processing that the Django UpdateCacheMiddleware performs or breaking your test suite (http://code.djangoproject.com/ticket/5176). The current implementation focuses on public facing sites and will only adjust successful, anonymous GET requests which do not already have a Cache-Control header.
5d6037a
@acdha acdha Escaped string formatting characters 8aeefa2
@acdha acdha Merge branch 'master' of http://github.com/montylounge/django-sugar ef4bf2f
@acdha acdha Added a handy profiling decorator
This allows you to do things like:

    with query_count("meaningful message"):
        … tons of code …

and get a simple log message:

meaningful message: 23 queries in 0.42 seconds
8330930
@acdha acdha query_count() can now use inspect to identify anonymous callers dd8935e
@acdha acdha profiling: query_count can optionally print the actual queries 398cdf5
@acdha acdha Whitespace cleanup 0eb67b7
@acdha acdha querystring_tags: qs_alter returns escaped HTML
The most common use case is generating links; this avoids the common case of producing invalid HTML due to unescaped query string parameters
6db3a7f
@acdha acdha sugar_template_utils: set_context tag allows defining variables
This allows you to define multiple variables in the current template context
7eaf25f
@acdha acdha querystring_tags: qs_alter can now remove a key=value pair
This allows you to remove a query string element only if it matches a
given value, which is handy if you have multiple instances of the same
key (e.g. /search/?facet=foo&facet=bar).
da60fce
@acdha acdha Fixed docstring typo 3be6b37
@acdha acdha Merge branch 'master' of github.com:acdha/django-sugar b0661c6
@acdha acdha Google Chrome SpeedTracer middleware
Record server-side performance data for Google Chrome's SpeedTracer

Getting started:

1. Download and install Speed Tracer:

    http://code.google.com/webtoolkit/speedtracer/get-started.html

2. Add this middleware to your MIDDLEWARE_CLASSES

3. Reload your page

4. Open SpeedTracer and expand the "Server Trace" in the page's
   detailed report which should look something like:

    http://flic.kr/p/8kwEw3
e6172dd
@acdha acdha Merge branch 'master' of github.com:acdha/django-sugar f668794
@acdha acdha Added deprecation warning for speedtracer middleware
This has moved to a separte django-speedtracer package:
http://pypi.python.org/pypi/django-speedtracer/
85b173d
@acdha acdha Template utils: get_key filter for accessing dictionaries 0740fc4
@acdha acdha Updated AUTHORS file 1eafcc9
@acdha acdha Now this translated error message will actually work… 7434e5b
@acdha acdha Sugar Template Utils: as_json filter
This accepts a Python object and returns the JSON-encoded version
b84a93d
@acdha acdha Added a base class for management commands using logging sanely cdea52a
@acdha acdha sugar_template_utils: allow set_context to set global variables
Normally set_context only operates on the current template context stack level
(i.e. the current block). This patch allows you to use something like:

{% set_context global foo="bar" %}

to affect the entire context stack, which can lead to madness if done unwisely.
c71e723
@acdha acdha setup.py fix c5af1e9
@edsu edsu adding CORS support to allow people to easily serve json so that it c…
…an be used by other parties from JavaScript. See http://www.w3.org/TR/cors/
9fe38af
@edsu edsu corrected suggest mediatype, and added a test that might work? need t…
…o figure out how to run it properly :-)
9d97f5a
@edsu edsu minor docfix c5e816b
@edsu edsu need to extend TestCase eh? f544e2f
@edsu edsu closer to tests working now? fc7112c
@acdha

Maybe this could use a CORS_MIMETYPES setting with these values as the default?

@acdha

Also an area for a setting if you don't want world access?

@montylounge
Owner

thanks edsu. i'll add in the near future.

acdha and others added some commits
@acdha acdha Working tests!
Tests run, including from setup.py test, and there's now a test project to base them on.
b3da2a3
@acdha acdha Pygments tag fixes
Now actually works, plus the code is somewhat cleaner
3fe1616
@edsu edsu got the cors test running, thanks chris! 39b0a91
@edsu edsu some more doc c37cedf
@acdha acdha CORS: confirm non-interference with out of scope MIME types 78d2d4b
@acdha

For flexibility, what do you think about instead defining this as a list of (path prefix, mime type, header) values? That way you'd simply iterate over the list and if request.path.startswith(prefix) you'd use the second & third values - intriguingly, you could even use that for completely custom headers.

The alternative would simply be to keep this really simple and add a decorator variant which could be used in views or urls.py

@edsu

I like that idea a lot ... you want to add it?

acdha and others added some commits
@acdha acdha CORS Middleware: more granular configuration
Now you can control the CORS middleware using settings.CORS_PATH to
specify a list of paths, mime types and headers to set for arbitrary parts of
your site.

This actually allows you to specify any header: perhaps this should be renamed
to reflect its ability to function as an arbitrary HTTP header setter?
388dc72
@acdha acdha Tests: split monolithic test-case f332af1
@acdha acdha Updated & sorted AUTHORS 98472f5
@edsu edsu small docfix c699e11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 18, 2009
  1. @acdha

    Adden in_list filter to be used as {% if value|in_list:list %} ...

    Nagy Viktor authored acdha committed
Commits on Nov 11, 2009
  1. @acdha
Commits on Nov 17, 2009
  1. @grengojbo @acdha
Commits on Dec 8, 2009
  1. @acdha

    Added a basic Query String editing tag

    acdha authored
    {% qs_alter %} can be used to add or delete variables from a query
    string:
    
    {% qs_alter request.GET foo=bar baaz=quux delete:corge %}
Commits on Dec 9, 2009
  1. @acdha
Commits on Dec 30, 2009
  1. @acdha

    Added string replacement tags

    acdha authored
    re_sub and replace filters providing regexp and string literal
    replacements
Commits on Jan 5, 2010
  1. @acdha

    Added string replacement tags

    acdha authored Chris Adams committed
    re_sub and replace filters providing regexp and string literal
    replacements
Commits on Jan 14, 2010
  1. @acdha

    Added a simple pagination control for template authors

    acdha authored
    This allows template authors to paginate any iterable they happen to
    have available
Commits on Jan 30, 2010
  1. @acdha
Commits on Feb 1, 2010
  1. @acdha

    Merge branch 'master' of git://github.com/montylounge/django-sugar

    acdha authored
    Conflicts:
    	sugar/cache/utils.py
    	sugar/templatetags/in_list.py
    	sugar/templatetags/querystring_tags.py
    	sugar/templatetags/sugar_pagination.py
    	sugar/templatetags/sugar_template_utils.py
    	sugar/templatetags/text_tags.py
    	sugar/views/exceptions.py
    	sugar/views/helpers.py
  2. @acdha
  3. @acdha

    Caching: add decorators, middleware

    acdha authored
    decorators contains equivalents to django.views.decorators.cache.cache_control and never_cache which accept string names so you can easily wrap urlpatterns rather than modifying the source view.
    
    middlware contains HTTPCacheControlMiddleware which provides a simple way to set the default HTTP Cache-Control header without all of the extra processing that the Django UpdateCacheMiddleware performs or breaking your test suite (http://code.djangoproject.com/ticket/5176). The current implementation focuses on public facing sites and will only adjust successful, anonymous GET requests which do not already have a Cache-Control header.
Commits on Mar 8, 2010
  1. @acdha
Commits on May 18, 2010
  1. @acdha
  2. @acdha

    Added a handy profiling decorator

    acdha authored
    This allows you to do things like:
    
        with query_count("meaningful message"):
            … tons of code …
    
    and get a simple log message:
    
    meaningful message: 23 queries in 0.42 seconds
  3. @acdha
Commits on Jun 28, 2010
  1. @acdha
  2. @acdha

    Whitespace cleanup

    acdha authored
Commits on Jun 30, 2010
  1. @acdha

    querystring_tags: qs_alter returns escaped HTML

    acdha authored
    The most common use case is generating links; this avoids the common case of producing invalid HTML due to unescaped query string parameters
  2. @acdha

    sugar_template_utils: set_context tag allows defining variables

    acdha authored
    This allows you to define multiple variables in the current template context
Commits on Jul 1, 2010
  1. @acdha

    querystring_tags: qs_alter can now remove a key=value pair

    acdha authored
    This allows you to remove a query string element only if it matches a
    given value, which is handy if you have multiple instances of the same
    key (e.g. /search/?facet=foo&facet=bar).
Commits on Jul 18, 2010
  1. @acdha

    Fixed docstring typo

    acdha authored
  2. @acdha
Commits on Jul 21, 2010
  1. @acdha

    Google Chrome SpeedTracer middleware

    acdha authored
    Record server-side performance data for Google Chrome's SpeedTracer
    
    Getting started:
    
    1. Download and install Speed Tracer:
    
        http://code.google.com/webtoolkit/speedtracer/get-started.html
    
    2. Add this middleware to your MIDDLEWARE_CLASSES
    
    3. Reload your page
    
    4. Open SpeedTracer and expand the "Server Trace" in the page's
       detailed report which should look something like:
    
        http://flic.kr/p/8kwEw3
  2. @acdha
Commits on Sep 10, 2010
  1. @acdha

    Added deprecation warning for speedtracer middleware

    acdha authored
    This has moved to a separte django-speedtracer package:
    http://pypi.python.org/pypi/django-speedtracer/
Commits on Oct 4, 2010
  1. @acdha
Commits on Oct 20, 2010
  1. @acdha

    Updated AUTHORS file

    acdha authored
  2. @acdha
  3. @acdha

    Sugar Template Utils: as_json filter

    acdha authored
    This accepts a Python object and returns the JSON-encoded version
Commits on Dec 8, 2010
  1. @acdha
  2. @acdha

    sugar_template_utils: allow set_context to set global variables

    acdha authored
    Normally set_context only operates on the current template context stack level
    (i.e. the current block). This patch allows you to use something like:
    
    {% set_context global foo="bar" %}
    
    to affect the entire context stack, which can lead to madness if done unwisely.
Commits on Dec 9, 2010
  1. @acdha

    setup.py fix

    acdha authored
Commits on Dec 15, 2010
  1. @edsu

    adding CORS support to allow people to easily serve json so that it c…

    edsu authored
    …an be used by other parties from JavaScript. See http://www.w3.org/TR/cors/
  2. @edsu

    corrected suggest mediatype, and added a test that might work? need t…

    edsu authored
    …o figure out how to run it properly :-)
  3. @edsu

    minor docfix

    edsu authored
  4. @edsu

    need to extend TestCase eh?

    edsu authored
  5. @edsu

    closer to tests working now?

    edsu authored
Commits on Dec 16, 2010
  1. @acdha
  2. @acdha

    Whitespace cleanup

    acdha authored
  3. @acdha

    Working tests!

    acdha authored
    Tests run, including from setup.py test, and there's now a test project to base them on.
  4. @acdha

    Pygments tag fixes

    acdha authored
    Now actually works, plus the code is somewhat cleaner
  5. @edsu
  6. @edsu

    some more doc

    edsu authored
  7. @acdha
  8. @acdha

    CORS Middleware: more granular configuration

    acdha authored
    Now you can control the CORS middleware using settings.CORS_PATH to
    specify a list of paths, mime types and headers to set for arbitrary parts of
    your site.
    
    This actually allows you to specify any header: perhaps this should be renamed
    to reflect its ability to function as an arbitrary HTTP header setter?
  9. @acdha
  10. @acdha

    Updated & sorted AUTHORS

    acdha authored
Commits on Dec 28, 2010
  1. @edsu

    small docfix

    edsu authored
Something went wrong with that request. Please try again.