Skip to content

Commit 1e4a46b

Browse files
authored
Standardizing the use of settings directly (#5632)
Standardizing the use of settings directly
2 parents 571f0b9 + 54e970b commit 1e4a46b

File tree

13 files changed

+64
-91
lines changed

13 files changed

+64
-91
lines changed

readthedocs/api/v2/client.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212

1313
log = logging.getLogger(__name__)
1414

15-
PRODUCTION_DOMAIN = settings.PRODUCTION_DOMAIN
16-
API_HOST = settings.SLUMBER_API_HOST
17-
USER = settings.SLUMBER_USERNAME
18-
PASS = settings.SLUMBER_PASSWORD
19-
2015

2116
class DrfJsonSerializer(serialize.JsonSerializer):
2217

@@ -31,7 +26,7 @@ def dumps(self, data):
3126

3227
def setup_api():
3328
session = requests.Session()
34-
if API_HOST.startswith('https'):
29+
if settings.SLUMBER_API_HOST.startswith('https'):
3530
# Only use the HostHeaderSSLAdapter for HTTPS connections
3631
adapter_class = host_header_ssl.HostHeaderSSLAdapter
3732
else:
@@ -50,12 +45,12 @@ def setup_api():
5045
)
5146

5247
session.mount(
53-
API_HOST,
48+
settings.SLUMBER_API_HOST,
5449
adapter_class(max_retries=retry),
5550
)
56-
session.headers.update({'Host': PRODUCTION_DOMAIN})
51+
session.headers.update({'Host': settings.PRODUCTION_DOMAIN})
5752
api_config = {
58-
'base_url': '%s/api/v2/' % API_HOST,
53+
'base_url': '%s/api/v2/' % settings.SLUMBER_API_HOST,
5954
'serializer': serialize.Serializer(
6055
default='json-drf',
6156
serializers=[
@@ -65,13 +60,13 @@ def setup_api():
6560
),
6661
'session': session,
6762
}
68-
if USER and PASS:
63+
if settings.SLUMBER_USERNAME and settings.SLUMBER_PASSWORD:
6964
log.debug(
7065
'Using slumber v2 with user %s, pointed at %s',
71-
USER,
72-
API_HOST,
66+
settings.SLUMBER_USERNAME,
67+
settings.SLUMBER_API_HOST,
7368
)
74-
session.auth = (USER, PASS)
69+
session.auth = (settings.SLUMBER_USERNAME, settings.SLUMBER_PASSWORD)
7570
else:
7671
log.warning('SLUMBER_USERNAME/PASSWORD settings are not set')
7772
return API(**api_config)

readthedocs/builds/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
from .version_slug import VersionSlugField
5151

5252

53-
DEFAULT_VERSION_PRIVACY_LEVEL = settings.DEFAULT_VERSION_PRIVACY_LEVEL
54-
5553
log = logging.getLogger(__name__)
5654

5755

@@ -102,7 +100,7 @@ class Version(models.Model):
102100
_('Privacy Level'),
103101
max_length=20,
104102
choices=PRIVACY_CHOICES,
105-
default=DEFAULT_VERSION_PRIVACY_LEVEL,
103+
default=settings.DEFAULT_VERSION_PRIVACY_LEVEL,
106104
help_text=_('Level of privacy for this Version.'),
107105
)
108106
tags = TaggableManager(blank=True)

