Skip to content

Commit

Permalink
Fixed #28984 -- Made assorted code simplifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jan 3, 2018
1 parent d79cf1e commit acc8dd4
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 61 deletions.
19 changes: 9 additions & 10 deletions django/contrib/admin/helpers.py
Expand Up @@ -203,8 +203,7 @@ def contents(self):
result_repr = self.empty_value_display
else:
if f is None:
boolean = getattr(attr, "boolean", False)
if boolean:
if getattr(attr, 'boolean', False):
result_repr = _boolean_icon(value)
else:
if hasattr(value, "__html__"):
Expand Down Expand Up @@ -331,14 +330,14 @@ def __iter__(self):
)

def needs_explicit_pk_field(self):
# Auto fields are editable (oddly), so need to check for auto or non-editable pk
if self.form._meta.model._meta.auto_field or not self.form._meta.model._meta.pk.editable:
return True
# Also search any parents for an auto field. (The pk info is propagated to child
# models so that does not need to be checked in parents.)
return any(
parent._meta.auto_field or not parent._meta.model._meta.pk.editable
for parent in self.form._meta.model._meta.get_parent_list()
return (
# Auto fields are editable, so check for auto or non-editable pk.
self.form._meta.model._meta.auto_field or not self.form._meta.model._meta.pk.editable or
# Also search any parents for an auto field. (The pk info is
# propagated to child models so that does not need to be checked
# in parents.)
any(parent._meta.auto_field or not parent._meta.model._meta.pk.editable
for parent in self.form._meta.model._meta.get_parent_list())
)

def pk_field(self):
Expand Down
4 changes: 1 addition & 3 deletions django/contrib/admin/options.py
Expand Up @@ -841,13 +841,11 @@ def get_actions(self, request):
actions = filter(None, actions)

# Convert the actions into an OrderedDict keyed by name.
actions = OrderedDict(
return OrderedDict(
(name, (func, name, desc))
for func, name, desc in actions
)

return actions

def get_action_choices(self, request, default_choices=BLANK_CHOICE_DASH):
"""
Return a list of choices for use in a form object. Each choice is a
Expand Down
3 changes: 1 addition & 2 deletions django/contrib/sessions/backends/base.py
Expand Up @@ -151,8 +151,7 @@ def _get_new_session_key(self):
while True:
session_key = get_random_string(32, VALID_KEY_CHARS)
if not self.exists(session_key):
break
return session_key
return session_key

def _get_or_create_session_key(self):
if self._session_key is None:
Expand Down
6 changes: 1 addition & 5 deletions django/core/serializers/__init__.py
Expand Up @@ -216,11 +216,7 @@ def sort_dependencies(app_list):
# If all of the models in the dependency list are either already
# on the final model list, or not on the original serialization list,
# then we've found another model with all it's dependencies satisfied.
found = True
for candidate in ((d not in models or d in model_list) for d in deps):
if not candidate:
found = False
if found:
if all(candidate for candidate in ((d not in models or d in model_list) for d in deps)):
model_list.append(model)
changed = True
else:
Expand Down
2 changes: 1 addition & 1 deletion django/db/migrations/autodetector.py
Expand Up @@ -301,7 +301,7 @@ def _build_migration_list(self, graph=None):
if deps_satisfied:
chopped.append(operation)
dependencies.update(operation_dependencies)
self.generated_operations[app_label] = self.generated_operations[app_label][1:]
del self.generated_operations[app_label][0]
else:
break
# Make a migration! Well, only if there's stuff to put in it
Expand Down
4 changes: 2 additions & 2 deletions django/db/migrations/graph.py
Expand Up @@ -304,7 +304,7 @@ def root_nodes(self, app=None):
"""
roots = set()
for node in self.nodes:
if not any(key[0] == node[0] for key in self.node_map[node].parents) and (not app or app == node[0]):
if all(key[0] != node[0] for key in self.node_map[node].parents) and (not app or app == node[0]):
roots.add(node)
return sorted(roots)

Expand All @@ -318,7 +318,7 @@ def leaf_nodes(self, app=None):
"""
leaves = set()
for node in self.nodes:
if not any(key[0] == node[0] for key in self.node_map[node].children) and (not app or app == node[0]):
if all(key[0] != node[0] for key in self.node_map[node].children) and (not app or app == node[0]):
leaves.add(node)
return sorted(leaves)

Expand Down
2 changes: 1 addition & 1 deletion django/db/models/fields/files.py
Expand Up @@ -39,7 +39,7 @@ def _require_file(self):

def _get_file(self):
self._require_file()
if not hasattr(self, '_file') or self._file is None:
if getattr(self, '_file', None) is None:
self._file = self.storage.open(self.name, 'rb')
return self._file

Expand Down
10 changes: 4 additions & 6 deletions django/dispatch/dispatcher.py
Expand Up @@ -215,12 +215,10 @@ def _clear_dead_receivers(self):
# Note: caller is assumed to hold self.lock.
if self._dead_receivers:
self._dead_receivers = False
new_receivers = []
for r in self.receivers:
if isinstance(r[1], weakref.ReferenceType) and r[1]() is None:
continue
new_receivers.append(r)
self.receivers = new_receivers
self.receivers = [
r for r in self.receivers
if not(isinstance(r[1], weakref.ReferenceType) and r[1]() is None)
]

def _live_receivers(self, sender):
"""
Expand Down
3 changes: 1 addition & 2 deletions django/test/runner.py
Expand Up @@ -531,8 +531,7 @@ def build_suite(self, test_labels=None, extra_tests=None, **kwargs):
# Since tests are distributed across processes on a per-TestCase
# basis, there's no need for more processes than TestCases.
parallel_units = len(parallel_suite.subsuites)
if self.parallel > parallel_units:
self.parallel = parallel_units
self.parallel = min(self.parallel, parallel_units)

# If there's only one TestCase, parallelization isn't needed.
if self.parallel > 1:
Expand Down
2 changes: 1 addition & 1 deletion django/utils/synch.py
Expand Up @@ -60,7 +60,7 @@ def reader(self):
def writer_enters(self):
with self.mutex:
if self.active_writers == 0 and self.waiting_writers == 0 and self.active_readers == 0:
self.active_writers += 1
self.active_writers = 1
self.can_write.release()
else:
self.waiting_writers += 1
Expand Down
8 changes: 2 additions & 6 deletions tests/auth_tests/test_context_processors.py
Expand Up @@ -10,14 +10,10 @@

class MockUser:
def has_module_perms(self, perm):
if perm == 'mockapp':
return True
return False
return perm == 'mockapp'

def has_perm(self, perm):
if perm == 'mockapp.someperm':
return True
return False
return perm == 'mockapp.someperm'


class PermWrapperTests(SimpleTestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/inspectdb/tests.py
Expand Up @@ -189,7 +189,7 @@ def test_special_column_name_introspection(self):
table_name_filter=lambda tn: tn.startswith('inspectdb_special'),
stdout=out)
output = out.getvalue()
base_name = 'Field' if not connection.features.uppercases_column_names else 'field'
base_name = 'field' if connection.features.uppercases_column_names else 'Field'
self.assertIn("field = models.IntegerField()", output)
self.assertIn("field_field = models.IntegerField(db_column='%s_')" % base_name, output)
self.assertIn("field_field_0 = models.IntegerField(db_column='%s__')" % base_name, output)
Expand Down
16 changes: 4 additions & 12 deletions tests/modeladmin/tests.py
Expand Up @@ -671,27 +671,19 @@ class ModelAdminPermissionTests(SimpleTestCase):

class MockUser:
def has_module_perms(self, app_label):
if app_label == "modeladmin":
return True
return False
return app_label == 'modeladmin'

class MockAddUser(MockUser):
def has_perm(self, perm):
if perm == "modeladmin.add_band":
return True
return False
return perm == 'modeladmin.add_band'

class MockChangeUser(MockUser):
def has_perm(self, perm):
if perm == "modeladmin.change_band":
return True
return False
return perm == 'modeladmin.change_band'

class MockDeleteUser(MockUser):
def has_perm(self, perm):
if perm == "modeladmin.delete_band":
return True
return False
return perm == 'modeladmin.delete_band'

def test_has_add_permission(self):
"""
Expand Down
11 changes: 2 additions & 9 deletions tests/serializers/test_json.py
Expand Up @@ -55,20 +55,13 @@ def _validate_output(serial_str):

@staticmethod
def _get_pk_values(serial_str):
ret_list = []
serial_list = json.loads(serial_str)
for obj_dict in serial_list:
ret_list.append(obj_dict["pk"])
return ret_list
return [obj_dict['pk'] for obj_dict in serial_list]

@staticmethod
def _get_field_values(serial_str, field_name):
ret_list = []
serial_list = json.loads(serial_str)
for obj_dict in serial_list:
if field_name in obj_dict["fields"]:
ret_list.append(obj_dict["fields"][field_name])
return ret_list
return [obj_dict['fields'][field_name] for obj_dict in serial_list if field_name in obj_dict['fields']]

def test_indentation_whitespace(self):
s = serializers.json.Serializer()
Expand Down

0 comments on commit acc8dd4

Please sign in to comment.