Skip to content

Commit

Permalink
Special case the TEMPLATES setting, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebryant committed Nov 10, 2015
1 parent 9cc231d commit 591f00c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
20 changes: 16 additions & 4 deletions django_autoconfig/autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def apply_changes(self, settings):

return changes

def merge_dictionaries(current, new, only_defaults=False):
def merge_dictionaries(current, new, only_defaults=False, template_special_case=False):
'''
Merge two settings dictionaries, recording how many changes were needed.
Expand All @@ -115,9 +115,20 @@ def merge_dictionaries(current, new, only_defaults=False):
elif isinstance(current_value, (list, tuple)):
for element in value:
if element not in current_value:
current[key] = list(current_value) + [element]
LOGGER.debug("Added %r to %r.", element, key)
changes += 1
if template_special_case and key == 'TEMPLATES':
existing_matches = [
template for template in current_value if template['BACKEND'] == element['BACKEND']
]
if existing_matches:
changes += merge_dictionaries(existing_matches[0], element)
else:
current[key] = list(current_value) + [element]
LOGGER.debug("Added %r to %r.", element, key)
changes += 1
else:
current[key] = list(current_value) + [element]
LOGGER.debug("Added %r to %r.", element, key)
changes += 1
else:
# If we don't know what to do with it, replace it.
if current_value is not value:
Expand Down Expand Up @@ -154,6 +165,7 @@ def configure_settings(settings, environment_settings=True):
changes += merge_dictionaries(
settings,
getattr(module, 'SETTINGS', {}),
template_special_case=True,
)
changes += merge_dictionaries(
settings,
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions django_autoconfig/tests/app_18templates1/autoconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SETTINGS = {
'TEMPLATES': [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
],
},
},
],
}
Empty file.
12 changes: 12 additions & 0 deletions django_autoconfig/tests/app_18templates2/autoconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SETTINGS = {
'TEMPLATES': [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': {
'context_processors': [
'context.processor.2',
],
},
},
],
}
25 changes: 25 additions & 0 deletions django_autoconfig/tests/test_autoconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,31 @@ def test_environment_settings(self):
)
self.assertEqual(results, {'BLAH': 'test-value'})

def test_django18_templates(self):
'''
Check that the Django 1.8 TEMPLATES setting works.
'''
self.settings_dict['INSTALLED_APPS'] = [
'django_autoconfig.tests.app_18templates1',
'django_autoconfig.tests.app_18templates2',
]
autoconfig.configure_settings(self.settings_dict)
self.assertEqual(
self.settings_dict['TEMPLATES'],
[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'context.processor.2',
],
},
},
],
)


class ConfigureUrlsTestCase(test.TestCase):
'''Test the autoconfiguration of the urlconf.'''
Expand Down

0 comments on commit 591f00c

Please sign in to comment.