8000 start using parent class · pythonanywhere/helper_scripts@c0ae007 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0ae007

Browse files
committed
start using parent class
1 parent 93d1d7f commit c0ae007

File tree

3 files changed

+7
-130
lines changed

3 files changed

+7
-130
lines changed

pythonanywhere/django_project.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,13 @@
33
import subprocess
44
from textwrap import dedent
55

6-
from pythonanywhere.api import Webapp
76
from pythonanywhere.exceptions import SanityException
87
from pythonanywhere.snakesay import snakesay
9-
from pythonanywhere.virtualenvs import create_virtualenv, virtualenv_path
10-
11-
12-
class DjangoProject:
13-
14-
def __init__(self, domain, python_version):
15-
self.domain = domain
16-
self.python_version = python_version
17-
self.domain = domain
18-
self.project_path = Path('~/').expanduser() / self.domain
19-
self.wsgi_file_path = '/var/www/' + domain.replace('.', '_') + '_wsgi.py'
20-
self.webapp = Webapp(domain)
21-
self.virtualenv_path = virtualenv_path(domain)
22-
23-
24-
def sanity_checks(self, nuke):
25-
self.webapp.sanity_checks(nuke=nuke)
26-
if nuke:
27-
return
28-
if self.virtualenv_path.exists():
29-
raise SanityException(f'You already have a virtualenv for {self.domain}.\n\nUse the --nuke option if you want to replace it.')
30-
if self.project_path.exists():
31-
raise SanityException(f'You already have a project folder at {self.project_path}.\n\nUse the --nuke option if you want to replace it.')
8+
from pythonanywhere.virtualenvs import create_virtualenv
9+
from .project import Project
3210

3311

12+
class DjangoProject(Project):
3413

3514
def download_repo(self, repo, nuke):
3615
if nuke and self.project_path.exists():
@@ -127,12 +106,3 @@ def update_wsgi_file(self):
127106
with open(self.wsgi_file_path, 'w') as f:
128107
f.write(template.format(project=self))
129108

130-
131-
def create_webapp(self, nuke):
132-
self.webapp.create(self.python_version, self.virtualenv_path, self.project_path, nuke=nuke)
133-
134-
135-
def add_static_file_mappings(self):
136-
self.webapp.add_default_static_files_mappings(self.project_path)
137-
138-

pythonanywhere/project.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pythonanywhere.exceptions import SanityException
44
from pythonanywhere.virtualenvs import virtualenv_path
55

6+
67
class Virtualenv:
78
def __init__(self, domain):
89
self.domain = domain
@@ -14,8 +15,9 @@ def create(self, python_version, nuke):
1415

1516

1617
class Project:
17-
def __init__(self, domain):
18+
def __init__(self, domain, python_version):
1819
self.domain = domain
20+
self.python_version = python_version
1921
self.project_path = Path(f'~/{domain}').expanduser()
2022
self.virtualenv = Virtualenv(self.domain)
2123
self.virtualenv_path = virtualenv_path(domain)
@@ -44,4 +46,3 @@ def create_webapp(self, nuke):
4446
def add_static_file_mappings(self):
4547
self.webapp.add_default_static_files_mappings(self.project_path)
4648

47-

