-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
351 lines (294 loc) · 10.3 KB
/
base.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
'''
Base Django settings
'''
import logging
from pathlib import Path as __Path
from django.utils.translation import ugettext_lazy as _
###############################################################################
# Build paths relative to the project root:
PROJECT_PATH = __Path(__file__).parent.parent.parent
print(f'PROJECT_PATH:{PROJECT_PATH}')
if __Path('/.dockerenv').is_file():
# We are inside a docker container
BASE_PATH = __Path('/django_volumes')
assert BASE_PATH.is_dir()
else:
# Build paths relative to the current working directory:
BASE_PATH = __Path().cwd().resolve()
print(f'BASE_PATH:{BASE_PATH}')
# Paths with Django dev. server:
# BASE_PATH...: .../django-for-runners
# PROJECT_PATH: .../django-for-runners/src
#
# Paths in Docker container:
# BASE_PATH...: /for_runners_volumes
# PROJECT_PATH: /usr/local/lib/python3.9/site-packages
###############################################################################
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# Serve static/media files by Django?
# In production Caddy should serve this!
SERVE_FILES = False
# SECURITY WARNING: keep the secret key used in production secret!
__SECRET_FILE = __Path(BASE_PATH, 'secret.txt').resolve()
if not __SECRET_FILE.is_file():
print(f'Generate {__SECRET_FILE}')
from secrets import token_urlsafe as __token_urlsafe
__SECRET_FILE.open('w').write(__token_urlsafe(128))
SECRET_KEY = __SECRET_FILE.open('r').read().strip()
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'bx_py_utils', # https://github.com/boxine/bx_py_utils
'import_export', # https://github.com/django-import-export/django-import-export
'dbbackup', # https://github.com/django-dbbackup/django-dbbackup
'ckeditor', # https://github.com/django-ckeditor/django-ckeditor
'ckeditor_uploader', # https://github.com/django-ckeditor/django-ckeditor
'reversion', # https://github.com/etianen/django-reversion
'reversion_compare', # https://github.com/jedie/django-reversion-compare
'tagulous', # https://github.com/radiac/django-tagulous
'adminsortable2', # https://github.com/jrief/django-admin-sortable2
'axes', # https://github.com/jazzband/django-axes
'django_processinfo', # https://github.com/jedie/django-processinfo/
# https://github.com/jedie/django-tools/tree/master/django_tools/serve_media_app
'django_tools.serve_media_app.apps.UserMediaFilesConfig',
'inventory.apps.InventoryConfig',
]
ROOT_URLCONF = 'inventory_project.urls'
WSGI_APPLICATION = 'inventory_project.wsgi.application'
SITE_ID = 1
AUTHENTICATION_BACKENDS = [
'axes.backends.AxesBackend',
'django.contrib.auth.backends.ModelBackend',
]
MIDDLEWARE = [
'django_processinfo.middlewares.ProcessInfoMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'inventory.middlewares.RequestDictMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'axes.middleware.AxesMiddleware', # AxesMiddleware should be the last middleware
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(__Path(PROJECT_PATH, 'inventory_project', 'templates'))],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'inventory.context_processors.inventory_version_string',
],
},
},
]
# _____________________________________________________________________________
# Internationalization
LANGUAGE_CODE = 'en'
LANGUAGES = [
('de', _('German')),
('en', _('English')),
]
USE_I18N = True
USE_L10N = True
TIME_ZONE = 'Europe/Paris'
USE_TZ = True
# _____________________________________________________________________________
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = str(__Path(BASE_PATH, 'static'))
MEDIA_URL = '/media/'
MEDIA_ROOT = str(__Path(BASE_PATH, 'media'))
# _____________________________________________________________________________
# django-processinfo
from django_processinfo import app_settings as PROCESSINFO # noqa
PROCESSINFO.ADD_INFO = False # Don't add info in HTML page
# _____________________________________________________________________________
# Django-dbbackup
DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
DBBACKUP_STORAGE_OPTIONS = {'location': str(__Path(BASE_PATH, 'backups'))}
# _____________________________________________________________________________
# django-ckeditor
CKEDITOR_BASEPATH = STATIC_URL + 'ckeditor/ckeditor/'
CKEDITOR_FILENAME_GENERATOR = 'inventory.ckeditor_upload.get_filename'
CKEDITOR_DEFAULT_CONFIG = {
'skin': 'moono-lisa',
'removeButtons': 'Language,Cut,Copy,Paste,Undo,Redo,Anchor',
# plugins are here: .../site-packages/ckeditor/static/ckeditor/ckeditor/plugins
# and here: https://github.com/ckeditor/ckeditor4/tree/major/plugins
# See also: .../site-packages/ckeditor/static/ckeditor/ckeditor/build-config.js
'removePlugins': (
# Generated with .../dev-scripts/ckeditor_info.py
'a11yhelp',
'about',
'adobeair',
'ajax',
'autoembed',
# 'autogrow',
'autolink',
# 'basicstyles',
'bbcode',
'bidi',
# 'blockquote',
'clipboard',
'codesnippet',
'codesnippetgeshi',
# 'colorbutton',
# 'colordialog',
'contextmenu',
'copyformatting',
'devtools',
'dialog',
'dialogadvtab',
'div',
'divarea',
'docprops',
# 'elementspath',
'embed',
'embedbase',
'embedsemantic',
'enterkey',
# 'entities',
# 'filebrowser',
# 'filetools',
'find',
'flash',
# 'floatingspace',
# 'font',
# 'format',
'forms',
# 'horizontalrule',
'htmlwriter',
'iframe',
'iframedialog',
# 'image',
# 'image2',
# 'indentblock',
# 'indentlist',
# 'justify',
'language',
# 'lineutils',
# 'link',
# 'list',
# 'liststyle',
'magicline',
'mathjax',
# 'maximize',
# 'menubutton',
'newpage',
'notification',
'notificationaggregator',
'pagebreak',
'pastefromgdocs',
'pastefromword',
'pastetext',
'pastetools',
'placeholder',
'preview',
'print',
# 'removeformat',
# 'resize',
'save',
'scayt',
'selectall',
'sharedspace',
# 'showblocks',
# 'showborders',
'smiley',
'sourcearea',
'sourcedialog',
'specialchar',
'stylescombo',
'stylesheetparser',
'tab',
# 'table',
# 'tableresize',
# 'tableselection',
# 'tabletools',
'templates',
# 'toolbar',
'uicolor',
# 'undo',
# 'uploadimage',
# 'uploadwidget',
'widget',
'wsc',
# 'wysiwygarea',
'xml',
),
'toolbar': 'full',
'height': '25em',
'width': '100%',
'filebrowserWindowWidth': 940,
'filebrowserWindowHeight': 725,
}
CKEDITOR_CONFIGS = {
'ItemModel.description': CKEDITOR_DEFAULT_CONFIG,
'LocationModel.description': CKEDITOR_DEFAULT_CONFIG
}
CKEDITOR_RESTRICT_BY_USER = True
CKEDITOR_RESTRICT_BY_DATE = True
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = 'pillow'
CKEDITOR_THUMBNAIL_SIZE = (300, 300)
CKEDITOR_IMAGE_QUALITY = 40
CKEDITOR_BROWSE_SHOW_DIRS = True
CKEDITOR_ALLOW_NONIMAGE_FILES = True
# _____________________________________________________________________________
# http://radiac.net/projects/django-tagulous/documentation/installation/#settings
TAGULOUS_NAME_MAX_LENGTH = 255
TAGULOUS_SLUG_MAX_LENGTH = 50
TAGULOUS_LABEL_MAX_LENGTH = TAGULOUS_NAME_MAX_LENGTH
TAGULOUS_SLUG_TRUNCATE_UNIQUE = 5
TAGULOUS_SLUG_ALLOW_UNICODE = False
SERIALIZATION_MODULES = {
'xml': 'tagulous.serializers.xml_serializer',
'json': 'tagulous.serializers.json',
'python': 'tagulous.serializers.python',
'yaml': 'tagulous.serializers.pyyaml',
}
# _____________________________________________________________________________
# cut 'pathname' in log output
old_factory = logging.getLogRecordFactory()
def cut_path(pathname, max_length):
if len(pathname) <= max_length:
return pathname
return f'...{pathname[-(max_length - 3):]}'
def record_factory(*args, **kwargs):
record = old_factory(*args, **kwargs)
record.cut_path = cut_path(record.pathname, 30)
return record
logging.setLogRecordFactory(record_factory)
# -----------------------------------------------------------------------------
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'colored': { # https://github.com/borntyping/python-colorlog
'()': 'colorlog.ColoredFormatter',
'format': '%(log_color)s%(asctime)s %(levelname)8s %(cut_path)s:%(lineno)-3s %(message)s',
}
},
'handlers': {'console': {'class': 'colorlog.StreamHandler', 'formatter': 'colored'}},
'loggers': {
'': {'handlers': ['console'], 'level': 'DEBUG', 'propagate': False},
'django': {'handlers': ['console'], 'level': 'INFO', 'propagate': False},
'axes': {'handlers': ['console'], 'level': 'WARNING', 'propagate': False},
'django_tools': {'handlers': ['console'], 'level': 'INFO', 'propagate': False},
'inventory': {'handlers': ['console'], 'level': 'DEBUG', 'propagate': False},
},
}