-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.py
executable file
·107 lines (91 loc) · 2.92 KB
/
demo.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
#!/usr/bin/env python3
"""
Inspired by *The World's Smallest Django Project* in *Lightweight
Django*.
Intended as a minimal setup to run the ``demo`` application::
./demo.py
This will create a temporary SQLite database, perform ``test``,
``migrate`` and ``runserver`` commands, launching the ``demo``
application.
"""
import os
import random
import sys
import tempfile
import django
from django.conf import settings
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse
from django.urls import include, path
DEBUG = os.environ.get("DEBUG", "on") == "on"
SECRET_KEY = os.environ.get(
"SECRET_KEY",
"".join(
[
random.SystemRandom().choice(
"abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
)
for i in range(50)
]
),
)
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "127.0.0.1").split(",")
db = tempfile.NamedTemporaryFile(delete=False)
settings.configure(
DEBUG=DEBUG,
SECRET_KEY=SECRET_KEY,
ALLOWED_HOSTS=ALLOWED_HOSTS,
ROOT_URLCONF=__name__,
MIDDLEWARE_CLASSES=(
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
),
INSTALLED_APPS=[
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.staticfiles",
"rest_framework",
"django_filters",
"annotator",
],
STATIC_URL="/static/",
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.template.context_processors.debug",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.static",
"django.template.context_processors.tz",
"django.contrib.messages.context_processors.messages",
],
},
},
],
REST_FRAMEWORK={
"DEFAULT_FILTER_BACKENDS": (
"django_filters.rest_framework.DjangoFilterBackend",
),
},
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": db.name,
}
},
)
django.setup()
urlpatterns = (path(r"", include("annotator.urls")),)
application = get_wsgi_application()
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line([os.path.abspath(__file__), "test", "--verbosity=2"])
execute_from_command_line([os.path.abspath(__file__), "migrate"])
execute_from_command_line([os.path.abspath(__file__), "runserver"])
os.unlink(db.name)