diff --git a/nexus/conf.py b/nexus/conf.py index f7a7895..63be2bc 100644 --- a/nexus/conf.py +++ b/nexus/conf.py @@ -4,3 +4,5 @@ if getattr(settings, 'NEXUS_USE_DJANGO_MEDIA_URL', False): MEDIA_PREFIX = getattr(settings, 'MEDIA_URL', MEDIA_PREFIX) + +USE_STATICFILES = getattr(settings, 'NEXUS_USE_STATICFILES', False) diff --git a/nexus/models.py b/nexus/models.py index d6ad4c2..b2ef4b2 100644 --- a/nexus/models.py +++ b/nexus/models.py @@ -9,3 +9,6 @@ raise ImproperlyConfigured("Put '%s' in your " "INSTALLED_APPS setting in order to use the nexus application." % r) +# XXX but we needed registered nexus modules to update +# STATICFILES_DIRS setting for collectstatic command +__import__(settings.ROOT_URLCONF) diff --git a/nexus/sites.py b/nexus/sites.py index b673662..5c93a0f 100644 --- a/nexus/sites.py +++ b/nexus/sites.py @@ -1,5 +1,6 @@ # Core site concept heavily inspired by django.contrib.sites +from django.conf import settings from django.core.context_processors import csrf from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotModified, Http404 @@ -12,6 +13,10 @@ from django.views.decorators.cache import never_cache from django.views.decorators.csrf import csrf_protect from django.views.static import was_modified_since +try: + from django.contrib.staticfiles.views import serve +except ImportError: # django<1.3 + serve = None from nexus import conf @@ -73,6 +78,16 @@ def register(self, module, namespace=None, category=None): namespace = module.get_namespace() if namespace: module.app_name = module.name = namespace + if conf.USE_STATICFILES and os.path.exists(module.media_root): + static_prefix = namespace if namespace != 'admin' else 'nexus' + nexus_static = ( + (static_prefix, module.media_root), + ) + if isinstance(settings.STATICFILES_DIRS, tuple): + settings.STATICFILES_DIRS += nexus_static + else: + settings.STATICFILES_DIRS.extends(nexus_static) + self._registry[namespace] = (module, category) return module @@ -205,6 +220,9 @@ def media(self, request, module, path): """ Serve static files below a given point in the directory structure. """ + if conf.USE_STATICFILES and serve: + return serve(path='%s/%s' % (module, path)) + if module == 'nexus': document_root = os.path.join(NEXUS_ROOT, 'media') else: diff --git a/nexus/templatetags/nexus_helpers.py b/nexus/templatetags/nexus_helpers.py index 8d65d6e..e8e0a16 100644 --- a/nexus/templatetags/nexus_helpers.py +++ b/nexus/templatetags/nexus_helpers.py @@ -1,4 +1,5 @@ from django import template +from django.conf import settings from django.utils.datastructures import SortedDict import nexus @@ -9,6 +10,8 @@ def nexus_media_prefix(): + if conf.USE_STATICFILES: + return settings.STATIC_URL.rstrip('/') return conf.MEDIA_PREFIX.rstrip('/') register.simple_tag(nexus_media_prefix)