forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessors.py
More file actions
159 lines (126 loc) · 4.71 KB
/
Copy pathprocessors.py
File metadata and controls
159 lines (126 loc) · 4.71 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from __future__ import absolute_import, print_function, unicode_literals
import logging
import re
import sys
from django.conf import settings as django_settings
from django.http import Http404, HttpResponseRedirect
logger = logging.getLogger(__name__)
def redirect_request_processor(page, request):
"""
Returns a ``HttpResponseRedirect`` instance if the current page says
a redirect should happen.
"""
target = page.get_redirect_to_target(request)
if target:
extra_path = request._feincms_extra_context.get("extra_path", "/")
if extra_path == "/":
return HttpResponseRedirect(target)
logger.debug(
"Page redirect on '%s' not taken because extra path '%s' present",
page.get_absolute_url(),
extra_path,
)
raise Http404()
def extra_context_request_processor(page, request):
"""
Fills ``request._feincms_extra_context`` with a few useful variables.
"""
request._feincms_extra_context.update(
{
# XXX This variable name isn't accurate anymore.
"in_appcontent_subpage": False,
"extra_path": "/",
}
)
url = page.get_absolute_url()
if request.path != url:
request._feincms_extra_context.update(
{
"in_appcontent_subpage": True,
"extra_path": re.sub(
"^" + re.escape(url.rstrip("/")), "", request.path
),
}
)
def etag_request_processor(page, request):
"""
Short-circuits the request-response cycle if the ETag matches.
"""
# XXX is this a performance concern? Does it create a new class
# every time the processor is called or is this optimized to a static
# class??
class DummyResponse(dict):
"""
This is a dummy class with enough behaviour of HttpResponse so we
can use the condition decorator without too much pain.
"""
def has_header(page, what):
return False
def dummy_response_handler(*args, **kwargs):
return DummyResponse()
def etagger(request, page, *args, **kwargs):
etag = page.etag(request)
return etag
def lastmodifier(request, page, *args, **kwargs):
lm = page.last_modified()
return lm
# Unavailable in Django 1.0 -- the current implementation of ETag support
# requires Django 1.1 unfortunately.
from django.views.decorators.http import condition
# Now wrap the condition decorator around our dummy handler:
# the net effect is that we will be getting a DummyResponse from
# the handler if processing is to continue and a non-DummyResponse
# (should be a "304 not modified") if the etag matches.
rsp = condition(etag_func=etagger, last_modified_func=lastmodifier)(
dummy_response_handler
)(request, page)
# If dummy then don't do anything, if a real response, return and
# thus shortcut the request processing.
if not isinstance(rsp, DummyResponse):
return rsp
def etag_response_processor(page, request, response):
"""
Response processor to set an etag header on outgoing responses.
The Page.etag() method must return something valid as etag content
whenever you want an etag header generated.
"""
etag = page.etag(request)
if etag is not None:
response["ETag"] = '"' + etag + '"'
def debug_sql_queries_response_processor(verbose=False, file=sys.stderr):
"""
Attaches a handler which prints the query count (and optionally all
individual queries which have been executed) on the console. Does nothing
if ``DEBUG = False``.
Example::
from feincms.module.page import models, processors
models.Page.register_response_processor(
processors.debug_sql_queries_response_processor(verbose=True),
)
"""
if not django_settings.DEBUG:
return lambda page, request, response: None
def processor(page, request, response):
from django.db import connection
try:
import sqlparse
def print_sql(x):
return sqlparse.format(x, reindent=True, keyword_case="upper")
except Exception:
def print_sql(x):
return x
if verbose:
print("-" * 60, file=file)
time = 0.0
i = 0
for q in connection.queries:
i += 1
if verbose:
print(
"%d : [%s]\n%s\n" % (i, q["time"], print_sql(q["sql"])), file=file
)
time += float(q["time"])
print("-" * 60, file=file)
print("Total: %d queries, %.3f ms" % (i, time), file=file)
print("-" * 60, file=file)
return processor