diff --git a/djangocms_installer/config/__init__.py b/djangocms_installer/config/__init__.py index 57e8979..2a6c08d 100644 --- a/djangocms_installer/config/__init__.py +++ b/djangocms_installer/config/__init__.py @@ -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) diff --git a/djangocms_installer/django/__init__.py b/djangocms_installer/django/__init__.py index 310094a..3568612 100644 --- a/djangocms_installer/django/__init__.py +++ b/djangocms_installer/django/__init__.py @@ -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") @@ -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: @@ -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) diff --git a/docs/reference.rst b/docs/reference.rst index 50f5cad..a670603 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -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 \ No newline at end of file +.. _django source: https://github.com/django/django/blob/master/django/conf/global_settings.py#L50 diff --git a/docs/usage.rst b/docs/usage.rst index 6f8e19a..d318c4a 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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 ----- diff --git a/tests/data/extra_settings.py b/tests/data/extra_settings.py new file mode 100644 index 0000000..3e1cecd --- /dev/null +++ b/tests/data/extra_settings.py @@ -0,0 +1,2 @@ +CUSTOM_SETTINGS_VAR = True +CMS_PERMISSION = False \ No newline at end of file diff --git a/tests/django.py b/tests/django.py index bf53206..06a3f97 100644 --- a/tests/django.py +++ b/tests/django.py @@ -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',