Skip to content

Commit

Permalink
Merge pull request #113 from nephila/feature/extra_settings
Browse files Browse the repository at this point in the history
Feature/extra settings
Fix #101
  • Loading branch information
yakky committed May 21, 2014
2 parents 825b470 + f19fa33 commit fdbeeab
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions djangocms_installer/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def parse(args):
default=False, help="Don't create the admin user")
parser.add_argument('--template', dest='template', action='store',
default=None, help="The path or URL to load the template from")
parser.add_argument('--extra-settings', dest='extra_settings', action='store',
default=None, help="The path to an file that contains extra settings.")

args = parser.parse_args(args)

Expand Down
8 changes: 8 additions & 0 deletions djangocms_installer/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def patch_settings(config_data):
overridden_settings = ('MIDDLEWARE_CLASSES', 'INSTALLED_APPS',
'TEMPLATE_LOADERS', 'TEMPLATE_CONTEXT_PROCESSORS',
'TEMPLATE_DIRS', 'LANGUAGES')
extra_settings = ''

if not os.path.exists(config_data.settings_path):
sys.stdout.write("Error while creating target project, please check the given configuration")
Expand All @@ -81,6 +82,11 @@ def patch_settings(config_data):
with open(config_data.settings_path, 'r') as fd_original:
original = fd_original.read()

# extra settings reading
if config_data.extra_settings and os.path.exists(config_data.extra_settings):
with open(config_data.extra_settings, 'r') as fd_extra:
extra_settings = fd_extra.read()

original = original.replace("# -*- coding: utf-8 -*-\n", "")

if original.find('BASE_DIR') == -1:
Expand Down Expand Up @@ -140,6 +146,8 @@ def patch_settings(config_data):
original += "SITE_ID = 1\n\n"

original += _build_settings(config_data)
# Append extra settings at the end of the file
original += ("\n" + extra_settings)

with open(config_data.settings_path, "w") as fd_dest:
fd_dest.write(original)
Expand Down
4 changes: 3 additions & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ advanced usage:
* ``--list-plugins``, ``-P``: List plugins that's going to be installed and
configured for the project; this will not alter the virtualenv or create the
project;
* ``--extra-settings``: Path to a file with extra variables to append to generated settings file. It doesn't need to be
a Python file, its content is blindly copied in the project settings.


.. _dj-database-url: https://github.com/kennethreitz/dj-database-url
.. _django source: https://github.com/django/django/blob/master/django/conf/global_settings.py#L50
.. _django source: https://github.com/django/django/blob/master/django/conf/global_settings.py#L50
8 changes: 8 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ This can be helpful to customize the virtualenv:

See :ref:`arguments` for arguments reference

Custom settings
---------------

If want or need to provide custom settings **before** the initial database sync is run, use `--extra-settings`
parameter.
To use this option, pass the path to a file as argument: its content is going to be appended to the generated
settings file.


HOWTO
-----
Expand Down
2 changes: 2 additions & 0 deletions tests/data/extra_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CUSTOM_SETTINGS_VAR = True
CMS_PERMISSION = False
26 changes: 26 additions & 0 deletions tests/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ def test_copy_data(self):
self.assertTrue(os.path.exists(starting_page_py))
self.assertTrue(os.path.exists(starting_page_json))

def test_patch_16_settings(self):
extra_path = os.path.join(os.path.dirname(__file__), 'data', 'extra_settings.py')
config_data = config.parse(['--db=sqlite://localhost/test.db',
'--lang=en', '--extra-settings=%s' % extra_path,
'--django-version=1.6',
'--cms-version=3.0', '--timezone=Europe/Moscow',
'-q', '-u', '-zno', '--i18n=no',
'-p'+self.project_dir, 'example_path_16_settigns'])
install.requirements(config_data.requirements)
django.create_project(config_data)
django.patch_settings(config_data)
django.copy_files(config_data)
# settings is importable even in non django environment
sys.path.append(config_data.project_directory)

project = __import__(config_data.project_name,
globals(), locals(), ['settings'])

## checking for django options
self.assertTrue(project.settings.MEDIA_ROOT, os.path.join(config_data.project_directory, 'media'))
self.assertEqual(project.settings.MEDIA_URL, '/media/')

# Data from external settings file
self.assertEqual(project.settings.CUSTOM_SETTINGS_VAR, True)
self.assertEqual(project.settings.CMS_PERMISSION, False)

def test_patch_16(self):
config_data = config.parse(['--db=sqlite://localhost/test.db',
'--lang=en',
Expand Down

0 comments on commit fdbeeab

Please sign in to comment.