Sourcery refactored develop branch#1
Conversation
| @@ -29,6 +29,7 @@ | |||
| Stevens, W. Richard. I{Unix Network Programming} (Addison-Wesley, 1990). | |||
| """ | |||
|
|
|||
There was a problem hiding this comment.
Lines 63-68 refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| for fd in range(0, maxfd): | ||
| for fd in range(maxfd): |
There was a problem hiding this comment.
Function _redirectFileDescriptors refactored with the following changes:
- Replace range(0, x) with range(x) (
remove-zero-from-range)
| if instance.pk: | ||
| if not instance.pk: | ||
| return | ||
| # Need a primary key to do m2m stuff | ||
| for m2m in sender._meta.local_many_to_many: | ||
| for m2m in sender._meta.local_many_to_many: | ||
| # This gets us all m2m fields, so limit it to just those that are denormed | ||
| if hasattr(m2m, 'denorm'): | ||
| # Does some extra jiggery-pokery for "through" m2m models. | ||
| # May not work under lots of conditions. | ||
| try: | ||
| remote = m2m.remote_field # Django>=1.10 | ||
| except AttributeError: | ||
| remote = m2m.rel | ||
| if hasattr(remote, 'through_model'): | ||
| # Clear exisiting through records (bit heavy handed?) | ||
| kwargs = {m2m.related.var_name: instance} | ||
|
|
||
| # Can't use m2m_column_name in a filter | ||
| # kwargs = { m2m.m2m_column_name(): instance.pk, } | ||
| remote.through_model.objects.filter(**kwargs).delete() | ||
|
|
||
| values = m2m.denorm.func(instance) | ||
| for value in values: | ||
| kwargs.update({m2m.m2m_reverse_name(): value.pk}) | ||
| remote.through_model.objects.create(**kwargs) | ||
| if hasattr(m2m, 'denorm'): | ||
| # Does some extra jiggery-pokery for "through" m2m models. | ||
| # May not work under lots of conditions. | ||
| try: | ||
| remote = m2m.remote_field # Django>=1.10 | ||
| except AttributeError: | ||
| remote = m2m.rel | ||
| if hasattr(remote, 'through_model'): | ||
| # Clear exisiting through records (bit heavy handed?) | ||
| kwargs = {m2m.related.var_name: instance} | ||
|
|
||
| # Can't use m2m_column_name in a filter | ||
| # kwargs = { m2m.m2m_column_name(): instance.pk, } | ||
| remote.through_model.objects.filter(**kwargs).delete() | ||
|
|
||
| values = m2m.denorm.func(instance) | ||
| for value in values: | ||
| kwargs[m2m.m2m_reverse_name()] = value.pk | ||
| remote.through_model.objects.create(**kwargs) | ||
|
|
||
| else: | ||
| values = m2m.denorm.func(instance) | ||
| setattr(instance, m2m.attname, values) | ||
| else: | ||
| values = m2m.denorm.func(instance) | ||
| setattr(instance, m2m.attname, values) |
There was a problem hiding this comment.
Function many_to_many_pre_save refactored with the following changes:
- Add guard clause (
last-if-guard) - Add single value to dictionary directly rather than using update() (
simplify-dictionary-update)
| for m2m in sender._meta.local_many_to_many: | ||
| if hasattr(m2m, 'denorm'): | ||
| return True | ||
| return False | ||
| return any(hasattr(m2m, 'denorm') for m2m in sender._meta.local_many_to_many) |
There was a problem hiding this comment.
Function many_to_many_post_save.check_resave refactored with the following changes:
- Use any() instead of for loop (
use-any)
| if hasattr(field, 'denorm'): | ||
| if not field.denorm.model._meta.swapped: | ||
| alldenorms.append(field.denorm) | ||
| if ( | ||
| hasattr(field, 'denorm') | ||
| and not field.denorm.model._meta.swapped | ||
| ): | ||
| alldenorms.append(field.denorm) |
There was a problem hiding this comment.
Function get_alldenorms refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| dbfield = CachedField(func, cache, *args, **kwargs) | ||
| return dbfield | ||
| return CachedField(func, cache, *args, **kwargs) |
There was a problem hiding this comment.
Function cached.deco refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| else: | ||
| self.connection = connection | ||
|
|
||
| self.connection = connections[self.using] if self.using else connection |
There was a problem hiding this comment.
Function Trigger.__init__ refactored with the following changes:
- Swap if/else to remove empty if body (
remove-pass-body) - Merge nested if conditions (
merge-nested-ifs) - Replace if statement with if expression (
assign-if-exp)
| if self.using: | ||
| self.connection = connections[self.using] | ||
| else: | ||
| self.connection = connection | ||
| self.connection = connections[self.using] if self.using else connection |
There was a problem hiding this comment.
Function TriggerSet.__init__ refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| params = [] | ||
| if isinstance(self.values, TriggerNestedSelect): | ||
| sql, nested_params = self.values.sql() | ||
| values = "(" + sql + ")" | ||
| params = [] |
There was a problem hiding this comment.
Function TriggerActionInsert.sql refactored with the following changes:
- Move assignments closer to their usage (
move-assign)
| # FIXME, set and de-set middleware values | ||
| f1 = models.Forum.objects.create(title="forumone") | ||
| m1 = models.Member.objects.create(first_name="first1", name="last1") | ||
| p1 = models.Post.objects.create(forum=f1, author=m1) | ||
|
|
||
| self.assertEqual(models.Post.objects.get(id=p1.id).author_name, "last1") | ||
|
|
||
| self.client.login(username="testuser", password="testuser") | ||
| self.client.post("/admin/denorm_testapp/member/%s/" % (m1.pk), { | ||
| 'name': 'last2', | ||
| 'first_name': 'first2', | ||
| }) | ||
|
|
||
| self.assertEqual(models.Post.objects.get(id=p1.id).author_name, "last2") |
There was a problem hiding this comment.
Function TestDenormalisation.test_middleware refactored with the following changes:
- Remove unreachable code (
remove-unreachable-code)
This removes the following comments ( why? ):
# FIXME, set and de-set middleware values
Branch
developrefactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
developbranch, then run:Help us improve this pull request!