Switch to unified view

a/Allura/allura/lib/patches.py b/Allura/allura/lib/patches.py
1
import webob
1
import webob
2
from tg.decorators import Decoration
2
import tg.decorators
3
from pylons import request
3
4
4
from allura.lib import helpers as h
5
from allura.lib import helpers as h
5
6
7
def apply():
8
    old_lookup_template_engine = tg.decorators.Decoration.lookup_template_engine
6
9
7
def apply():
8
    old_lookup_template_engine = Decoration.lookup_template_engine
9
10
    @h.monkeypatch(Decoration)
10
    @h.monkeypatch(tg.decorators.Decoration)
11
    def lookup_template_engine(self, request):
11
    def lookup_template_engine(self, request):
12
        '''Wrapper to handle totally borked-up HTTP-ACCEPT headers'''
12
        '''Wrapper to handle totally borked-up HTTP-ACCEPT headers'''
13
        try:
13
        try:
14
            return old_lookup_template_engine(self, request)
14
            return old_lookup_template_engine(self, request)
15
        except:
15
        except:
16
            pass
16
            pass
17
        environ = dict(request.environ, HTTP_ACCEPT='*/*')
17
        environ = dict(request.environ, HTTP_ACCEPT='*/*')
18
        request = webob.Request(environ)
18
        request = webob.Request(environ)
19
        return old_lookup_template_engine(self, request)
19
        return old_lookup_template_engine(self, request)
20
21
    @h.monkeypatch(tg, tg.decorators)
22
    def override_template(controller, template):
23
        '''Copy-pasted patch to allow multiple colons in a template spec'''
24
        if hasattr(controller, 'decoration'):
25
            decoration = controller.decoration
26
        else:
27
            return
28
        if hasattr(decoration, 'engines'):
29
            engines = decoration.engines
30
        else:
31
            return
32
33
        for content_type, content_engine in engines.iteritems():
34
            template = template.split(':', 1)
35
            template.extend(content_engine[2:])
36
            try:
37
                override_mapping = request._override_mapping
38
            except AttributeError:
39
                override_mapping = request._override_mapping = {}
40
            override_mapping[controller.im_func] = {content_type: template}