readthedocs/builds/syncers.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ def copy(cls, path, target, is_file=False, **kwargs):
6363
6464
Respects the ``MULTIPLE_APP_SERVERS`` setting when copying.
6565
"""
66-
sync_user = settings.SYNC_USER
67-
app_servers = settings.MULTIPLE_APP_SERVERS
68-
if app_servers:
69-
log.info('Remote Copy %s to %s on %s', path, target, app_servers)
70-
for server in app_servers:
66+
if settings.MULTIPLE_APP_SERVERS:
67+
log.info(
68+
'Remote Copy %s to %s on %s',
69+
path, target,
70+
settings.MULTIPLE_APP_SERVERS
71+
)
72+
for server in settings.MULTIPLE_APP_SERVERS:
7173
mkdir_cmd = (
72-
'ssh {}@{} mkdir -p {}'.format(sync_user, server, target)
74+
'ssh {}@{} mkdir -p {}'.format(settings.SYNC_USER, server, target)
7375
)
7476
ret = os.system(mkdir_cmd)
7577
if ret != 0:
@@ -83,7 +85,7 @@ def copy(cls, path, target, is_file=False, **kwargs):
8385
"rsync -e 'ssh -T' -av --delete {path}{slash} {user}@{server}:{target}".format(
8486
path=path,
8587
slash=slash,
86-
user=sync_user,
88+
user=settings.SYNC_USER,
8789
server=server,
8890
target=target,
8991
)
@@ -102,15 +104,13 @@ def copy(cls, path, target, host, is_file=False, **kwargs): # pylint: disable=a
102104
103105
Respects the ``MULTIPLE_APP_SERVERS`` setting when copying.
104106
"""
105-
sync_user = settings.SYNC_USER
106-
app_servers = settings.MULTIPLE_APP_SERVERS
107107
if not is_file:
108108
path += '/'
109109
log.info('Remote Copy %s to %s', path, target)
110-
for server in app_servers:
110+
for server in settings.MULTIPLE_APP_SERVERS:
111111
if not is_file:
112112
mkdir_cmd = 'ssh {user}@{server} mkdir -p {target}'.format(
113-
user=sync_user,
113+
user=settings.SYNC_USER,
114114
server=server,
115115
target=target,
116116
)
@@ -123,7 +123,7 @@ def copy(cls, path, target, host, is_file=False, **kwargs): # pylint: disable=a
123123
"--delete --exclude projects {user}@{host}:{path} {target}'".format(
124124
host=host,
125125
path=path,
126-
user=sync_user,
126+
user=settings.SYNC_USER,
127127
server=server,
128128
target=target,
129129
)
@@ -142,7 +142,6 @@ def copy(cls, path, target, host, is_file=False, **kwargs): # pylint: disable=a
142142
143143
Respects the ``MULTIPLE_APP_SERVERS`` setting when copying.
144144
"""
145-
sync_user = settings.SYNC_USER
146145
if not is_file:
147146
path += '/'
148147
log.info('Remote Pull %s to %s', path, target)
@@ -156,7 +155,7 @@ def copy(cls, path, target, host, is_file=False, **kwargs): # pylint: disable=a
156155
sync_cmd = "rsync -e 'ssh -T' -av --delete {user}@{host}:{path} {target}".format(
157156
host=host,
158157
path=path,
159-
user=sync_user,
158+
user=settings.SYNC_USER,
160159
target=target,
161160
)
162161
ret = os.system(sync_cmd)

readthedocs/config/config.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@
6666
INVALID_KEYS_COMBINATION = 'invalid-keys-combination'
6767
INVALID_KEY = 'invalid-key'
6868

69-
DOCKER_DEFAULT_IMAGE = settings.DOCKER_DEFAULT_IMAGE
70-
DOCKER_DEFAULT_VERSION = settings.DOCKER_DEFAULT_VERSION
71-
# These map to corresponding settings in the .org,
72-
# so they haven't been renamed.
73-
DOCKER_IMAGE = settings.DOCKER_IMAGE
74-
DOCKER_IMAGE_SETTINGS = settings.DOCKER_IMAGE_SETTINGS
75-
7669
LATEST_CONFIGURATION_VERSION = 2
7770

7871

@@ -159,7 +152,8 @@ class BuildConfigBase:
159152
'submodules',
160153
]
161154

162-
default_build_image = DOCKER_DEFAULT_VERSION
155+
default_build_image = settings.DOCKER_DEFAULT_VERSION
156+
163157
version = None
164158

165159
def __init__(self, env_config, raw_config, source_file):
@@ -268,7 +262,7 @@ def valid_build_images(self):
268262
``readthedocs/build`` part) plus ``stable`` and ``latest``.
269263
"""
270264
images = {'stable', 'latest'}
271-
for k in DOCKER_IMAGE_SETTINGS:
265+
for k in settings.DOCKER_IMAGE_SETTINGS:
272266
_, version = k.split(':')
273267
if re.fullmatch(r'^[\d\.]+$', version):
274268
images.add(version)
@@ -284,12 +278,12 @@ def get_valid_python_versions_for_image(self, build_image):
284278
Returns supported versions for the ``DOCKER_DEFAULT_VERSION`` if not
285279
``build_image`` found.
286280
"""
287-
if build_image not in DOCKER_IMAGE_SETTINGS:
281+
if build_image not in settings.DOCKER_IMAGE_SETTINGS:
288282
build_image = '{}:{}'.format(
289-
DOCKER_DEFAULT_IMAGE,
283+
settings.DOCKER_DEFAULT_IMAGE,
290284
self.default_build_image,
291285
)
292-
return DOCKER_IMAGE_SETTINGS[build_image]['python']['supported_versions']
286+
return settings.DOCKER_IMAGE_SETTINGS[build_image]['python']['supported_versions']
293287

294288
def as_dict(self):
295289
config = {}
@@ -326,7 +320,7 @@ def get_valid_python_versions(self):
326320
return self.env_config['python']['supported_versions']
327321
except (KeyError, TypeError):
328322
versions = set()
329-
for _, options in DOCKER_IMAGE_SETTINGS.items():
323+
for _, options in settings.DOCKER_IMAGE_SETTINGS.items():
330324
versions = versions.union(
331325
options['python']['supported_versions']
332326
)
@@ -381,7 +375,7 @@ def validate_build(self):
381375
if 'build' in self.env_config:
382376
build = self.env_config['build'].copy()
383377
else:
384-
build = {'image': DOCKER_IMAGE}
378+
build = {'image': settings.DOCKER_IMAGE}
385379

386380
# User specified
387381
if 'build' in self.raw_config:
@@ -395,12 +389,12 @@ def validate_build(self):
395389
if ':' not in build['image']:
396390
# Prepend proper image name to user's image name
397391
build['image'] = '{}:{}'.format(
398-
DOCKER_DEFAULT_IMAGE,
392+
settings.DOCKER_DEFAULT_IMAGE,
399393
build['image'],
400394
)
401395
# Update docker default settings from image name
402-
if build['image'] in DOCKER_IMAGE_SETTINGS:
403-
self.env_config.update(DOCKER_IMAGE_SETTINGS[build['image']])
396+
if build['image'] in settings.DOCKER_IMAGE_SETTINGS:
397+
self.env_config.update(settings.DOCKER_IMAGE_SETTINGS[build['image']])
404398

405399
# Allow to override specific project
406400
config_image = self.defaults.get('build_image')
@@ -702,7 +696,7 @@ def validate_build(self):
702696
with self.catch_validation_error('build.image'):
703697
image = self.pop_config('build.image', self.default_build_image)
704698
build['image'] = '{}:{}'.format(
705-
DOCKER_DEFAULT_IMAGE,
699+
settings.DOCKER_DEFAULT_IMAGE,
706700
validate_choice(
707701
image,
708702
self.valid_build_images,

readthedocs/core/middleware.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
log = logging.getLogger(__name__)
1616

1717
LOG_TEMPLATE = '(Middleware) %(msg)s [%(host)s%(path)s]'
18-
SUBDOMAIN_URLCONF = settings.SUBDOMAIN_URLCONF
19-
SINGLE_VERSION_URLCONF = settings.SINGLE_VERSION_URLCONF
2018

2119

2220
class SubdomainMiddleware(MiddlewareMixin):
@@ -38,10 +36,9 @@ def process_request(self, request):
3836
path = request.get_full_path()
3937
log_kwargs = dict(host=host, path=path)
4038
public_domain = settings.PUBLIC_DOMAIN
41-
production_domain = settings.PRODUCTION_DOMAIN
4239

4340
if public_domain is None:
44-
public_domain = production_domain
41+
public_domain = settings.PRODUCTION_DOMAIN
4542
if ':' in host:
4643
host = host.split(':')[0]
4744
domain_parts = host.split('.')
@@ -57,21 +54,23 @@ def process_request(self, request):
5754
raise Http404(_('Project not found'))
5855
request.subdomain = True
5956
request.slug = subdomain
60-
request.urlconf = SUBDOMAIN_URLCONF
57+
request.urlconf = settings.SUBDOMAIN_URLCONF
6158
return None
6259

6360
# Serve CNAMEs
6461
if (
65-
public_domain not in host and production_domain not in host and
66-
'localhost' not in host and 'testserver' not in host
62+
public_domain not in host and
63+
settings.PRODUCTION_DOMAIN not in host and
64+
'localhost' not in host and
65+
'testserver' not in host
6766
):
6867
request.cname = True
6968
domains = Domain.objects.filter(domain=host)
7069
if domains.count():
7170
for domain in domains:
7271
if domain.domain == host:
7372
request.slug = domain.project.slug
74-
request.urlconf = SUBDOMAIN_URLCONF
73+
request.urlconf = settings.SUBDOMAIN_URLCONF
7574
request.domain_object = True
7675
log.debug(
7776
LOG_TEMPLATE,
@@ -86,7 +85,7 @@ def process_request(self, request):
8685
'HTTP_X_RTD_SLUG' in request.META
8786
):
8887
request.slug = request.META['HTTP_X_RTD_SLUG'].lower()
89-
request.urlconf = SUBDOMAIN_URLCONF
88+
request.urlconf = settings.SUBDOMAIN_URLCONF
9089
request.rtdheader = True
9190
log.debug(
9291
LOG_TEMPLATE,
@@ -171,7 +170,7 @@ def process_request(self, request):
171170
return None
172171

173172
if getattr(proj, 'single_version', False):
174-
request.urlconf = SINGLE_VERSION_URLCONF
173+
request.urlconf = settings.SINGLE_VERSION_URLCONF
175174
# Logging
176175
host = request.get_host()
177176
path = request.get_full_path()

readthedocs/core/resolver.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,15 @@ def resolve(
180180
else:
181181
domain = settings.PRODUCTION_DOMAIN
182182

183-
public_domain = settings.PUBLIC_DOMAIN
184-
use_https = settings.PUBLIC_DOMAIN_USES_HTTPS
185-
186183
use_https_protocol = any([
187184
# Rely on the ``Domain.https`` field
188185
use_custom_domain and custom_domain.https,
189186
# or force it if specified
190187
require_https,
191188
# or fallback to settings
192-
use_https and public_domain and public_domain in domain,
189+
settings.PUBLIC_DOMAIN_USES_HTTPS and
190+
settings.PUBLIC_DOMAIN and
191+
settings.PUBLIC_DOMAIN in domain,
193192
])
194193
protocol = 'https' if use_https_protocol else 'http'
195194

@@ -229,11 +228,10 @@ def _get_canonical_project(self, project, projects=None):
229228

230229
def _get_project_subdomain(self, project):
231230
"""Determine canonical project domain as subdomain."""
232-
public_domain = settings.PUBLIC_DOMAIN
233231
if self._use_subdomain():
234232
project = self._get_canonical_project(project)
235233
subdomain_slug = project.slug.replace('_', '-')
236-
return '{}.{}'.format(subdomain_slug, public_domain)
234+
return '{}.{}'.format(subdomain_slug, settings.PUBLIC_DOMAIN)
237235

238236
def _get_private(self, project, version_slug):
239237
from readthedocs.builds.models import Version
@@ -266,9 +264,7 @@ def _use_custom_domain(self, custom_domain):
266264

267265
def _use_subdomain(self):
268266
"""Make decision about whether to use a subdomain to serve docs."""
269-
use_subdomain = settings.USE_SUBDOMAIN
270-
public_domain = settings.PUBLIC_DOMAIN
271-
return use_subdomain and public_domain is not None
267+
return settings.USE_SUBDOMAIN and settings.PUBLIC_DOMAIN is not None
272268

273269

274270
class Resolver(SettingsOverrideObject):

readthedocs/core/utils/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
log = logging.getLogger(__name__)
2222

23-
SYNC_USER = settings.SYNC_USER
24-
2523

2624
def broadcast(type, task, args, kwargs=None, callback=None): # pylint: disable=redefined-builtin
2725
"""

