8000 move create_webapp to being a method on Webapp object · pythonanywhere/helper_scripts@2eb2603 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2eb2603

Browse files
committed
move create_webapp to being a method on Webapp object
1 parent 9dd8a53 commit 2eb2603

8 files changed

+47
-47
lines changed

pythonanywhere/api.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,21 @@ def sanity_checks(self, nuke):
7070

7171

7272

73-
def create_webapp(domain, python_version, virtualenv_path, project_path, nuke):
74-
print(snakesay('Creating web app via API'))
75-
if nuke:
76-
webapp_url = API_ENDPOINT.format(username=getpass.getuser()) + domain + '/'
77-
call_api(webapp_url, 'delete')
78-
post_url = API_ENDPOINT.format(username=getpass.getuser())
79-
patch_url = post_url + domain + '/'
80-
response = call_api(post_url, 'post', data={
81-
'domain_name': domain, 'python_version': PYTHON_VERSIONS[python_version]},
82-
)
83-
if not response.ok or response.json().get('status') == 'ERROR':
84-
raise Exception(f'POST to create webapp via API failed, got {response}:{response.text}')
85-
response = call_api(patch_url, 'patch', data={'virtualenv_path': virtualenv_path})
86-
if not response.ok:
87-
raise Exception(f'PATCH to set virtualenv path via API failed, got {response}:{response.text}')
73+
def create(self, python_version, virtualenv_path, project_path, nuke):
74+
print(snakesay('Creating web app via API'))
75+
if nuke:
76+
webapp_url = API_ENDPOINT.format(username=getpass.getuser()) + self.domain + '/'
77+
call_api(webapp_url, 'delete')
78+
post_url = API_ENDPOINT.format(username=getpass.getuser())
79+
patch_url = post_url + self.domain + '/'
80+
response = call_api(post_url, 'post', data={
81+
'domain_name': self.domain, 'python_version': PYTHON_VERSIONS[python_version]},
82+
)
83+
if not response.ok or response.json().get('status') == 'ERROR':
84+
raise Exception(f'POST to create webapp via API failed, got {response}:{response.text}')
85+
response = call_api(patch_url, 'patch', data={'virtualenv_path': virtualenv_path})
86+
if not response.ok:
87+
raise Exception(f'PATCH to set virtualenv path via API failed, got {response}:{response.text}')
8888

8989

9090

pythonanywhere/django_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,7 @@ def update_wsgi_file(self):
8888
with open(self.wsgi_file_path, 'w') as f:
8989
f.write(template.format(project_path=self.project_path))
9090

91+
92+
def create_webapp(self, nuke):
93+
self.webapp.create(self.python_version, self.virtualenv_path, self.project_path, nuke=nuke)
94+

scripts/pa_autoconfigure_django.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import subprocess
2323
import shutil
2424

25-
from pythonanywhere.api import create_webapp
2625
from pythonanywhere.django_project import DjangoProject
2726

2827

@@ -39,17 +38,17 @@ def main(repo_url, domain, python_version, nuke):
3938
username = getpass.getuser().lower()
4039
domain = f'{username}.pythonanywhere.com'
4140

42-
project_path = download_repo(repo_url, domain, nuke=nuke)
41+
download_repo(repo_url, domain, nuke=nuke)
4342

4443
project = DjangoProject(domain)
4544
project.sanity_checks(nuke=nuke)
4645
project.create_virtualenv(python_version, 'django', nuke=nuke)
4746

48-
create_webapp(domain, python_version, project.virtualenv_path, project_path, nuke=nuke)
4947

5048
project.update_wsgi_file()
5149
project.update_settings_file()
5250
project.run_collectstatic()
51+
project.create_webapp(nuke=nuke)
5352
# add_static_file_mappings(domain, project_path)
5453
# reload_webapp(domain)
5554

