From ad8b3f9e9500c0a906ff8c2810ca92c490683610 Mon Sep 17 00:00:00 2001 From: Glenn Jones Date: Fri, 31 Jul 2020 15:32:00 +0100 Subject: [PATCH] Do not add settings to settings.py if they are already there --- pythonanywhere/django_project.py | 16 +++--- tests/test_django_project.py | 91 +++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 16 deletions(-) diff --git a/pythonanywhere/django_project.py b/pythonanywhere/django_project.py index f57c83d..f722a7f 100644 --- a/pythonanywhere/django_project.py +++ b/pythonanywhere/django_project.py @@ -1,7 +1,7 @@ from pathlib import Path +import re import shutil import subprocess -from textwrap import dedent from pythonanywhere.exceptions import SanityException from pythonanywhere.snakesay import snakesay @@ -67,13 +67,13 @@ def update_settings_file(self): 'ALLOWED_HOSTS = []', f'ALLOWED_HOSTS = [{self.domain!r}]' ) - new_settings += dedent( - """ - MEDIA_URL = '/media/' - STATIC_ROOT = os.path.join(BASE_DIR, 'static') - MEDIA_ROOT = os.path.join(BASE_DIR, 'media') - """ - ) + if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None: + new_settings += "\nMEDIA_URL = '/media/'" + if re.search(r'^STATIC_ROOT\s*=', settings, flags=re.MULTILINE) is None: + new_settings += "\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static')" + if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None: + new_settings += "\nMEDIA_ROOT = os.path.join(BASE_DIR, 'media')" + with self.settings_path.open('w') as f: f.write(new_settings) diff --git a/tests/test_django_project.py b/tests/test_django_project.py index 40e7317..518906c 100644 --- a/tests/test_django_project.py +++ b/tests/test_django_project.py @@ -204,10 +204,10 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self, virtualenvs_folder): f.write( dedent( """ - # settings file - STATIC_URL = '/static/' - ALLOWED_HOSTS = [] - """ + # settings file + STATIC_URL = '/static/' + ALLOWED_HOSTS = [] + """ ) ) @@ -229,10 +229,10 @@ def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder): f.write( dedent( """ - # settings file - STATIC_URL = '/static/' - ALLOWED_HOSTS = [] - """ + # settings file + STATIC_URL = '/static/' + ALLOWED_HOSTS = [] + """ ) ) @@ -243,6 +243,78 @@ def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder): assert "ALLOWED_HOSTS = ['mydomain.com']" in lines + def test_only_adds_MEDIA_URL_if_its_not_already_there(self, virtualenvs_folder): + project = DjangoProject("mydomain.com", "python.version") + project.settings_path = Path(tempfile.NamedTemporaryFile().name) + + with project.settings_path.open("w") as f: + f.write( + dedent( + """ + # settings file + STATIC_URL = '/static/' + ALLOWED_HOSTS = [] + MEDIA_ROOT = media_root + """ + ) + ) + + project.update_settings_file() + + with project.settings_path.open() as f: + lines = f.read().split("\n") + + assert "MEDIA_ROOT = media_root" in lines + assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" not in lines + + def test_only_adds_STATIC_ROOT_if_its_not_already_there(self, virtualenvs_folder): + project = DjangoProject("mydomain.com", "python.version") + project.settings_path = Path(tempfile.NamedTemporaryFile().name) + + with project.settings_path.open("w") as f: + f.write( + dedent( + """ + # settings file + STATIC_URL = '/static/' + ALLOWED_HOSTS = [] + STATIC_ROOT = static_root + """ + ) + ) + + project.update_settings_file() + + with project.settings_path.open() as f: + lines = f.read().split("\n") + + assert "STATIC_ROOT = '/static/'" not in lines + assert "STATIC_ROOT = static_root" in lines + + def test_only_adds_MEDIA_ROOT_if_its_not_already_there(self, virtualenvs_folder): + project = DjangoProject("mydomain.com", "python.version") + project.settings_path = Path(tempfile.NamedTemporaryFile().name) + + with project.settings_path.open("w") as f: + f.write( + dedent( + """ + # settings file + STATIC_URL = '/static/' + ALLOWED_HOSTS = [] + MEDIA_ROOT = media_root + """ + ) + ) + + project.update_settings_file() + + with project.settings_path.open() as f: + lines = f.read().split("\n") + + assert "MEDIA_ROOT = media_root" in lines + assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" not in lines + class TestRunCollectStatic: def test_runs_manage_py_in_correct_virtualenv(self, mock_subprocess, fake_home, virtualenvs_folder): @@ -275,6 +347,7 @@ def test_updates_wsgi_file_from_template(self, virtualenvs_folder): with project.wsgi_file_path.open() as f: contents = f.read() + print(contents) assert contents == template.format(project=project) @@ -289,6 +362,7 @@ def test_actually_produces_wsgi_file_that_can_import_project_non_nested( project.create_virtualenv(django_version="latest") else: project.create_virtualenv() + project.find_django_files() project.wsgi_file_path = Path(tempfile.NamedTemporaryFile().name) @@ -308,6 +382,7 @@ def test_actually_produces_wsgi_file_that_can_import_nested_project( project.create_virtualenv(django_version="latest") else: project.create_virtualenv() + project.find_django_files() project.wsgi_file_path = Path(tempfile.NamedTemporaryFile().name)