Fixed #28795 -- Removed 'not in' checks and used dict.setdefault().

This commit is contained in:
Дилян Палаузов 2017-11-13 16:15:49 -05:00 committed by Tim Graham
parent a2ec1e6b2d
commit 23bf4ad87f
13 changed files with 27 additions and 60 deletions

View File

@ -8,5 +8,4 @@ class GISConfig(AppConfig):
verbose_name = _("GIS")
def ready(self):
if 'geojson' not in serializers.BUILTIN_SERIALIZERS:
serializers.BUILTIN_SERIALIZERS['geojson'] = "django.contrib.gis.serializers.geojson"
serializers.BUILTIN_SERIALIZERS.setdefault('geojson', 'django.contrib.gis.serializers.geojson')

View File

@ -255,9 +255,8 @@ class GeometryField(BaseSpatialField):
'srid': self.srid,
}
defaults.update(kwargs)
if (self.dim > 2 and 'widget' not in kwargs and
not getattr(defaults['form_class'].widget, 'supports_3d', False)):
defaults['widget'] = forms.Textarea
if self.dim > 2 and not getattr(defaults['form_class'].widget, 'supports_3d', False):
defaults.setdefault('widget', forms.Textarea)
return super().formfield(**defaults)
def select_format(self, compiler, sql, params):

View File

@ -381,27 +381,14 @@ class GDALRaster(GDALRasterBase):
consult the GDAL_RESAMPLE_ALGORITHMS constant.
"""
# Get the parameters defining the geotransform, srid, and size of the raster
if 'width' not in ds_input:
ds_input['width'] = self.width
if 'height' not in ds_input:
ds_input['height'] = self.height
if 'srid' not in ds_input:
ds_input['srid'] = self.srs.srid
if 'origin' not in ds_input:
ds_input['origin'] = self.origin
if 'scale' not in ds_input:
ds_input['scale'] = self.scale
if 'skew' not in ds_input:
ds_input['skew'] = self.skew
ds_input.setdefault('width', self.width)
ds_input.setdefault('height', self.height)
ds_input.setdefault('srid', self.srs.srid)
ds_input.setdefault('origin', self.origin)
ds_input.setdefault('scale', self.scale)
ds_input.setdefault('skew', self.skew)
# Get the driver, name, and datatype of the target raster
if 'driver' not in ds_input:
ds_input['driver'] = self.driver.name
ds_input.setdefault('driver', self.driver.name)
if 'name' not in ds_input:
ds_input['name'] = self.name + '_copy.' + self.driver.name

View File

@ -693,8 +693,8 @@ class AlterModelOptions(ModelOptionOperation):
model_state.options = dict(model_state.options)
model_state.options.update(self.options)
for key in self.ALTER_OPTION_KEYS:
if key not in self.options and key in model_state.options:
del model_state.options[key]
if key not in self.options:
model_state.options.pop(key, False)
state.reload_model(app_label, self.name_lower, delay=True)
def database_forwards(self, app_label, schema_editor, from_state, to_state):

View File

@ -692,8 +692,7 @@ class Query:
# in the parent list. Again, it must be mentioned to ensure that
# only "must include" fields are pulled in.
for model in orig_opts.get_parent_list():
if model not in seen:
seen[model] = set()
seen.setdefault(model, set())
for model, values in seen.items():
callback(target, model, values)

View File

@ -87,20 +87,10 @@ class BoundField:
attrs = attrs or {}
attrs = self.build_widget_attrs(attrs, widget)
auto_id = self.auto_id
if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
if not only_initial:
attrs['id'] = auto_id
else:
attrs['id'] = self.html_initial_id
if not only_initial:
name = self.html_name
else:
name = self.html_initial_name
if self.auto_id and 'id' not in widget.attrs:
attrs.setdefault('id', self.html_initial_id if only_initial else self.auto_id)
return widget.render(
name=name,
name=self.html_initial_name if only_initial else self.html_name,
value=self.value(),
attrs=attrs,
renderer=self.form.renderer,

View File

@ -136,10 +136,7 @@ class HttpResponseBase:
self._headers[header.lower()] = (header, value)
def __delitem__(self, header):
try:
del self._headers[header.lower()]
except KeyError:
pass
self._headers.pop(header.lower(), False)
def __getitem__(self, header):
return self._headers[header.lower()][1]

View File

@ -57,6 +57,5 @@ class LocaleMiddleware(MiddlewareMixin):
if not (i18n_patterns_used and language_from_path):
patch_vary_headers(response, ('Accept-Language',))
if 'Content-Language' not in response:
response['Content-Language'] = language
response.setdefault('Content-Language', language)
return response

View File

@ -37,10 +37,10 @@ class SecurityMiddleware(MiddlewareMixin):
sts_header = sts_header + "; preload"
response["strict-transport-security"] = sts_header
if self.content_type_nosniff and 'x-content-type-options' not in response:
response["x-content-type-options"] = "nosniff"
if self.content_type_nosniff:
response.setdefault('x-content-type-options', 'nosniff')
if self.xss_filter and 'x-xss-protection' not in response:
response["x-xss-protection"] = "1; mode=block"
if self.xss_filter:
response.setdefault('x-xss-protection', '1; mode=block')
return response

View File

@ -225,8 +225,7 @@ class IfChangedNode(Node):
def render(self, context):
# Init state storage
state_frame = self._get_context_stack_frame(context)
if self not in state_frame:
state_frame[self] = None
state_frame.setdefault(self)
nodelist_true_output = None
if self._varlist:

View File

@ -103,8 +103,8 @@ def condition(etag_func=None, last_modified_func=None):
if request.method in ('GET', 'HEAD'):
if res_last_modified and not response.has_header('Last-Modified'):
response['Last-Modified'] = http_date(res_last_modified)
if res_etag and not response.has_header('ETag'):
response['ETag'] = res_etag
if res_etag:
response.setdefault('ETag', res_etag)
return response

View File

@ -21,8 +21,7 @@ class ContextMixin:
extra_context = None
def get_context_data(self, **kwargs):
if 'view' not in kwargs:
kwargs['view'] = self
kwargs.setdefault('view', self)
if self.extra_context is not None:
kwargs.update(self.extra_context)
return kwargs

View File

@ -455,8 +455,7 @@ if __name__ == "__main__":
if options.settings:
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
else:
if "DJANGO_SETTINGS_MODULE" not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_sqlite'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_sqlite')
options.settings = os.environ['DJANGO_SETTINGS_MODULE']
if options.selenium: