-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcreate.py
118 lines (90 loc) · 2.4 KB
/
create.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
import os
import shutil
settings = """
# Set the root directory of the repo as the search path
import os
import sys
sys.path.insert(0, os.path.dirname(BASE_DIR))
# Install translations app, and sample app to test it
INSTALLED_APPS += [
'translations.apps.TranslationsConfig',
'sample.apps.SampleConfig',
'tests.apps.TestsConfig',
'rest_framework',
]
MIDDLEWARE += [
'django.middleware.locale.LocaleMiddleware',
]
LANGUAGES = (
('en', 'English'),
('en-gb', 'English (Great Britain)'),
('de', 'German'),
('tr', 'Turkish'),
)
# Read DB configuration from environment variables
DEFAULT_ENGINE = 'sqlite3'
DEFAULT_NAME = os.path.join(BASE_DIR, 'db.sqlite3')
ENGINE = os.environ.get('EXAMPLE_ENGINE', DEFAULT_ENGINE)
NAME = os.environ.get('EXAMPLE_NAME', DEFAULT_NAME)
HOST = os.environ.get('EXAMPLE_HOST', None)
PORT = os.environ.get('EXAMPLE_PORT', None)
USER = os.environ.get('EXAMPLE_USER', None)
PASSWORD = os.environ.get('EXAMPLE_PASSWORD', None)
def get_database_conf():
conf = {}
conf['ENGINE'] = 'django.db.backends.{}'.format(ENGINE)
conf['NAME'] = NAME
if HOST:
conf['HOST'] = HOST
if PORT:
conf['PORT'] = PORT
if USER:
conf['USER'] = USER
if PASSWORD:
conf['PASSWORD'] = PASSWORD
return conf
DATABASES = {
'default': get_database_conf()
}
# Read logging configuration from environment variables
LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': LOG_LEVEL,
},
'django.db': {
'handlers': ['console'],
'level': LOG_LEVEL,
},
},
}
"""
urls = """
from django.urls import include
urlpatterns += [
path('sample/', include('sample.urls'))
]
"""
if __name__ == '__main__':
# remove the old project
try:
shutil.rmtree('project')
except FileNotFoundError:
pass
# create a new project
os.system('django-admin startproject project')
# configure settings
with open(os.path.join('project', 'project', 'settings.py'), 'a') as fh:
fh.write(settings)
# configure urls
with open(os.path.join('project', 'project', 'urls.py'), 'a') as fh:
fh.write(urls)