-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdjango_project.py
More file actions
116 lines (90 loc) · 3.81 KB
/
django_project.py
File metadata and controls
116 lines (90 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from pathlib import Path
import re
import shutil
import subprocess
from packaging import version
from pythonanywhere.exceptions import SanityException
from pythonanywhere.snakesay import snakesay
from .project import Project
class DjangoProject(Project):
def download_repo(self, repo, nuke):
if nuke and self.project_path.exists():
shutil.rmtree(str(self.project_path))
subprocess.check_call(['git', 'clone', repo, str(self.project_path)])
def create_virtualenv(self, django_version=None, nuke=False):
self.virtualenv.create(nuke=nuke)
if django_version is None:
packages = self.detect_requirements()
elif django_version == 'latest':
packages = 'django'
else:
packages = f'django=={django_version}'
self.virtualenv.pip_install(packages)
def detect_requirements(self):
requirements_txt = self.project_path / 'requirements.txt'
if requirements_txt.exists():
return f'-r {requirements_txt.resolve()}'
return 'django'
def run_startproject(self, nuke):
print(snakesay('Starting Django project'))
if nuke and self.project_path.exists():
shutil.rmtree(str(self.project_path))
self.project_path.mkdir()
subprocess.check_call([
str(Path(self.virtualenv.path) / 'bin/django-admin.py'),
'startproject',
'mysite',
str(self.project_path),
])
def find_django_files(self):
try:
self.settings_path = next(self.project_path.glob('**/settings.py'))
except StopIteration:
raise SanityException('Could not find your settings.py')
try:
self.manage_py_path = next(self.project_path.glob('**/manage.py'))
except StopIteration:
raise SanityException('Could not find your manage.py')
def update_settings_file(self):
print(snakesay('Updating settings.py'))
with self.settings_path.open() as f:
settings = f.read()
new_settings = settings.replace(
'ALLOWED_HOSTS = []',
f'ALLOWED_HOSTS = [{self.domain!r}]'
)
new_django = version.parse(self.virtualenv.get_version("django")) >= version.parse("3.1")
if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None:
new_settings += "\nMEDIA_URL = '/media/'"
if re.search(r'^STATIC_ROOT\s*=', settings, flags=re.MULTILINE) is None:
if new_django:
new_settings += "\nSTATIC_ROOT = Path(BASE_DIR / 'static')"
else:
new_settings += "\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static')"
if re.search(r'^MEDIA_ROOT\s*=', settings, flags=re.MULTILINE) is None:
if new_django:
new_settings += "\nMEDIA_ROOT = Path(BASE_DIR / 'media')"
else:
new_settings += "\nMEDIA_ROOT = os.path.join(BASE_DIR, 'media')"
with self.settings_path.open('w') as f:
f.write(new_settings)
def run_collectstatic(self):
print(snakesay('Running collectstatic'))
subprocess.check_call([
str(Path(self.virtualenv.path) / 'bin/python'),
str(self.manage_py_path),
'collectstatic',
'--noinput',
])
def run_migrate(self):
print(snakesay('Running migrate database'))
subprocess.check_call([
str(Path(self.virtualenv.path) / 'bin/python'),
str(self.manage_py_path),
'migrate',
])
def update_wsgi_file(self):
print(snakesay(f'Updating wsgi file at {self.wsgi_file_path}'))
template = (Path(__file__).parent / 'wsgi_file_template.py').open().read()
with self.wsgi_file_path.open('w') as f:
f.write(template.format(project=self))