Skip to content

Commit ad8b3f9

Browse files
author
Glenn Jones
committed
Do not add settings to settings.py if they are already there
1 parent 2f7e82d commit ad8b3f9

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

pythonanywhere/django_project.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
2+
import re
23
import shutil
34
import subprocess
4-
from textwrap import dedent
55

66
from pythonanywhere.exceptions import SanityException
77
from pythonanywhere.snakesay import snakesay
@@ -67,13 +67,13 @@ def update_settings_file(self):
6767
'ALLOWED_HOSTS = []',
6868
f'ALLOWED_HOSTS = [{self.domain!r}]'
6969
)
70-
new_settings += dedent(
71-
"""
72-
MEDIA_URL = '/media/'
73-
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
74-
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
75-
"""
76-
)
70+
if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None:
71+
new_settings += "\nMEDIA_URL = '/media/'"
72+
if re.search(r'^STATIC_ROOT\s*=', settings, flags=re.MULTILINE) is None:
73+
new_settings += "\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static')"
74+
if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None:
75+
new_settings += "\nMEDIA_ROOT = os.path.join(BASE_DIR, 'media')"
76+
7777
with self.settings_path.open('w') as f:
7878
f.write(new_settings)
7979

tests/test_django_project.py

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self, virtualenvs_folder):
204204
f.write(
205205
dedent(
206206
"""
207-
# settings file
208-
STATIC_URL = '/static/'
209-
ALLOWED_HOSTS = []
210-
"""
207+
# settings file
208+
STATIC_URL = '/static/'
209+
ALLOWED_HOSTS = []
210+
"""
211211
)
212212
)
213213

@@ -229,10 +229,10 @@ def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder):
229229
f.write(
230230
dedent(
231231
"""
232-
# settings file
233-
STATIC_URL = '/static/'
234-
ALLOWED_HOSTS = []
235-
"""
232+
# settings file
233+
STATIC_URL = '/static/'
234+
ALLOWED_HOSTS = []
235+
"""
236236
)
237237
)
238238

@@ -243,6 +243,78 @@ def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder):
243243

244244
assert "ALLOWED_HOSTS = ['mydomain.com']" in lines
245245

246+
def test_only_adds_MEDIA_URL_if_its_not_already_there(self, virtualenvs_folder):
247+
project = DjangoProject("mydomain.com", "python.version")
248+
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
249+
250+
with project.settings_path.open("w") as f:
251+
f.write(
252+
dedent(
253+
"""
254+
# settings file
255+
STATIC_URL = '/static/'
256+
ALLOWED_HOSTS = []
257+
MEDIA_ROOT = media_root
258+
"""
259+
)
260+
)
261+
262+
project.update_settings_file()
263+
264+
with project.settings_path.open() as f:
265+
lines = f.read().split("\n")
266+
267+
assert "MEDIA_ROOT = media_root" in lines
268+
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" not in lines
269+
270+
def test_only_adds_STATIC_ROOT_if_its_not_already_there(self, virtualenvs_folder):
271+
project = DjangoProject("mydomain.com", "python.version")
272+
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
273+
274+
with project.settings_path.open("w") as f:
275+
f.write(
276+
dedent(
277+
"""
278+
# settings file
279+
STATIC_URL = '/static/'
280+
ALLOWED_HOSTS = []
281+
STATIC_ROOT = static_root
282+
"""
283+
)
284+
)
285+
286+
project.update_settings_file()
287+
288+
with project.settings_path.open() as f:
289+
lines = f.read().split("\n")
290+
291+
assert "STATIC_ROOT = '/static/'" not in lines
292+
assert "STATIC_ROOT = static_root" in lines
293+
294+
def test_only_adds_MEDIA_ROOT_if_its_not_already_there(self, virtualenvs_folder):
295+
project = DjangoProject("mydomain.com", "python.version")
296+
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
297+
298+
with project.settings_path.open("w") as f:
299+
f.write(
300+
dedent(
301+
"""
302+
# settings file
303+
STATIC_URL = '/static/'
304+
ALLOWED_HOSTS = []
305+
MEDIA_ROOT = media_root
306+
"""
307+
)
308+
)
309+
310+
project.update_settings_file()
311+
312+
with project.settings_path.open() as f:
313+
lines = f.read().split("\n")
314+
315+
assert "MEDIA_ROOT = media_root" in lines
316+
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" not in lines
317+
246318

247319
class TestRunCollectStatic:
248320
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):
275347

276348
with project.wsgi_file_path.open() as f:
277349
contents = f.read()
350+
278351
print(contents)
279352
assert contents == template.format(project=project)
280353

@@ -289,6 +362,7 @@ def test_actually_produces_wsgi_file_that_can_import_project_non_nested(
289362
project.create_virtualenv(django_version="latest")
290363
else:
291364
project.create_virtualenv()
365+
292366
project.find_django_files()
293367
project.wsgi_file_path = Path(tempfile.NamedTemporaryFile().name)
294368

@@ -308,6 +382,7 @@ def test_actually_produces_wsgi_file_that_can_import_nested_project(
308382
project.create_virtualenv(django_version="latest")
309383
else:
310384
project.create_virtualenv()
385+
311386
project.find_django_files()
312387
project.wsgi_file_path = Path(tempfile.NamedTemporaryFile().name)
313388

0 commit comments

Comments
 (0)