Fixed #28985 -- Removed unneeded None checks before hasattr().

This commit is contained in:
Дилян Палаузов 2018-01-03 11:34:10 -05:00 committed by Tim Graham
parent b3cd9fb18b
commit d79cf1e9e2
8 changed files with 14 additions and 17 deletions

View File

@ -281,7 +281,7 @@ def lookup_field(name, obj, model_admin=None):
if callable(name): if callable(name):
attr = name attr = name
value = attr(obj) value = attr(obj)
elif model_admin is not None and hasattr(model_admin, name) and name != '__str__': elif hasattr(model_admin, name) and name != '__str__':
attr = getattr(model_admin, name) attr = getattr(model_admin, name)
value = attr(obj) value = attr(obj)
else: else:
@ -341,7 +341,7 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
else: else:
if callable(name): if callable(name):
attr = name attr = name
elif model_admin is not None and hasattr(model_admin, name): elif hasattr(model_admin, name):
attr = getattr(model_admin, name) attr = getattr(model_admin, name)
elif hasattr(model, name): elif hasattr(model, name):
attr = getattr(model, name) attr = getattr(model, name)

View File

@ -137,7 +137,7 @@ def logout(request):
# Dispatch the signal before the user is logged out so the receivers have a # Dispatch the signal before the user is logged out so the receivers have a
# chance to find out *who* logged out. # chance to find out *who* logged out.
user = getattr(request, 'user', None) user = getattr(request, 'user', None)
if hasattr(user, 'is_authenticated') and not user.is_authenticated: if not getattr(user, 'is_authenticated', True):
user = None user = None
user_logged_out.send(sender=user.__class__, request=request, user=user) user_logged_out.send(sender=user.__class__, request=request, user=user)

View File

@ -70,10 +70,10 @@ class FieldFile(File):
def open(self, mode='rb'): def open(self, mode='rb'):
self._require_file() self._require_file()
if hasattr(self, '_file') and self._file is not None: if getattr(self, '_file', None) is None:
self.file.open(mode)
else:
self.file = self.storage.open(self.name, mode) self.file = self.storage.open(self.name, mode)
else:
self.file.open(mode)
return self return self
# open() doesn't alter the file's contents, but it does reset the pointer # open() doesn't alter the file's contents, but it does reset the pointer
open.alters_data = True open.alters_data = True

View File

@ -1349,8 +1349,7 @@ class ModelMultipleChoiceField(ModelChoiceField):
def modelform_defines_fields(form_class): def modelform_defines_fields(form_class):
return (form_class is not None and ( return hasattr(form_class, '_meta') and (
hasattr(form_class, '_meta') and form_class._meta.fields is not None or
(form_class._meta.fields is not None or form_class._meta.exclude is not None
form_class._meta.exclude is not None) )
))

View File

@ -52,9 +52,7 @@ class Element:
child.finalize() child.finalize()
def __eq__(self, element): def __eq__(self, element):
if not hasattr(element, 'name'): if not hasattr(element, 'name') or self.name != element.name:
return False
if hasattr(element, 'name') and self.name != element.name:
return False return False
if len(self.attributes) != len(element.attributes): if len(self.attributes) != len(element.attributes):
return False return False

View File

@ -346,7 +346,7 @@ class ExceptionReporter:
Return (pre_context_lineno, pre_context, context_line, post_context). Return (pre_context_lineno, pre_context, context_line, post_context).
""" """
source = None source = None
if loader is not None and hasattr(loader, "get_source"): if hasattr(loader, 'get_source'):
try: try:
source = loader.get_source(module_name) source = loader.get_source(module_name)
except ImportError: except ImportError:

View File

@ -145,7 +145,7 @@ class SingleObjectTemplateResponseMixin(TemplateResponseMixin):
object_meta.model_name, object_meta.model_name,
self.template_name_suffix self.template_name_suffix
)) ))
elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model): elif getattr(self, 'model', None) is not None and issubclass(self.model, models.Model):
names.append("%s/%s%s.html" % ( names.append("%s/%s%s.html" % (
self.model._meta.app_label, self.model._meta.app_label,
self.model._meta.model_name, self.model._meta.model_name,

View File

@ -83,7 +83,7 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
if self.model is not None: if self.model is not None:
# If a model has been explicitly provided, use it # If a model has been explicitly provided, use it
model = self.model model = self.model
elif hasattr(self, 'object') and self.object is not None: elif getattr(self, 'object', None) is not None:
# If this view is operating on a single object, use # If this view is operating on a single object, use
# the class of that object # the class of that object
model = self.object.__class__ model = self.object.__class__