-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
64 lines (51 loc) · 2.22 KB
/
__init__.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
# -*- coding:utf-8 -*-
"""
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
"""
import logging
import os
from django.apps import AppConfig
from django.conf import settings
VERSION = (0, 8, 2)
__version__ = '.'.join((str(i) for i in VERSION))
default_app_config = __name__ + '.App'
logger = logging.getLogger(__name__)
class App(AppConfig):
name = 'theming'
class Defaults:
THEMING_DEFAULT_THEME = 'default'
THEMING_ROOT = 'themes' # will append to BASE_DIR
THEMING_URL = 'themes' # will append to STATIC_URL
def ready(self):
self.configure()
self.patch_settings_staticfiles_dirs()
def configure(self):
for name in (name for name in dir(self) if name.upper() == name):
setattr(settings, name, getattr(self, name))
for name in (name for name in dir(self.Defaults) if name.upper() == name):
try:
getattr(settings, name)
except AttributeError:
setattr(settings, name, getattr(self.Defaults, name))
def patch_settings_staticfiles_dirs(self):
staticfiles_dirs = []
for theme_slug in os.listdir(settings.THEMING_ROOT):
if theme_slug.startswith('~'):
continue
real_path = os.path.join(settings.THEMING_ROOT, theme_slug, 'static').replace('\\', '/')
if os.path.isdir(real_path):
# here we need its path under static so using THEMING_URL
key = os.path.join(settings.THEMING_URL, theme_slug).replace('\\', '/')
row = (key, real_path)
if os.name == 'nt': # fix for windows
row = [r.replace('/', '\\') for r in row]
staticfiles_dirs.append(row)
else:
logger.debug('theme `%s` not found.' % theme_slug)
PRE_STATICFILES_DIRS = getattr(settings, 'PRE_STATICFILES_DIRS', None)
if PRE_STATICFILES_DIRS is None:
PRE_STATICFILES_DIRS = settings.STATICFILES_DIRS
# so in concurrent calls the STATICFILES_DIRS not raise
setattr(settings, 'PRE_STATICFILES_DIRS', PRE_STATICFILES_DIRS)
settings.STATICFILES_DIRS = tuple(PRE_STATICFILES_DIRS) + tuple(staticfiles_dirs)