8000 DjangoProject no longer initialized with virtualenv · pythonanywhere/helper_scripts@6814663 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6814663

Browse files
committed
DjangoProject no longer initialized with virtualenv
1 parent 5c035cc commit 6814663

7 files changed

+57
-27
lines changed

pythonanywhere/django_project.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88

99
class DjangoProject:
10-
def __init__(self, domain, virtualenv_path):
10+
11+
def __init__(self, domain):
1112
self.domain = domain
12-
self.virtualenv_path = virtualenv_path
1313
self.project_path = Path('~/').expanduser() / self.domain
1414
self.wsgi_file_path = '/var/www/' + domain.replace('.', '_') + '_wsgi.py'
1515

pythonanywhere/sanity_checks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def sanity_checks(domain, nuke):
3232
raise SanityException(f'You already have a webapp for {domain}.\n\nUse the --nuke option if you want to replace it.')
3333
if _virtualenv_path(domain).exists():
3434
raise SanityException(f'You already have a virtualenv for {domain}.\n\nUse the --nuke option if you want to replace it.')
35-
project = DjangoProject(domain, '') # TODO: make non-django-specific parent class
35+
project = DjangoProject(domain) # TODO: make non-django-specific parent class
3636
if project.project_path.exists():
3737
raise SanityException(f'You already have a project folder at {project.project_path}.\n\nUse the --nuke option if you want to replace it.')
3838

scripts/pa_autoconfigure_django.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def main(repo_url, domain, python_version, nuke):
4747

4848
create_webapp(domain, python_version, virtualenv, project_path, nuke=nuke)
4949

50-
project = DjangoProject(domain, virtualenv)
50+
project = DjangoProject(domain)
51+
project.virtualenv = virtualenv
5152
project.update_wsgi_file()
5253
project.update_settings_file()
5354
project.run_collectstatic()

scripts/pa_start_django_webapp_with_virtualenv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def main(domain, django_version, python_version, nuke):
3535
packages = 'django' if django_version == 'latest' else f'django=={django_version}'
3636
virtualenv = create_virtualenv(domain, python_version, packages, nuke=nuke)
3737

38-
project = DjangoProject(domain, virtualenv)
38+
project = DjangoProject(domain)
39+
project.virtualenv_path = virtualenv
3940
project.run_startproject(nuke=nuke)
4041
project.update_settings_file()
4142
project.run_collectstatic()

tests/test_django_project.py

Lines changed: 19 additions & 16 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
class TestDjangoProject:
1212

1313
def test_project_path(self, fake_home):
14-
project = DjangoProject('mydomain.com', '/path/to/virtualenv')
14+
project = DjangoProject('mydomain.com')
1515
assert project.project_path == fake_home / 'mydomain.com'
1616

1717

1818
def test_wsgi_file_path(self, fake_home):
19-
project = DjangoProject('mydomain.com', '/path/to/virtualenv')
19+
project = DjangoProject('mydomain.com')
2020
assert project.wsgi_file_path == '/var/www/mydomain_com_wsgi.py'
2121

2222

2323

2424
class TestCreateVirtualenv:
2525

