32
32
from .fixtures import settings # noqa
33
33
from .fixtures import transactional_db # noqa
34
34
35
- from .lazy_django import django_settings_is_configured , skip_if_no_django
35
+ from .lazy_django import (django_settings_is_configured ,
36
+ get_django_version , skip_if_no_django )
36
37
37
38
38
39
SETTINGS_MODULE_ENV = 'DJANGO_SETTINGS_MODULE'
@@ -140,7 +141,7 @@ def _setup_django():
140
141
141
142
import django .conf
142
143
143
- # Avoid trying to force-load Django when settings is not properly configured
144
+ # Avoid force-loading Django when settings are not properly configured.
144
145
if not django .conf .settings .configured :
145
146
return
146
147
@@ -203,7 +204,6 @@ def pytest_load_initial_conftests(early_config, parser, args):
203
204
os .environ [INVALID_TEMPLATE_VARS_ENV ] = 'true'
204
205
205
206
# Configure DJANGO_SETTINGS_MODULE
206
-
207
207
if options .ds :
208
208
ds_source = 'command line option'
209
209
ds = options .ds
@@ -218,7 +218,8 @@ def pytest_load_initial_conftests(early_config, parser, args):
218
218
ds_source = None
219
219
220
220
if ds :
221
- early_config ._dsm_report_header = 'django settings: %s (from %s)' % (ds , ds_source )
221
+ early_config ._dsm_report_header = 'Django settings: %s (from %s)' % (
222
+ ds , ds_source )
222
223
else :
223
224
early_config ._dsm_report_header = None
224
225
@@ -237,12 +238,12 @@ def pytest_load_initial_conftests(early_config, parser, args):
237
238
import configurations .importer
238
239
configurations .importer .install ()
239
240
240
- # Forcefully load django settings, throws ImportError or
241
+ # Forcefully load Django settings, throws ImportError or
241
242
# ImproperlyConfigured if settings cannot be loaded.
242
- from django .conf import settings # noqa
243
+ from django .conf import settings as dj_settings
243
244
244
245
with _handle_import_error (_django_project_scan_outcome ):
245
- settings .DATABASES
246
+ dj_settings .DATABASES
246
247
247
248
_setup_django ()
248
249
@@ -266,7 +267,8 @@ def _method_is_defined_at_leaf(cls, method_name):
266
267
if hasattr (base_cls , method_name ):
267
268
super_method = getattr (base_cls , method_name )
268
269
269
- assert super_method is not None , '%s could not be found in base class' % method_name
270
+ assert super_method is not None , (
271
+ '%s could not be found in base class' % method_name )
270
272
271
273
return getattr (cls , method_name ).__func__ is not super_method .__func__
272
274
@@ -332,9 +334,10 @@ def django_test_environment(request):
332
334
"""
333
335
if django_settings_is_configured ():
334
336
_setup_django ()
335
- from django .conf import settings # noqa
336
- from django .test .utils import setup_test_environment , teardown_test_environment
337
- settings .DEBUG = False
337
+ from django .conf import settings as dj_settings
338
+ from django .test .utils import (setup_test_environment ,
339
+ teardown_test_environment )
340
+ dj_settings .DEBUG = False
338
341
setup_test_environment ()
339
342
request .addfinalizer (teardown_test_environment )
340
343
@@ -348,10 +351,11 @@ def django_db_blocker():
348
351
special database handling.
349
352
350
353
The object is a context manager and provides the methods
351
- .enable_database_access()/.disable_database_access() and .restore_database_access() to
352
- temporarily enable database access.
354
+ .enable_database_access()/.disable_database_access() and
355
+ .restore_database_access() to temporarily enable database access.
353
356
354
- This is an advanced feature that is meant to be used to implement database fixtures.
357
+ This is an advanced feature that is meant to be used to implement database
358
+ fixtures.
355
359
"""
356
360
if not django_settings_is_configured ():
357
361
return None
@@ -480,22 +484,24 @@ def __mod__(self, var):
480
484
"""Handle TEMPLATE_STRING_IF_INVALID % var."""
481
485
template = self ._get_template ()
482
486
if template :
483
- msg = "Undefined template variable '%s' in '%s'" % (var , template .name )
487
+ msg = "Undefined template variable '%s' in '%s'" % (
488
+ var , template .name )
484
489
else :
485
490
msg = "Undefined template variable '%s'" % var
486
491
if self .fail :
487
492
pytest .fail (msg , pytrace = False )
488
493
else :
489
494
return msg
490
- if os .environ .get (INVALID_TEMPLATE_VARS_ENV , 'false' ) == 'true' :
491
- if django_settings_is_configured ():
492
- import django
493
- from django .conf import settings # noqa
494
495
495
- if django .VERSION >= (1 , 8 ) and settings .TEMPLATES :
496
- settings .TEMPLATES [0 ]['OPTIONS' ]['string_if_invalid' ] = InvalidVarException ()
497
- else :
498
- settings .TEMPLATE_STRING_IF_INVALID = InvalidVarException ()
496
+ if (os .environ .get (INVALID_TEMPLATE_VARS_ENV , 'false' ) == 'true' and
497
+ django_settings_is_configured ()):
498
+ from django .conf import settings as dj_settings
499
+
500
+ if get_django_version () >= (1 , 8 ) and dj_settings .TEMPLATES :
501
+ dj_settings .TEMPLATES [0 ]['OPTIONS' ]['string_if_invalid' ] = (
502
+ InvalidVarException ())
503
+ else :
504
+ dj_settings .TEMPLATE_STRING_IF_INVALID = InvalidVarException ()
499
505
500
506
501
507
@pytest .fixture (autouse = True )
@@ -505,13 +511,12 @@ def _template_string_if_invalid_marker(request):
505
511
marker = request .keywords .get ('ignore_template_errors' , None )
506
512
if os .environ .get (INVALID_TEMPLATE_VARS_ENV , 'false' ) == 'true' :
507
513
if marker and django_settings_is_configured ():
508
- import django
509
- from django .conf import settings # noqa
514
+ from django .conf import settings as dj_settings
510
515
511
- if django . VERSION >= (1 , 8 ) and settings .TEMPLATES :
512
- settings .TEMPLATES [0 ]['OPTIONS' ]['string_if_invalid' ].fail = False
516
+ if get_django_version () >= (1 , 8 ) and dj_settings .TEMPLATES :
517
+ dj_settings .TEMPLATES [0 ]['OPTIONS' ]['string_if_invalid' ].fail = False
513
518
else :
514
- settings .TEMPLATE_STRING_IF_INVALID .fail = False
519
+ dj_settings .TEMPLATE_STRING_IF_INVALID .fail = False
515
520
516
521
# ############### Helper Functions ################
517
522
@@ -535,7 +540,7 @@ def _dj_db_wrapper(self):
535
540
from django .db .backends import BaseDatabaseWrapper
536
541
537
542
# The first time the _dj_db_wrapper is accessed, we will save a
538
- # reference to the real implementation
543
+ # reference to the real implementation.
539
544
if self ._real_ensure_connection is None :
540
545
self ._real_ensure_connection = BaseDatabaseWrapper .ensure_connection
541
546
0 commit comments