readthedocs/core/views/serve.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,15 @@ def _serve_symlink_docs(request, project, privacy_level, filename=''):
242242

243243
files_tried = []
244244

245-
serve_docs = settings.SERVE_DOCS
246-
247-
if (settings.DEBUG or constants.PUBLIC in serve_docs) and privacy_level != constants.PRIVATE: # yapf: disable # noqa
245+
if (settings.DEBUG or constants.PUBLIC in settings.SERVE_DOCS) and privacy_level != constants.PRIVATE: # yapf: disable # noqa
248246
public_symlink = PublicSymlink(project)
249247
basepath = public_symlink.project_root
250248
if os.path.exists(os.path.join(basepath, filename)):
251249
return _serve_file(request, filename, basepath)
252250

253251
files_tried.append(os.path.join(basepath, filename))
254252

255-
if (settings.DEBUG or constants.PRIVATE in serve_docs) and privacy_level == constants.PRIVATE: # yapf: disable # noqa
253+
if (settings.DEBUG or constants.PRIVATE in settings.SERVE_DOCS) and privacy_level == constants.PRIVATE: # yapf: disable # noqa
256254
# Handle private
257255
private_symlink = PrivateSymlink(project)
258256
basepath = private_symlink.project_root

readthedocs/notifications/backends.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def send_notification(request, notification):
2626
should be a list of class paths to be loaded, using the standard Django
2727
string module loader.
2828
"""
29-
backends = settings.NOTIFICATION_BACKENDS
30-
for cls_name in backends:
29+
for cls_name in settings.NOTIFICATION_BACKENDS:
3130
backend = import_string(cls_name)(request)
3231
backend.send(notification)
3332

0 commit comments

Comments
 (0)