Skip to content

Commit 0c7525a

Browse files
#31 makes django scripts compatibile with django > 3.1. by Giles and Filip
1 parent 24b428d commit 0c7525a

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

pythonanywhere/django_project.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import shutil
44
import subprocess
55

6+
from packaging import version
7+
68
from pythonanywhere.exceptions import SanityException
79
from pythonanywhere.snakesay import snakesay
810
from .project import Project
@@ -34,6 +36,7 @@ def detect_requirements(self):
3436
return 'django'
3537

3638

39+
3740
def run_startproject(self, nuke):
3841
print(snakesay('Starting Django project'))
3942
if nuke and self.project_path.exists():
@@ -67,12 +70,21 @@ def update_settings_file(self):
6770
'ALLOWED_HOSTS = []',
6871
f'ALLOWED_HOSTS = [{self.domain!r}]'
6972
)
73+
74+
new_django = version.parse(self.virtualenv.get_version("django")) >= version.parse("3.1")
75+
7076
if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None:
7177
new_settings += "\nMEDIA_URL = '/media/'"
7278
if re.search(r'^STATIC_ROOT\s*=', settings, flags=re.MULTILINE) is None:
73-
new_settings += "\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static')"
79+
if new_django:
80+
new_settings += "\nSTATIC_ROOT = Path(BASE_DIR / 'static')"
81+
else:
82+
new_settings += "\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static')"
7483
if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None:
75-
new_settings += "\nMEDIA_ROOT = os.path.join(BASE_DIR, 'media')"
84+
if new_django:
85+
new_settings += "\nMEDIA_ROOT = Path(BASE_DIR / 'media')"
86+
else:
87+
new_settings += "\nMEDIA_ROOT = os.path.join(BASE_DIR, 'media')"
7688

7789
with self.settings_path.open('w') as f:
7890
f.write(new_settings)

pythonanywhere/virtualenvs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ def pip_install(self, packages):
2626
print(snakesay(f"Pip installing {packages} (this may take a couple of minutes)"))
2727
commands = [str(self.path / "bin/pip"), "install"] + packages.split()
2828
subprocess.check_call(commands)
29+
30+
def get_version(self, package_name):
31+
commands = [str(self.path / "bin/pip"), "show", package_name]
32+
output = subprocess.check_output(commands).decode()
33+
for line in output.splitlines():
34+
if line.startswith("Version: "):
35+
return line.split()[1]

pythonanywhere/virtualenvs.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ class Virtualenv:
1010
def __eq__(self, other: Virtualenv) -> bool: ...
1111
def create(self, nuke: bool) -> None: ...
1212
def pip_install(self, packages: List[str]) -> None: ...
13+
def get_version(self, package_name: str) -> str: ...

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
python-dateutil==2.8.1
22
docopt==0.6.2
3+
packaging
34
psutil==5.7.0
45
pytest==5.4.2
56
pytest-cov==2.8.1

tests/test_django_project.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,10 @@ def test_raises_if_manage_py_not_found(self, fake_home, non_nested_submodule, vi
196196

197197

198198
class TestUpdateSettingsFile:
199-
def test_adds_STATIC_and_MEDIA_config_to_settings(self, virtualenvs_folder):
199+
def test_adds_STATIC_and_MEDIA_config_to_settings_with_old_django(self, virtualenvs_folder):
200200
project = DjangoProject("mydomain.com", "python.version")
201201
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
202+
project.virtualenv.get_version = Mock(return_value="1.0")
202203

203204
with project.settings_path.open("w") as f:
204205
f.write(
@@ -221,9 +222,36 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self, virtualenvs_folder):
221222
assert "STATIC_ROOT = os.path.join(BASE_DIR, 'static')" in lines
222223
assert "MEDIA_ROOT = os.path.join(BASE_DIR, 'media')" in lines
223224

225+
def test_adds_STATIC_and_MEDIA_config_to_settings_with_new_django(self, virtualenvs_folder):
226+
project = DjangoProject("mydomain.com", "python.version")
227+
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
228+
project.virtualenv.get_version = Mock(return_value="3.1.1")
229+
230+
with project.settings_path.open("w") as f:
231+
f.write(
232+
dedent(
233+
"""
234+
# settings file
235+
STATIC_URL = '/static/'
236+
ALLOWED_HOSTS = []
237+
"""
238+
)
239+
)
240+
241+
project.update_settings_file()
242+
243+
with project.settings_path.open() as f:
244+
lines = f.read().split("\n")
245+
246+
assert "STATIC_URL = '/static/'" in lines
247+
assert "MEDIA_URL = '/media/'" in lines
248+
assert "STATIC_ROOT = Path(BASE_DIR / 'static')" in lines
249+
assert "MEDIA_ROOT = Path(BASE_DIR / 'media')" in lines
250+
224251
def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder):
225252
project = DjangoProject("mydomain.com", "python.version")
226253
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
254+
project.virtualenv.get_version = Mock(return_value="1.0")
227255

228256
with project.settings_path.open("w") as f:
229257
f.write(
@@ -246,6 +274,7 @@ def test_adds_domain_to_ALLOWED_HOSTS(self, virtualenvs_folder):
246274
def test_only_adds_MEDIA_URL_if_its_not_already_there(self, virtualenvs_folder):
247275
project = DjangoProject("mydomain.com", "python.version")
248276
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
277+
project.virtualenv.get_version = Mock(return_value="1.0")
249278

250279
with project.settings_path.open("w") as f:
251280
f.write(
@@ -270,6 +299,7 @@ def test_only_adds_MEDIA_URL_if_its_not_already_there(self, virtualenvs_folder):
270299
def test_only_adds_STATIC_ROOT_if_its_not_already_there(self, virtualenvs_folder):
271300
project = DjangoProject("mydomain.com", "python.version")
272301
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
302+
project.virtualenv.get_version = Mock(return_value="1.0")
273303

274304
with project.settings_path.open("w") as f:
275305
f.write(
@@ -294,6 +324,7 @@ def test_only_adds_STATIC_ROOT_if_its_not_already_there(self, virtualenvs_folder
294324
def test_only_adds_MEDIA_ROOT_if_its_not_already_there(self, virtualenvs_folder):
295325
project = DjangoProject("mydomain.com", "python.version")
296326
project.settings_path = Path(tempfile.NamedTemporaryFile().name)
327+
project.virtualenv.get_version = Mock(return_value="1.0")
297328

298329
with project.settings_path.open("w") as f:
299330
f.write(

tests/test_virtualenvs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,12 @@ def test_actually_installing_a_real_package(self, fake_home, virtualenvs_folder)
5252
v.pip_install("aafigure")
5353

5454
subprocess.check_call([str(v.path / "bin/python"), "-c" "import aafigure"])
55+
56+
@pytest.mark.slowtest
57+
def test_gets_version(self, fake_home, virtualenvs_folder):
58+
running_python_version = ".".join(python_version().split(".")[:2])
59+
v = Virtualenv("www.adomain.com", running_python_version)
60+
v.create(nuke=False)
61+
v.pip_install("aafigure==0.6")
62+
63+
assert v.get_version("aafigure") == "0.6"

0 commit comments

Comments
 (0)