Skip to content

Commit

Permalink
Do not add settings to settings.py if they are already there
Browse files Browse the repository at this point in the history
  • Loading branch information
Glenn Jones committed Jul 31, 2020
1 parent 2f7e82d commit ad8b3f9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 16 deletions.
16 changes: 8 additions & 8 deletions pythonanywhere/django_project.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down
91 changes: 83 additions & 8 deletions tests/test_django_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
"""
)
)

Expand All @@ -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 = []
"""
)
)

Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit ad8b3f9

Please sign in to comment.