Skip to content
Browse files

Move get_* functions from utils into settings and update tests.

  • Loading branch information...
1 parent 8c94510 commit b1e2477fdef3971bff9a21757683602fca8f980d @rfreebern rfreebern committed
Showing with 97 additions and 48 deletions.
  1. +54 −0 funfactory/settings_base.py
  2. +0 −29 funfactory/utils.py
  3. +0 −18 tests/test__utils.py
  4. +43 −1 tests/test_settings.py
View
54 funfactory/settings_base.py
@@ -8,6 +8,7 @@
from .manage import ROOT, path
+
# For backwards compatability, (projects built based on cloning playdoh)
# we still have to have a ROOT_URLCONF.
# For new-style playdoh projects this will be overridden automatically
@@ -104,6 +105,7 @@
'en-US',
)
+
def lazy_lang_url_map():
from django.conf import settings
langs = settings.DEV_LANGUAGES if settings.DEV else settings.PROD_LANGUAGES
@@ -111,6 +113,7 @@ def lazy_lang_url_map():
LANGUAGE_URL_MAP = lazy(lazy_lang_url_map, dict)()
+
# Override Django's built-in with our native names
def lazy_langs():
from django.conf import settings
@@ -184,6 +187,24 @@ def lazy_langs():
#'jingo_minify.helpers.build_ids',
)
+
+def get_template_context_processors(exclude=(), append=(),
+ current={'processors': TEMPLATE_CONTEXT_PROCESSORS}):
+ """
+ Returns TEMPLATE_CONTEXT_PROCESSORS without the processors listed in
+ exclude and with the processors listed in append.
+
+ The use of a mutable dict is intentional, in order to preserve the state of
+ the TEMPLATE_CONTEXT_PROCESSORS tuple across multiple settings files.
+ """
+
+ current['processors'] = tuple(
+ [p for p in current['processors'] if p not in exclude]
+ ) + tuple(append)
+
+ return current['processors']
+
+
TEMPLATE_DIRS = (
path('templates'),
)
@@ -207,6 +228,7 @@ def lazy_langs():
'compressor.finders.CompressorFinder',
)
+
def JINJA_CONFIG():
import jinja2
from django.conf import settings
@@ -241,6 +263,23 @@ def JINJA_CONFIG():
'mobility.middleware.XMobileMiddleware',
)
+
+def get_middleware(exclude=(), append=(),
+ current={'middleware': MIDDLEWARE_CLASSES}):
+ """
+ Returns MIDDLEWARE_CLASSES without the middlewares listed in exclude and
+ with the middlewares listed in append.
+
+ The use of a mutable dict is intentional, in order to preserve the state of
+ the MIDDLEWARE_CLASSES tuple across multiple settings files.
+ """
+
+ current['middleware'] = tuple(
+ [m for m in current['middleware'] if m not in exclude]
+ ) + tuple(append)
+ return current['middleware']
+
+
INSTALLED_APPS = (
# Local apps
'funfactory', # Content common to most playdoh-based apps.
@@ -273,6 +312,21 @@ def JINJA_CONFIG():
'product_details',
)
+
+def get_apps(exclude=(), append=(), current={'apps': INSTALLED_APPS}):
+ """
+ Returns INSTALLED_APPS without the apps listed in exclude and with the apps
+ listed in append.
+
+ The use of a mutable dict is intentional, in order to preserve the state of
+ the INSTALLED_APPS tuple across multiple settings files.
+ """
+
+ current['apps'] = tuple(
+ [a for a in current['apps'] if a not in exclude]
+ ) + tuple(append)
+ return current['apps']
+
# Path to Java. Used for compress_assets.
JAVA_BIN = '/usr/bin/java'
View
29 funfactory/utils.py
@@ -2,7 +2,6 @@
from django.conf import settings
-import settings_base
log = logging.getLogger('funfactory')
@@ -22,31 +21,3 @@ def absolutify(url):
site_url = ''.join(map(str, (protocol, hostname, ':', port)))
return site_url + url
-
-
-def get_middleware(exclude):
- """
- Returns the default funfactory MIDDLEWARE_CLASSES list without the
- middlewares listed in exclude.
- """
-
- return filter(lambda m: m not in exclude, settings_base.MIDDLEWARE_CLASSES)
-
-
-def get_apps(exclude):
- """
- Returns the default funfactory INSTALLED_APPS list without the apps
- listed in exclude.
- """
-
- return filter(lambda a: a not in exclude, settings_base.INSTALLED_APPS)
-
-
-def get_template_context_processors(exclude):
- """
- Returns the default funfactory TEMPLATE_CONTEXT_PROCESSORS without the
- processors listed in exclude.
- """
-
- return filter(lambda p: p not in exclude,
- settings_base.TEMPLATE_CONTEXT_PROCESSORS)
View
18 tests/test__utils.py
@@ -26,21 +26,3 @@ def test_https(self):
def test_with_port(self):
url = utils.absolutify(AbsolutifyTests.ABS_PATH)
eq_('http://test.mo.com:8009/some/absolute/path', url)
-
-
-@patch.object(utils.settings_base, 'INSTALLED_APPS', ('one', 'two', 'three'))
-class GetAppsTests(TestCase):
- def test_exclude(self):
- eq_(utils.get_apps(exclude=['three']), ('one', 'two'))
-
-
-@patch.object(utils.settings_base, 'MIDDLEWARE_CLASSES', ('foo', 'bar', 'baz'))
-class GetMiddlewareTests(TestCase):
- def test_exclude(self):
- eq_(utils.get_middleware(exclude=['foo']), ('bar', 'baz'))
-
-
-@patch.object(utils.settings_base, 'TEMPLATE_CONTEXT_PROCESSORS', ('a', 'b'))
-class GetTemplateContextProcessorsTests(TestCase):
- def test_exclude(self):
- eq_(utils.get_template_context_processors(exclude=['a']), ('b',))
View
44 tests/test_settings.py
@@ -2,9 +2,11 @@
from django.core.exceptions import ImproperlyConfigured
from mock import Mock, patch
-from nose.tools import raises
+from nose.tools import eq_, raises
from funfactory.manage import validate_settings
+from funfactory.settings_base import (get_apps, get_middleware,
+ get_template_context_processors)
@patch.object(settings, 'DEBUG', True)
@@ -59,3 +61,43 @@ def test_empty_hmac_in_dev():
@patch.object(settings, 'SESSION_COOKIE_SECURE', False)
def test_empty_hmac_in_prod():
validate_settings(settings)
+
+
+def test_get_apps():
+ eq_(get_apps(exclude=('chico',),
+ current={'apps': ('groucho', 'harpo', 'chico')}),
+ ('groucho', 'harpo'))
+ eq_(get_apps(append=('zeppo',),
+ current={'apps': ('groucho', 'harpo', 'chico')}),
+ ('groucho', 'harpo', 'chico', 'zeppo'))
+ eq_(get_apps(exclude=('harpo', 'zeppo'), append=('chico',),
+ current={'apps': ('groucho', 'harpo', 'zeppo')}),
+ ('groucho', 'chico'))
+ eq_(get_apps(exclude=('funfactory'), append=('gummo',)), get_apps())
+
+
+def test_get_middleware():
+ eq_(get_middleware(exclude=['larry', 'moe'],
+ current={'middleware': ('larry', 'curly', 'moe')}),
+ ('curly',))
+ eq_(get_middleware(append=('shemp', 'moe'),
+ current={'middleware': ('larry', 'curly')}),
+ ('larry', 'curly', 'shemp', 'moe'))
+ eq_(get_middleware(exclude=('curly'), append=['moe'],
+ current={'middleware': ('shemp', 'curly', 'larry')}),
+ ('shemp', 'larry', 'moe'))
+ eq_(get_middleware(append=['emil']), get_middleware())
+
+
+def test_get_processors():
+ eq_(get_template_context_processors(exclude=('aramis'),
+ current={'processors': ('athos', 'porthos', 'aramis')}),
+ ('athos', 'porthos'))
+ eq_(get_template_context_processors(append=("d'artagnan",),
+ current={'processors': ('athos', 'porthos')}),
+ ('athos', 'porthos', "d'artagnan"))
+ eq_(get_template_context_processors(exclude=['athos'], append=['aramis'],
+ current={'processors': ('athos', 'porthos', "d'artagnan")}),
+ ('porthos', "d'artagnan", 'aramis'))
+ eq_(get_template_context_processors(append=['richelieu']),
+ get_template_context_processors())

0 comments on commit b1e2477

Please sign in to comment.
Something went wrong with that request. Please try again.