Skip to content

Commit 89ebbf4

Browse files
Use correct django-admin executable in DjangoProject. by: Filip, Piotr
Co-authored-by: Filip Łajszczak <[email protected]>
1 parent 40e548f commit 89ebbf4

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pythonanywhere/django_project.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,25 @@ def create_virtualenv(self, django_version=None, nuke=False):
5454
packages = f'django=={django_version}'
5555
self.virtualenv.pip_install(packages)
5656

57-
5857
def detect_requirements(self):
5958
requirements_txt = self.project_path / 'requirements.txt'
6059
if requirements_txt.exists():
6160
return f'-r {requirements_txt.resolve()}'
6261
return 'django'
6362

64-
65-
6663
def run_startproject(self, nuke):
6764
print(snakesay('Starting Django project'))
6865
if nuke and self.project_path.exists():
6966
shutil.rmtree(str(self.project_path))
7067
self.project_path.mkdir()
68+
69+
new_django = self.django_version_newer_or_equal_than("4.0")
70+
django_admin_executable = "django-admin" if new_django else "django-admin.py"
71+
7172
subprocess.check_call([
72-
str(Path(self.virtualenv.path) / 'bin/django-admin.py'),
73-
'startproject',
74-
'mysite',
73+
str(Path(self.virtualenv.path) / "bin" / django_admin_executable),
74+
"startproject",
75+
"mysite",
7576
str(self.project_path),
7677
])
7778

tests/test_django_project.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ def test_uses_detect_if_django_version_not_specified(self, project_with_mock_vir
198198
class TestRunStartproject:
199199
def test_creates_folder(self, mock_subprocess, fake_home, virtualenvs_folder):
200200
project = DjangoProject("mydomain.com", "python.version")
201+
project.virtualenv.get_version = Mock(return_value="1.0")
201202
project.run_startproject(nuke=False)
202203
assert (fake_home / "mydomain.com").is_dir()
203204

204-
def test_calls_startproject(self, mock_subprocess, fake_home, virtualenvs_folder):
205+
def test_calls_startproject_for_older_django(self, mock_subprocess, fake_home, virtualenvs_folder):
205206
project = DjangoProject("mydomain.com", "python.version")
207+
project.virtualenv.get_version = Mock(return_value="3.9")
206208
project.run_startproject(nuke=False)
207209
assert mock_subprocess.check_call.call_args == call(
208210
[
@@ -213,18 +215,34 @@ def test_calls_startproject(self, mock_subprocess, fake_home, virtualenvs_folder
213215
]
214216
)
215217

218+
219+
def test_calls_startproject_for_newer_django(self, mock_subprocess, fake_home, virtualenvs_folder):
220+
project = DjangoProject("mydomain.com", "python.version")
221+
project.virtualenv.get_version = Mock(return_value="4.0")
222+
project.run_startproject(nuke=False)
223+
assert mock_subprocess.check_call.call_args == call(
224+
[
225+
str(Path(project.virtualenv.path / "bin/django-admin")),
226+
"startproject",
227+
"mysite",
228+
str(fake_home / "mydomain.com"),
229+
]
230+
)
231+
216232
def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home, virtualenvs_folder):
217233
project = DjangoProject("mydomain.com", "python.version")
218234
(fake_home / project.domain).mkdir()
219235
old_file = fake_home / project.domain / "old_file.py"
220236
old_file.write_text("old stuff")
237+
project.virtualenv.get_version = Mock(return_value="1.0")
221238

222239
project.run_startproject(nuke=True)
223240

224241
assert not old_file.exists()
225242

226243
def test_nuke_option_handles_directory_not_existing(self, mock_subprocess, fake_home, virtualenvs_folder):
227244
project = DjangoProject("mydomain.com", "python.version")
245+
project.virtualenv.get_version = Mock(return_value="1.0")
228246
project.run_startproject(nuke=True) # should not raise
229247

230248

0 commit comments

Comments
 (0)