-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
352 lines (304 loc) · 11.1 KB
/
manage.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import os
import sys
from datetime import datetime
from tempfile import gettempdir
import django
from django.conf import settings
from django.http import HttpResponse, Http404
from django.urls import path, re_path, include
from django.template.loader import select_template
from django.template import Template, Context
HAS_JINJA = False
HAS_PATTERNLIBRARY = False
HAS_LIVERELOADISH = False
HAS_SHOUTY_TEMPLATES = False
try:
import jinja2
HAS_JINJA = True
except ModuleNotFoundError:
sys.stderr.write("`jinja2` is necessary for this demo project")
sys.exit(1)
EXTRA_INSTALLED_APPS = ()
EXTRA_MIDDLEWARE = ()
USE_TECHNICALERRORS = os.environ.get("USE_TECHNICALERRORS", "1").lower() in {
"1",
"t",
"true",
"ok",
"yes",
}
if USE_TECHNICALERRORS:
EXTRA_INSTALLED_APPS += ("technicalerrors.TechnicalErrors",)
try:
from livereloadish import watch_file
EXTRA_INSTALLED_APPS += ("livereloadish",)
EXTRA_MIDDLEWARE += ("livereloadish.middleware.LivereloadishMiddleware",)
HAS_LIVERELOADISH = True
except ModuleNotFoundError:
pass
try:
import pattern_library
EXTRA_INSTALLED_APPS += ("pattern_library.apps.PatternLibraryAppConfig",)
HAS_PATTERNLIBRARY = True
except ModuleNotFoundError:
pass
try:
import shouty
# TODO: set this up and make sure the contexts are good.
# EXTRA_INSTALLED_APPS += ("shouty.Shout",)
HAS_SHOUTY_TEMPLATES = True
except ModuleNotFoundError:
pass
HERE = os.path.abspath(os.path.dirname(__file__))
if not settings.configured:
settings.configure(
SECRET_KEY="??????????????????????????????????????????????????????????",
DEBUG=True,
INSTALLED_APPS=(
"django.contrib.staticfiles",
"django.contrib.contenttypes",
"django.contrib.auth",
"django.contrib.messages",
"django.contrib.sessions",
"django.contrib.admin",
"django.contrib.admindocs",
)
+ EXTRA_INSTALLED_APPS,
ALLOWED_HOSTS=("*"),
ROOT_URLCONF=__name__,
MIDDLEWARE=(
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
)
+ EXTRA_MIDDLEWARE,
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": (os.path.join(HERE, "extra_templates"),),
"APP_DIRS": True,
"OPTIONS": {
"context_processors": (
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
),
"builtins": ["pattern_library.loader_tags"] if HAS_PATTERNLIBRARY else [],
},
},
{
"BACKEND": "django.template.backends.jinja2.Jinja2",
"DIRS": [os.path.join(HERE, "extra_templates")],
"APP_DIRS": True,
"OPTIONS": {
"environment": "jinja2.Environment",
# Not recommended, but supported
"context_processors": (
"django.template.context_processors.request",
"django.contrib.messages.context_processors.messages",
),
},
},
],
LOGGING={
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"loggers": {
"livereloadish": {
"handlers": ["console"],
"level": "WARNING",
"propagate": False,
},
"django.template": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": False,
},
},
},
STATIC_URL="/static/",
USE_I18N=True,
USE_TZ=True,
TIME_ZONE="UTC",
X_FRAME_OPTIONS="SAMEORIGIN",
PATTERN_LIBRARY={
"SECTIONS": (
("components", ["patterns/components"]),
("pages", ["patterns/pages"]),
),
"TEMPLATE_SUFFIX": ".html",
"PATTERN_BASE_TEMPLATE_NAME": "patterns/base.html",
"BASE_TEMPLATE_NAMES": ["patterns/base_page.html"],
},
)
django.setup()
from django.contrib import admin
from django.contrib.admindocs import urls as admindocs_urls
def demo404(request, *args, **kwargs) -> HttpResponse:
raise Http404("Custom message")
def demo404_with_long_message(request, *args, **kwargs) -> HttpResponse:
raise Http404("Ruined"*50)
def nested3():
filename = os.path.join(
gettempdir(),
"a_long_path",
"that_probably",
"goes_off_screen",
"and_causes_wrapping_issues",
"elsewhere",
"on_the_page",
"that_need_addressing",
"a_random_file_that_shouldnt_exist.mp3",
)
with open(filename, "rb") as f:
return f.read()
def nested2():
a = 1
b = {1, 2, 3}
c = (1, 2, 3)
a_really_long_variable_name = 4
nested_var = {"a": a, "b": b, "c": c}
a_really_long_variable_value = "Ruined" * 50
try:
return nested3()
except ZeroDivisionError as exc:
raise ValueError("Ruined")
except FileNotFoundError as exc:
raise TypeError(
"RuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuinedRuined"
) from exc
def nested1():
nested_var = datetime.utcnow()
return nested2()
def nested():
nested_var = 1
return nested1()
def demo500(request) -> None:
var = nested()
def demo500templatemissing(request):
return select_template(("demo/a.html", "demo/b.html", "demo/c.html", "demo/d.html"))
def demo500templatesyntax(request):
return Template(
"""Line 1
More lines go here
and then some
and soon we'll maybe get to the error?
Nah let's keep going
Here's some HTML to keep us going
<br>
<br>
<br>
<br>
Go away, line 1!
Example template {{ 'syntax'|bad_filter }} some really long line error that might cause issues on small screens
Ok now we've done the error
How much more do we show afterwards?
Got to be a few lines right?
Probably...
5 or 10 maybe?
Looks like 10, so onwards we go...
At some point the EOF will get cut off.
But not yet, lines aplenty
Keep it up, nearly there
One more line perhaps? Boom there it is.
EOF"""
)
def demo500unicodeencode(request):
"🤞😡🤬👊😔".encode("ascii")
def demo500unicodedecode(request):
b"\xf0\x9f\xa4\x9e\xf0\x9f\x98\xa1\xf0\x9f\xa4\xac\xf0\x9f\x91\x8a\xf0\x9f\x98\x94".decode(
"ascii"
)
from django.template.response import TemplateResponse
def index(request):
class PatchedTemplate(Template):
def render(self, context, request=None):
return super().render(context=context)
return TemplateResponse(request, PatchedTemplate("""
<!DOCTYPE html><html lang="en"><body>
<ul>
<li><a href="{% url 'demo500' %}">Example 500</a>
<li><a href="{% url 'nested_appname:unicodes:demo500unicodedecode' %}">Example 500 #2 (string decoding)</a>
<li><a href="{% url 'nested_appname:unicodes:demo500unicodeencode' %}">Example 500 #3 (string encoding)</a>
<li><a href="{% url 'nested_appname:demo500templatesyntax' %}">Example 500 #3 (template syntax)</a>
<li><a href="{% url 'nested_appname:demo500templatemissing' %}">Example 500 #4 (template missing)</a>
<li><a href="{% url 'demo404' %}">Example 404</a>
<li><a href="{% url 'demo404_with_value_arg' '100' %}">Example 404 #2</a>
<li><a href="{% url 'demo404_with_any_args' 'example' %}">Example 404 #3</a>
<li><a href="{% url 'demo404_ruined' %}">Example 404 #4</a>
<li><a href="/not_a_url">Example 404 #5</a>
<li><a href="/500/unicode/not_a_url">Example 404 #6</a>
</ul>
{{ something.goes.here }}
</body></html>
"""), Context({'something': {'goes': {}}}))
urlpatterns = [
path("admin/docs/", include(admindocs_urls)),
path("admin/", admin.site.urls),
re_path("^404/" + '_'.join(['ruined']*30), demo404_with_long_message, name="demo404_ruined"),
path("404/<int:value>", demo404, name="demo404_with_value_arg"),
re_path("^404/(.+)", demo404, name="demo404_with_any_args"),
path("404", demo404, name="demo404"),
path(
"500/",
include(
(
[
re_path(
"^unicode/",
include(
(
[
path(
"decode",
demo500unicodedecode,
name="demo500unicodedecode",
),
path(
"encode",
demo500unicodeencode,
name="demo500unicodeencode",
),
],
"unicodes",
)
),
),
path(
"inucedo/other_path",
demo500unicodedecode,
name="same_length_as_unicode",
),
path(
"template/syntax",
demo500templatesyntax,
name="demo500templatesyntax",
),
path(
"template/missing",
demo500templatemissing,
name="demo500templatemissing",
),
],
"nested_appname",
)
),
),
path("500", demo500, name="demo500"),
path("", index, name="index"),
]
if HAS_PATTERNLIBRARY:
urlpatterns += [
path("pattern-library/", include("pattern_library.urls")),
]
if __name__ == "__main__":
from django.core import management
management.execute_from_command_line()
else:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()