-
Notifications
You must be signed in to change notification settings - Fork 1
/
django_aspen.py
67 lines (51 loc) · 1.95 KB
/
django_aspen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import absolute_import, division, print_function, unicode_literals
import inspect
import os
from aspen.request_processor import RequestProcessor
from aspen.simplates.renderers import Renderer, Factory
from django.conf import settings
from django.http import HttpResponse
from django.template import Template
from django.template.context import Context
class NoProjectRoot(Exception):
pass
def install(*a, **kw):
"""Install Aspen into a Django app.
"""
arp = kw.pop('_aspen_request_processor', None)
if arp is None:
# Infer project and www roots.
default_project_root = None
parent = inspect.stack()[1]
if parent:
default_project_root = os.path.dirname(parent[1])
kw['project_root'] = kw.get('project_root', default_project_root)
if kw['project_root'] is None:
raise NoProjectRoot()
kw['www_root'] = kw.get('www_root', os.path.join(kw['project_root'], 'www'))
# Instantiate and configure.
arp = RequestProcessor(*a, **kw)
arp.renderer_factories['django'] = DjangoRendererFactory(arp)
arp.default_renderers_by_media_type['text/html'] = 'django'
return arp
class DjangoRenderer(Renderer):
def compile(self, fspath, raw):
return Template(raw)
def render_content(self, context):
context = Context(context)
return self.compiled.render(context)
class DjangoRendererFactory(Factory):
Renderer = DjangoRenderer
def view(request):
arp = settings.ASPEN_REQUEST_PROCESSOR
state = arp.process( request.path
, request.META.get('QUERY_STRING', '')
, request.META.get('HTTP_ACCEPT', None)
, request=request
)
output = state['output']
body = output.body
if type(body) is not type(b''):
assert output.charset
body = body.encode(output.charset)
return HttpResponse(content=body)