2626
def xtest_calls_create_virtualenv_with_latest_django_by_default(self):
27-
project = DjangoProject('mydomain.com', '/path/to/virtualenv')
27+
project = DjangoProject('mydomain.com')
2828
with patch('pythonanywhere.django_project.create_virtualenv') as mock_create_virtualenv:
2929
project.create_virtualenv()
3030
assert mock_create_virtualenv.call_args == call(
@@ -35,13 +35,16 @@ def xtest_calls_create_virtualenv_with_latest_django_by_default(self):
3535
class TestRunStartproject:
3636

3737
def test_creates_folder(self, mock_subprocess, fake_home):
38-
project = DjangoProject('mydomain.com', '/path/to/virtualenv')
38+
project = DjangoProject('mydomain.com')
39+
project.virtualenv_path = '/path/to/virtualenv'
3940
project.run_startproject(nuke=False)
4041
assert (fake_home / 'mydomain.com').is_dir()
4142

4243

4344
def test_calls_startproject(self, mock_subprocess, fake_home):
44-
DjangoProject('mydomain.com', '/path/to/virtualenv').run_startproject(nuke=False)
45+
project = DjangoProject('mydomain.com')
46+
project.virtualenv_path = '/path/to/virtualenv'
47+
project.run_startproject(nuke=False)
4548
assert mock_subprocess.check_call.call_args == call([
4649
Path('/path/to/virtualenv/bin/django-admin.py'),
4750
'startproject',
@@ -51,13 +54,14 @@ def test_calls_startproject(self, mock_subprocess, fake_home):
5154

5255

5356
def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home):
54-
domain = 'mydomain.com'
55-
(fake_home / domain).mkdir()
56-
old_file = fake_home / domain / 'old_file.py'
57+
project = DjangoProject('mydomain.com')
58+
project.virtualenv_path = '/path/to/virtualenv'
59+
(fake_home / project.domain).mkdir()
60+
old_file = fake_home / project.domain / 'old_file.py'
5761
with open(old_file, 'w') as f:
5862
f.write('old stuff')
5963

60-
DjangoProject(domain, '/path/to/virtualenv').run_startproject(nuke=True) # should not raise
64+
project.run_startproject(nuke=True) # should not raise
6165

6266
assert not old_file.exists()
6367

@@ -66,7 +70,7 @@ def test_nuke_option_deletes_directory_first(self, mock_subprocess, fake_home):
6670
class TestUpdateSettingsFile:
6771

6872
def test_adds_STATIC_and_MEDIA_config_to_settings(self):
69-
project = DjangoProject('mydomain.com', 'ignored')
73+
project = DjangoProject('mydomain.com')
7074
project.project_path = Path(tempfile.mkdtemp())
7175
(project.project_path / 'mysite').mkdir(parents=True)
7276
with open(project.project_path / 'mysite/settings.py', 'w') as f:
@@ -91,7 +95,7 @@ def test_adds_STATIC_and_MEDIA_config_to_settings(self):
9195

9296

9397
def test_adds_domain_to_ALLOWED_HOSTS(self):
94-
project = DjangoProject('mydomain.com', 'ignored')
98+
project = DjangoProject('mydomain.com')
9599
project.project_path = Path(tempfile.mkdtemp())
96100
(project.project_path / 'mysite').mkdir(parents=True)
97101
with open(project.project_path / 'mysite/settings.py', 'w') as f:
@@ -117,20 +121,19 @@ def test_adds_domain_to_ALLOWED_HOSTS(self):
117121
class TestRunCollectStatic:
118122

119123
def test_runs_manage_py_in_correct_virtualenv(self, mock_subprocess, fake_home):
120-
domain, virtualenv = 'mydomain.com', '/path/to/virtualenv'
121-
project = DjangoProject(domain, virtualenv)
124+
project = DjangoProject('mydomain.com')
125+
project.virtualenv_path = '/path/to/virtualenv'
122126
project.run_collectstatic()
123127
assert mock_subprocess.check_call.call_args == call([
124-
Path(virtualenv) / 'bin/python', project.project_path / 'manage.py', 'collectstatic', '--noinput'
128+
Path(project.virtualenv_path) / 'bin/python', project.project_path / 'manage.py', 'collectstatic', '--noinput'
125129
])
126130

127131

128132

129133
class TestUpdateWsgiFile:
130134

131135
def test_updates_wsgi_file_from_template(self):
132-
domain, virtualenv = 'mydomain.com', '/path/to/virtualenv'
133-
project = DjangoProject(domain, virtualenv)
136+
project = DjangoProject('mydomain.com')
134137
project.wsgi_file_path = tempfile.NamedTemporaryFile().name
135138
template = open(Path(pythonanywhere.django_project.__file__).parent / 'wsgi_file_template.py').read()
136139

tests/test_pa_autoconfigure_django.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class TestMain:
4040

4141
def test_calls_all_stuff_in_right_order(self, mock_main_functions):
4242
main('https://github.com/pythonanywhere.com/example-django-project.git', 'www.domain.com', 'python.version', nuke='nuke option')
43+
mock_django_project = mock_main_functions.DjangoProject.return_value
4344
assert mock_main_functions.method_calls == [
4445
call.sanity_checks('www.domain.com', nuke='nuke option'),
4546
call.download_repo('https://github.com/pythonanywhere.com/example-django-project.git', 'www.domain.com', nuke='nuke option'),
@@ -53,9 +54,9 @@ def test_calls_all_stuff_in_right_order(self, mock_main_functions):
5354
mock_main_functions.download_repo.return_value,
5455
nuke='nuke option'
5556
),
56-
call.DjangoProject('www.domain.com', mock_main_functions.create_virtualenv.return_value),
57+
call.DjangoProject('www.domain.com'),
5758
]
58-
assert mock_main_functions.DjangoProject.return_value.method_calls == [
59+
assert mock_django_project.method_calls == [
5960
call.update_wsgi_file(),
6061
call.update_settings_file(),
6162
call.run_collectstatic(),

tests/test_pa_start_django_webapp_with_virtualenv.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ def mock_main_functions():
1414
mocks = Mock()
1515
patchers = []
1616
functions = [
17+
'DjangoProject',
1718
'sanity_checks',
1819
'create_virtualenv',
19-
'DjangoProject.run_startproject',
20-
'DjangoProject.update_settings_file',
21-
'DjangoProject.run_collectstatic',
2220
'create_webapp',
2321
'add_static_file_mappings',
24-
'DjangoProject.update_wsgi_file',
2522
'reload_webapp',
2623
]
2724
for function in functions:
@@ -42,6 +39,33 @@ def mock_main_functions():
4239

4340
class TestMain:
4441

42+
def test_calls_all_stuff_in_right_order(self, mock_main_functions):
43+
main('www.domain.com', 'django.version', 'python.version', nuke='nuke option')
44+
mock_django_project = mock_main_functions.DjangoProject.return_value
45+
assert mock_main_functions.method_calls == [
46+
call.sanity_checks('www.domain.com', nuke='nuke option'),
47+
call.create_virtualenv(
48+
'www.domain.com', 'python.version', 'django==django.version', nuke='nuke option'
49+
),
50+
call.DjangoProject('www.domain.com'),
51+
call.create_webapp(
52+
'www.domain.com',
53+
'python.version',
54+
mock_main_functions.create_virtualenv.return_value,
55+
mock_django_project.project_path,
56+
nuke='nuke option'
57+
),
58+
call.add_static_file_mappings('www.domain.com', mock_django_project.project_path),
59+
call.reload_webapp('www.domain.com')
60+
]
61+
assert mock_django_project.method_calls == [
62+
call.run_startproject(nuke='nuke option'),
63+
call.update_settings_file(),
64+
call.run_collectstatic(),
65+
call.update_wsgi_file(),
66+
]
67+
68+
4569
def test_domain_defaults_to_using_current_username(self, mock_main_functions):
4670
username = getpass.getuser()
4771
main('your-username.pythonanywhere.com', 'django.version', 'python.version', nuke=False)

0 commit comments

Comments
 (0)
0