Skip to content

Commit

Permalink
v0.7.10 - move patch_settings_staticfiles_dirs to AppConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
wtayyeb committed May 8, 2017
1 parent 05d1195 commit 1d3f187
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[wheel]
# create "py2.py3-none-any.whl" package
universal = 1

[aliases]
release = bdist_wheel upload -r pypi-main
32 changes: 31 additions & 1 deletion theming/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
"""
import logging
import os

from django.apps import AppConfig
from django.conf import settings

VERSION = (0, 7, 9)
VERSION = (0, 7, 10)
__version__ = '.'.join((str(i) for i in VERSION))

default_app_config = __name__ + '.App'
logger = logging.getLogger(__name__)


class App(AppConfig):
Expand All @@ -22,6 +26,7 @@ class Defaults:

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):
Expand All @@ -32,3 +37,28 @@ def configure(self):
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)
26 changes: 2 additions & 24 deletions theming/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding:utf-8 -*-
'''
"""
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
'''
"""

import json
import logging
Expand Down Expand Up @@ -67,16 +67,13 @@ def __init__(self, *args, **kwargs):
self._themes = None
self.host = None

self.patch_settings_staticfiles_dirs()

def find_themes(self, force=False):
if self._themes is None or force:
self._themes = {}
root = settings.THEMING_ROOT
for dirname in os.listdir(root):
if not dirname.startswith('~'):
self._themes[dirname] = Theme(dirname)

return self._themes

def get_themes_choice(self):
Expand All @@ -98,25 +95,6 @@ def get_theme(self, theme_slug):
self.find_themes()
return self._themes[theme_slug]

def patch_settings_staticfiles_dirs(self):
staticfiles_dirs = []
for theme_slug in self.find_themes():
real_path = os.path.join(settings.THEMING_ROOT, theme_slug, 'static')
if os.path.isdir(real_path):
key = os.path.join(settings.THEMING_ROOT, theme_slug)
staticfiles_dirs.append((key, real_path))
if os.name == 'nt': # hack for windows
staticfiles_dirs.append((key.replace('/', '\\'), real_path))
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
setattr(settings, 'PRE_STATICFILES_DIRS', PRE_STATICFILES_DIRS)

settings.STATICFILES_DIRS = tuple(PRE_STATICFILES_DIRS) + tuple(staticfiles_dirs)


thememanager = ThemeManager()

Expand Down

0 comments on commit 1d3f187

Please sign in to comment.