Skip to content

Commit 5e9164b

Browse files
committed
add new settings example
1 parent db3f48e commit 5e9164b

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

content/pages/examples/django/django-conf-settings-examples.markdown

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ is a [Django](/django.html)
2222
to simplify handling time data in your applications using
2323
users' geolocation data.
2424

25+
This example shows how to import the configuration from your Django
26+
`settings.py` file into a different part of your web application.
27+
2528
[**django-easy-timezones/easy_timezones/middleware.py**](https://github.com/Miserlou/django-easy-timezones/blob/master/easy_timezones/middleware.py)
2629

2730
```python
@@ -132,3 +135,268 @@ class EasyTimezoneMiddleware(middleware_base_class):
132135
timezone.deactivate()
133136
```
134137

138+
139+
## Example 2 from django-filer
140+
[django-filer](https://github.com/divio/django-filer)
141+
([project documentation](https://django-filer.readthedocs.io/en/latest/))
142+
is a file management library for uploading and organizing files and images
143+
in Django's admin interface. The project's code is available under the
144+
[BSD 3-Clause "New" or "Revised" open source license](https://github.com/divio/django-filer/blob/develop/LICENSE.txt).
145+
146+
This example code file shows how to set many parts of the configuration
147+
within a `setttings.py` file.
148+
149+
[**django-filer / filer / settings.py**](https://github.com/divio/django-filer/blob/develop/filer/settings.py)
150+
151+
```python
152+
# -*- coding: utf-8 -*-
153+
from __future__ import absolute_import
154+
155+
import logging
156+
import os
157+
158+
~~from django.conf import settings
159+
from django.core.exceptions import ImproperlyConfigured
160+
from django.core.files.storage import get_storage_class
161+
162+
from .utils.loader import load_object
163+
from .utils.recursive_dictionary import RecursiveDictionaryWithExcludes
164+
165+
166+
logger = logging.getLogger(__name__)
167+
168+
# FILER_IMAGE_MODEL setting is used to swap Image model.
169+
# If such global setting does not exist, it will be created at this
170+
# point (with default model name).
171+
# This is needed especially when using this setting in migrations.
172+
~~if not hasattr(settings, 'FILER_IMAGE_MODEL'):
173+
~~ setattr(settings, 'FILER_IMAGE_MODEL', 'filer.Image')
174+
~~FILER_IMAGE_MODEL = settings.FILER_IMAGE_MODEL
175+
176+
~~FILER_DEBUG = getattr(settings, 'FILER_DEBUG', False) # When True makes
177+
~~FILER_SUBJECT_LOCATION_IMAGE_DEBUG = getattr(settings,
178+
~~ 'FILER_SUBJECT_LOCATION_IMAGE_DEBUG',
179+
~~ False)
180+
~~FILER_WHITESPACE_COLOR = getattr(settings, 'FILER_WHITESPACE_COLOR',
181+
~~ '#FFFFFF')
182+
183+
~~FILER_0_8_COMPATIBILITY_MODE = getattr(settings,
184+
~~ 'FILER_0_8_COMPATIBILITY_MODE',
185+
~~ False)
186+
187+
~~FILER_ENABLE_LOGGING = getattr(settings, 'FILER_ENABLE_LOGGING',
188+
~~ False)
189+
~~if FILER_ENABLE_LOGGING:
190+
~~ FILER_ENABLE_LOGGING = (
191+
~~ FILER_ENABLE_LOGGING and (getattr(settings, 'LOGGING')
192+
~~ and ('' in settings.LOGGING['loggers']
193+
~~ or 'filer' in settings.LOGGING['loggers'])))
194+
195+
~~FILER_ENABLE_PERMISSIONS = getattr(settings, 'FILER_ENABLE_PERMISSIONS',
196+
~~ False)
197+
~~FILER_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS = getattr(settings,
198+
~~ 'FILER_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS', False)
199+
~~FILER_IS_PUBLIC_DEFAULT = getattr(settings, 'FILER_IS_PUBLIC_DEFAULT', True)
200+
201+
~~FILER_PAGINATE_BY = getattr(settings, 'FILER_PAGINATE_BY', 20)
202+
203+
~~_ICON_SIZES = getattr(settings, 'FILER_ADMIN_ICON_SIZES',
204+
~~ ('16', '32', '48', '64'))
205+
if not _ICON_SIZES:
206+
raise ImproperlyConfigured('Please, configure FILER_ADMIN_ICON_SIZES')
207+
# Reliably sort by integer value, but keep icon size as string.
208+
# (There is some code in the wild that depends on this being strings.)
209+
FILER_ADMIN_ICON_SIZES = \
210+
[str(i) for i in sorted([int(s) for s in _ICON_SIZES])]
211+
212+
# Filer admin templates have specific icon sizes hardcoded: 32 and 48.
213+
_ESSENTIAL_ICON_SIZES = ('32', '48')
214+
if not all(x in FILER_ADMIN_ICON_SIZES for x in _ESSENTIAL_ICON_SIZES):
215+
logger.warn(
216+
"FILER_ADMIN_ICON_SIZES has not all of the essential icon sizes "
217+
"listed: {}. Some icons might be missing in admin templates.".format(
218+
_ESSENTIAL_ICON_SIZES))
219+
220+
# This is an ordered iterable that describes a list of
221+
# classes that I should check for when adding files
222+
~~FILER_FILE_MODELS = getattr(
223+
~~ settings, 'FILER_FILE_MODELS',
224+
~~ (FILER_IMAGE_MODEL, 'filer.File'))
225+
226+
~~DEFAULT_FILE_STORAGE = getattr(settings, 'DEFAULT_FILE_STORAGE',
227+
~~ 'django.core.files.storage.FileSystemStorage')
228+
229+
MINIMAL_FILER_STORAGES = {
230+
'public': {
231+
'main': {
232+
'ENGINE': None,
233+
'OPTIONS': {},
234+
},
235+
'thumbnails': {
236+
'ENGINE': None,
237+
'OPTIONS': {},
238+
}
239+
},
240+
'private': {
241+
'main': {
242+
'ENGINE': None,
243+
'OPTIONS': {},
244+
},
245+
'thumbnails': {
246+
'ENGINE': None,
247+
'OPTIONS': {},
248+
},
249+
},
250+
}
251+
252+
253+
DEFAULT_FILER_STORAGES = {
254+
'public': {
255+
'main': {
256+
'ENGINE': DEFAULT_FILE_STORAGE,
257+
'OPTIONS': {},
258+
'UPLOAD_TO': 'filer.utils.generate_filename.randomized',
259+
'UPLOAD_TO_PREFIX': 'filer_public',
260+
},
261+
'thumbnails': {
262+
'ENGINE': DEFAULT_FILE_STORAGE,
263+
'OPTIONS': {},
264+
'THUMBNAIL_OPTIONS': {
265+
'base_dir': 'filer_public_thumbnails',
266+
},
267+
},
268+
},
269+
'private': {
270+
'main': {
271+
'ENGINE': 'filer.storage.PrivateFileSystemStorage',
272+
'OPTIONS': {
273+
~~ 'location': os.path.abspath(os.path.join(settings.MEDIA_ROOT,
274+
~~ '../smedia/filer_private')),
275+
'base_url': '/smedia/filer_private/',
276+
},
277+
'UPLOAD_TO': 'filer.utils.generate_filename.randomized',
278+
'UPLOAD_TO_PREFIX': '',
279+
},
280+
'thumbnails': {
281+
'ENGINE': 'filer.storage.PrivateFileSystemStorage',
282+
'OPTIONS': {
283+
~~ 'location': os.path.abspath(os.path.join(settings.MEDIA_ROOT,
284+
~~ '../smedia/filer_private_thumbnails')),
285+
'base_url': '/smedia/filer_private_thumbnails/',
286+
},
287+
'THUMBNAIL_OPTIONS': {},
288+
},
289+
},
290+
}
291+
292+
MINIMAL_FILER_SERVERS = {
293+
'private': {
294+
'main': {
295+
'ENGINE': None,
296+
'OPTIONS': {},
297+
},
298+
'thumbnails': {
299+
'ENGINE': None,
300+
'OPTIONS': {},
301+
},
302+
},
303+
}
304+
305+
DEFAULT_FILER_SERVERS = {
306+
'private': {
307+
'main': {
308+
'ENGINE': 'filer.server.backends.default.DefaultServer',
309+
'OPTIONS': {},
310+
},
311+
'thumbnails': {
312+
'ENGINE': 'filer.server.backends.default.DefaultServer',
313+
'OPTIONS': {},
314+
},
315+
},
316+
}
317+
318+
FILER_STORAGES = RecursiveDictionaryWithExcludes(MINIMAL_FILER_STORAGES,
319+
rec_excluded_keys=('OPTIONS', 'THUMBNAIL_OPTIONS'))
320+
if FILER_0_8_COMPATIBILITY_MODE:
321+
user_filer_storages = {
322+
'public': {
323+
'main': {
324+
'ENGINE': DEFAULT_FILE_STORAGE,
325+
'UPLOAD_TO': 'filer.utils.generate_filename.randomized',
326+
~~ 'UPLOAD_TO_PREFIX': getattr(settings,
327+
~~ 'FILER_PUBLICMEDIA_PREFIX',
328+
~~ 'filer_public'),
329+
},
330+
'thumbnails': {
331+
'ENGINE': DEFAULT_FILE_STORAGE,
332+
'OPTIONS': {},
333+
'THUMBNAIL_OPTIONS': {
334+
'base_dir': 'filer_public_thumbnails',
335+
},
336+
},
337+
},
338+
'private': {
339+
'main': {
340+
'ENGINE': DEFAULT_FILE_STORAGE,
341+
'UPLOAD_TO': 'filer.utils.generate_filename.randomized',
342+
~~ 'UPLOAD_TO_PREFIX': getattr(settings,
343+
~~ 'FILER_PRIVATEMEDIA_PREFIX',
344+
~~ 'filer_private'),
345+
},
346+
'thumbnails': {
347+
'ENGINE': DEFAULT_FILE_STORAGE,
348+
'OPTIONS': {},
349+
'THUMBNAIL_OPTIONS': {
350+
'base_dir': 'filer_private_thumbnails',
351+
},
352+
},
353+
},
354+
}
355+
else:
356+
~~ user_filer_storages = getattr(settings, 'FILER_STORAGES', {})
357+
358+
FILER_STORAGES.rec_update(user_filer_storages)
359+
360+
361+
def update_storage_settings(user_settings, defaults, s, t):
362+
if not user_settings[s][t]['ENGINE']:
363+
user_settings[s][t]['ENGINE'] = defaults[s][t]['ENGINE']
364+
user_settings[s][t]['OPTIONS'] = defaults[s][t]['OPTIONS']
365+
if t == 'main':
366+
if 'UPLOAD_TO' not in user_settings[s][t]:
367+
user_settings[s][t]['UPLOAD_TO'] = defaults[s][t]['UPLOAD_TO']
368+
if 'UPLOAD_TO_PREFIX' not in user_settings[s][t]:
369+
user_settings[s][t]['UPLOAD_TO_PREFIX'] = \
370+
defaults[s][t]['UPLOAD_TO_PREFIX']
371+
if t == 'thumbnails':
372+
if 'THUMBNAIL_OPTIONS' not in user_settings[s][t]:
373+
user_settings[s][t]['THUMBNAIL_OPTIONS'] = \
374+
defaults[s][t]['THUMBNAIL_OPTIONS']
375+
return user_settings
376+
377+
378+
update_storage_settings(FILER_STORAGES, DEFAULT_FILER_STORAGES,
379+
'public', 'main')
380+
update_storage_settings(FILER_STORAGES, DEFAULT_FILER_STORAGES,
381+
'public', 'thumbnails')
382+
update_storage_settings(FILER_STORAGES, DEFAULT_FILER_STORAGES,
383+
'private', 'main')
384+
update_storage_settings(FILER_STORAGES, DEFAULT_FILER_STORAGES,
385+
'private', 'thumbnails')
386+
387+
FILER_SERVERS = RecursiveDictionaryWithExcludes(MINIMAL_FILER_SERVERS,
388+
rec_excluded_keys=('OPTIONS',))
389+
~~FILER_SERVERS.rec_update(getattr(settings, 'FILER_SERVERS', {}))
390+
391+
392+
~~def update_server_settings(settings, defaults, s, t):
393+
~~ if not settings[s][t]['ENGINE']:
394+
~~ settings[s][t]['ENGINE'] = defaults[s][t]['ENGINE']
395+
~~ settings[s][t]['OPTIONS'] = defaults[s][t]['OPTIONS']
396+
~~ return settings
397+
398+
399+
# file continues here with a few more settings examples, but nothing
400+
# too different from what was shown above
401+
```
402+

0 commit comments

Comments
 (0)