scripts/pa_start_django_webapp_with_virtualenv.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from pythonanywhere.snakesay import snakesay
1919
from pythonanywhere.api import (
2020
add_static_file_mappings,
21-
create_webapp,
2221
reload_webapp,
2322
)
2423

@@ -36,8 +35,7 @@ def main(domain, django_version, python_version, nuke):
3635
project.run_startproject(nuke=nuke)
3736
project.update_settings_file()
3837
project.run_collectstatic()
39-
40-
create_webapp(domain, python_version, project.virtualenv_path, project.project_path, nuke=nuke)
38+
project.create_webapp(nuke=nuke)
4139
add_static_file_mappings(domain, project.project_path)
4240

4341
project.update_wsgi_file()

tests/test_api.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Webapp,
1212
add_static_file_mappings,
1313
call_api,
14-
create_webapp,
1514
reload_webapp,
1615
)
1716
from pythonanywhere.exceptions import SanityException
@@ -97,7 +96,7 @@ def test_does_post_to_create_webapp(self, api_responses, api_token):
9796
api_responses.add(responses.POST, expected_post_url, status=201, body=json.dumps({'status': 'OK'}))
9897
api_responses.add(responses.PATCH, expected_patch_url, status=200)
9998

100-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=False)
99+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=False)
101100

102101
post = api_responses.calls[0]
103102
assert post.request.url == expected_post_url
@@ -114,7 +113,7 @@ def test_does_patch_to_update_virtualenv_path(self, api_responses, api_token):
114113
api_responses.add(responses.POST, expected_post_url, status=201, body=json.dumps({'status': 'OK'}))
115114
api_responses.add(responses.PATCH, expected_patch_url, status=200)
116115

117-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=False)
116+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=False)
118117

119118
patch = api_responses.calls[1]
120119
assert patch.request.url == expected_patch_url
@@ -129,7 +128,7 @@ def test_raises_if_post_does_not_20x(self, api_responses, api_token):
129128
api_responses.add(responses.POST, expected_post_url, status=500, body='an error')
130129

131130
with pytest.raises(Exception) as e:
132-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=False)
131+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=False)
133132

134133
assert 'POST to create webapp via API failed' in str(e.value)
135134
assert 'an error' in str(e.value)
@@ -142,7 +141,7 @@ def test_raises_if_post_returns_a_200_with_status_error(self, api_responses, api
142141
}))
143142

144143
with pytest.raises(Exception) as e:
145-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=False)
144+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=False)
146145

147146
assert 'POST to create webapp via API failed' in str(e.value)
148147
assert 'bad things happened' in str(e.value)
@@ -155,7 +154,7 @@ def test_raises_if_patch_does_not_20x(self, api_responses, api_token):
155154
api_responses.add(responses.PATCH, expected_patch_url, status=400, json={'message': 'an error'})
156155

157156
with pytest.raises(Exception) as e:
158-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=False)
157+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=False)
159158

160159
assert 'PATCH to set virtualenv path via API failed' in str(e.value)
161160
assert 'an error' in str(e.value)
@@ -168,7 +167,7 @@ def test_does_delete_first_for_nuke_call(self, api_responses, api_token):
168167
api_responses.add(responses.POST, post_url, status=201, body=json.dumps({'status': 'OK'}))
169168
api_responses.add(responses.PATCH, webapp_url, status=200)
170169

171-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=True)
170+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=True)
172171