tests/test_django_project.py

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,6 @@
1010
import pythonanywhere.django_project
1111
from pythonanywhere.django_project import DjangoProject
1212
from pythonanywhere.exceptions import SanityException
13-
from pythonanywhere.api import Webapp
14-
from pythonanywhere.virtualenvs import virtualenv_path
15-
16-
17-
18-
class TestDjangoProject:
19-
20-
def test_project_path(self, fake_home):
21-
project = DjangoProject('mydomain.com', 'python.version')
22-
assert project.project_path == fake_home / 'mydomain.com'
23-
24-
25-
def test_wsgi_file_path(self, fake_home):
26-
project = DjangoProject('mydomain.com', 'python.version')
27-
assert project.wsgi_file_path == '/var/www/mydomain_com_wsgi.py'
28-
29-
30-
def test_webapp(self, fake_home):
31-
project = DjangoProject('mydomain.com', 'python.version')
32-
assert project.webapp == Webapp('mydomain.com')
33-
34-
35-
def test_virtualenv_path(self, fake_home):
36-
project = DjangoProject('mydomain.com', 'python.version')
37-
assert project.virtualenv_path == virtualenv_path('mydomain.com')
38-
39-
40-
41-
class TestSanityChecks:
42-
43-
def test_calls_webapp_sanity_checks(self, fake_home):
44-
project = DjangoProject('mydomain.com', 'python.version')
45-
project.webapp.sanity_checks = Mock()
46-
project.sanity_checks(nuke='nuke.option')
47-
assert project.webapp.sanity_checks.call_args == call(nuke='nuke.option')
48-
49-
50-
def test_raises_if_virtualenv_exists(self, fake_home, virtualenvs_folder):
51-
project = DjangoProject('mydomain.com', 'python.version')
52-
project.webapp.sanity_checks = Mock()
53-
project.virtualenv_path.mkdir()
54-
55-
with pytest.raises(SanityException) as e:
56-
project.sanity_checks(nuke=False)
57-
58-
assert "You already have a virtualenv for mydomain.com" in str(e.value)
59-
as F438 sert "nuke" in str(e.value)
60-
61-
62-
def test_raises_if_project_path_exists(self, fake_home, virtualenvs_folder):
63-
project = DjangoProject('mydomain.com', 'python.version')
64-
project.webapp.sanity_checks = Mock()
65-
project.project_path.mkdir()
66-
67-
with pytest.raises(SanityException) as e:
68-
project.sanity_checks(nuke=False)
69-
70-
expected_msg = f"You already have a project folder at {fake_home}/mydomain.com"
71-
assert expected_msg in str(e.value)
72-
assert "nuke" in str(e.value)
73-
74-
75-
def test_nuke_option_overrides_directory_checks(self, fake_home, virtualenvs_folder):
76-
project = DjangoProject('mydomain.com', 'python.version')
77-
project.webapp.sanity_checks = Mock()
78-
project.project_path.mkdir()
79-
project.virtualenv_path.mkdir()
80-
81-
project.sanity_checks(nuke=True) # should not raise
8213

8314

8415

@@ -136,6 +67,7 @@ def test_if_requirements_txt_exists(self, fake_home):
13667
assert project.detect_django_version() == f'-r {requirements_txt.resolve()}'
13768

13869

70+
13971
class TestCreateVirtualenv:
14072

14173
def test_calls_create_virtualenv(self):
@@ -426,29 +358,3 @@ def test_actually_produces_wsgi_file_that_can_import_nested_project(
426358
print(open(project.wsgi_file_path).read())
427359
subprocess.check_output([project.virtualenv_path / 'bin/python', project.wsgi_file_path])
428360

429-
430-
431-
432-
class TestCreateWebapp:
433-
434-
def test_calls_webapp_create(self):
435-
project = DjangoProject('mydomain.com', 'python.version')
436-
project.webapp.create = Mock()
437-
438-
project.create_webapp(nuke='nuke option')
439-
assert project.webapp.create.call_args == call(
440-
project.python_version, project.virtualenv_path, project.project_path, nuke='nuke option'
441-
)
442-
443-
444-
445-
class TestAddStaticFilesMappings:
446-
447-
def test_calls_webapp_add_default_static_files_mappings(self):
448-
project = DjangoProject('mydomain.com', 'python.version')
449-
project.webapp.add_default_static_files_mappings = Mock()
450-
project.add_static_file_mappings()
451-
assert project.webapp.add_default_static_files_mappings.call_args == call(
452-
project.project_path,
453-
)
454-

0 commit comments

Comments
 (0)
0