Changeset 5146

Show
Ignore:
Timestamp:
08/14/08 11:20:34 (5 months ago)
Author:
splee
Message:

* Added ability to set a custom content type directly to pylons.response.headers within a controller method without it being overridden by tg (#1923)
* Prevents addition of charset=utf8 to the content type if the type is not text/* or application/json (#1923)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tg/controllers.py

    r5108 r5146  
    2929 
    3030log = logging.getLogger(__name__) 
     31 
     32# If someone goes @expose(content_type=CUSTOM_CONTENT_TYPE) then we won't override pylons.request.content_type 
     33CUSTOM_CONTENT_TYPE = 'CUSTOM/LEAVE' 
    3134 
    3235def _configured_engines(): 
     
    207210            controller.decoration.lookup_template_engine(pylons.request) 
    208211 
    209         # Always set content type 
    210         pylons.response.headers['Content-Type'] = content_type 
     212        if content_type != CUSTOM_CONTENT_TYPE: 
     213            pylons.response.headers['Content-Type'] = content_type 
    211214        # Save these objeccts as locals from the SOP to avoid expensive lookups 
    212215        req = pylons.request._current_obj() 
  • trunk/tg/decorators.py

    r5053 r5146  
    7171        engine, template, exclude_names = self.engines[content_type] 
    7272         
    73         if 'charset' not in content_type:  
     73        if 'charset' not in content_type and ( 
     74           content_type.startswith('text') or content_type  == 'application/json'): 
    7475            content_type = '%s; charset=utf-8' % content_type 
    7576         
  • trunk/tg/tests/test_tg_controller_dispatch.py

    r5126 r5146  
    22 
    33import tg, pylons 
    4 from tg.controllers import TGController 
     4from tg.controllers import TGController, CUSTOM_CONTENT_TYPE 
    55from tg.decorators import expose, validate 
    66from routes import Mapper 
     
    123123        return dict(got_json=True) 
    124124         
     125    @expose(content_type=CUSTOM_CONTENT_TYPE) 
     126    def custom_content_type(self): 
     127        pylons.response.headers['content-type'] = 'image/png' 
     128        return 'PNG' 
     129 
     130 
    125131class TestTGController(TestWSGIController): 
    126132    def __init__(self, *args, **kargs): 
     
    214220        assert '{"got_json' in resp.body 
    215221 
     222    def test_custom_content_type(self): 
     223        resp = self.app.get('/custom_content_type') 
     224        self.assertEqual('image/png', dict(resp.headers)['content-type']) 
     225        assert resp.body == 'PNG' 
     226