173172
delete = api_responses.calls[0]
174173
assert delete.request.method == 'DELETE'
@@ -183,7 +182,7 @@ def test_ignores_404_from_delete_call_when_nuking(self, api_responses, api_token
183182
api_responses.add(responses.POST, post_url, status=201, body=json.dumps({'status': 'OK'}))
184183
api_responses.add(responses.PATCH, webapp_url, status=200)
185184

186-
create_webapp('mydomain.com', '2.7', '/virtualenv/path', '/project/path', nuke=True)
185+
Webapp('mydomain.com').create('2.7', '/virtualenv/path', '/project/path', nuke=True)
187186

188187

189188

tests/test_django_project.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def test_nuke_option_overrides_directory_checks(self, fake_home, virtualenvs_fol
7777
project.sanity_checks(nuke=True) # should not raise
7878

7979

80+
8081
class TestCreateVirtualenv:
8182

8283
def test_calls_create_virtualenv(self):
@@ -111,6 +112,7 @@ def test_sets_python_version_attribute(self):
111112
assert project.python_version == 'python.version'
112113

113114

115+
114116
class TestRunStartproject:
115117

116118
def test_creates_folder(self, mock_subprocess, fake_home):
@@ -222,3 +224,17 @@ def test_updates_wsgi_file_from_template(self):
222224
contents = f.read()
223225
assert contents == template.format(project_path=project.project_path)
224226

227+
228+
class TestCreateWebapp:
229+
230+
def test_calls_webapp_create(self):
231+
project = DjangoProject('mydomain.com')
232+
project.webapp.create = Mock()
233+
project.python_version = 'python.version'
234+
235+
project.create_webapp(nuke='nuke option')
236+
assert project.webapp.create.call_args == call(
237+
'python.version', project.virtualenv_path, project.project_path, nuke='nuke option'
238+
)
239+
240+

tests/test_pa_autoconfigure_django.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ def mock_main_functions():
1313
patchers = []
1414
functions = [
1515
'download_repo',
16-
'create_webapp',
1716
'DjangoProject',
18-
# 'add_static_file_mappings',
19-
# 'reload_webapp',
2017
]
2118
for function in functions:
2219
mock = getattr(mocks, function)
@@ -42,20 +39,14 @@ def test_calls_all_stuff_in_right_order(self, mock_main_functions):
4239
assert mock_main_functions.method_calls == [
4340
call.download_repo('https://github.com/pythonanywhere.com/example-django-project.git', 'www.domain.com', nuke='nuke option'),
4441
call.DjangoProject('www.domain.com'),
45-
call.create_webapp(
46-
'www.domain.com',
47-
'python.version',
48-
mock_django_project.virtualenv_path,
49-
mock_main_functions.download_repo.return_value,
50-
nuke='nuke option'
51-
),
5242
]
5343
assert mock_django_project.method_calls == [
5444
call.sanity_checks(nuke='nuke option'),
5545
call.create_virtualenv('python.version', 'django', nuke='nuke option'),
5646
call.update_wsgi_file(),
5747
call.update_settings_file(),
5848
call.run_collectstatic(),
49+
call.create_webapp(nuke='nuke option'),
5950
]
6051

6152

tests/test_pa_start_django_webapp_with_virtualenv.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def mock_main_functions():
1515
patchers = []
1616
functions = [
1717
'DjangoProject',
18-
'create_webapp',
1918
'add_static_file_mappings',
2019
'reload_webapp',
2120
]
@@ -42,13 +41,6 @@ def test_calls_all_stuff_in_right_order(self, mock_main_functions):
4241
mock_django_project = mock_main_functions.DjangoProject.return_value
4342
assert mock_main_functions.method_calls == [
4443
call.DjangoProject('www.domain.com'),
45-
call.create_webapp(
46-
'www.domain.com',
47-
'python.version',
48-
mock_django_project.virtualenv_path,
49-
mock_django_project.project_path,
50-
nuke='nuke option'
51-
),
5244
call.add_static_file_mappings('www.domain.com', mock_django_project.project_path),
5345
call.reload_webapp('www.domain.com')
5446
]
@@ -58,6 +50,7 @@ def test_calls_all_stuff_in_right_order(self, mock_main_functions):
5850
call.run_startproject(nuke='nuke option'),
5951
call.update_settings_file(),
6052
call.run_collectstatic(),
53+
call.create_webapp(nuke='nuke option'),
6154
call.update_wsgi_file(),
6255
]
6356

0 commit comments

Comments
 (0)
0