-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored develop branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| Stevens, W. Richard. I{Unix Network Programming} (Addison-Wesley, 1990). | ||
| """ | ||
|
|
||
|
|
||
| __version__ = "1.0.1" | ||
| __author__ = "Brian Clapper, bmc@clapper.org" | ||
| __url__ = "http://www.clapper.org/software/python/daemon/" | ||
|
|
@@ -60,12 +61,7 @@ | |
| MAXFD = 1024 | ||
|
|
||
| # The standard I/O file descriptors are redirected to /dev/null by default. | ||
| if (hasattr(os, "devnull")): | ||
| NULL_DEVICE = os.devnull | ||
| else: | ||
| NULL_DEVICE = "/dev/null" | ||
|
|
||
|
|
||
| NULL_DEVICE = os.devnull if (hasattr(os, "devnull")) else "/dev/null" | ||
| # --------------------------------------------------------------------------- | ||
| # Logging | ||
| # --------------------------------------------------------------------------- | ||
|
|
@@ -196,7 +192,7 @@ def _redirectFileDescriptors(): | |
|
|
||
| # Close all file descriptors. | ||
|
|
||
| for fd in range(0, maxfd): | ||
| for fd in range(maxfd): | ||
|
Comment on lines
-199
to
+195
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # Only close TTYs. | ||
| try: | ||
| os.ttyname(fd) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,11 +76,7 @@ def __init__(self, subject, time, event, actions, content_type, using=None, skip | |
| self.append(actions) | ||
| self.using = using | ||
|
|
||
| if self.using: | ||
| self.connection = connections[self.using] | ||
| else: | ||
| self.connection = connection | ||
|
|
||
| self.connection = connections[self.using] if self.using else connection | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| try: | ||
| from django.contrib.contenttypes.fields import GenericRelation | ||
| except ImportError: | ||
|
|
@@ -115,11 +111,12 @@ def __init__(self, subject, time, event, actions, content_type, using=None, skip | |
| except: | ||
| from django.db.models.related import RelatedObject as ForeignObjectRel | ||
| for k, v in get_fields_with_model(subject, self.model._meta): | ||
| if isinstance(k, ForeignObjectRel): | ||
| pass | ||
| else: | ||
| if not v and k.attname not in skip: | ||
| self.fields.append((k.attname, k.db_type(connection=self.connection))) | ||
| if ( | ||
| not isinstance(k, ForeignObjectRel) | ||
| and not v | ||
| and k.attname not in skip | ||
| ): | ||
| self.fields.append((k.attname, k.db_type(connection=self.connection))) | ||
|
|
||
| else: | ||
| raise NotImplementedError | ||
|
|
@@ -149,10 +146,7 @@ class TriggerSet(object): | |
| def __init__(self, using=None): | ||
| self.using = using | ||
| self.triggers = {} | ||
| if self.using: | ||
| self.connection = connections[self.using] | ||
| else: | ||
| self.connection = connection | ||
| self.connection = connections[self.using] if self.using else connection | ||
|
Comment on lines
-152
to
+149
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def cursor(self): | ||
| return self.connection.cursor() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,10 @@ class TriggerActionInsert(base.TriggerActionInsert): | |
| def sql(self): | ||
| table = self.model._meta.db_table | ||
| columns = "(" + ", ".join(self.columns) + ")" | ||
| params = [] | ||
| if isinstance(self.values, TriggerNestedSelect): | ||
| sql, nested_params = self.values.sql() | ||
| values = "(" + sql + ")" | ||
| params = [] | ||
|
Comment on lines
-25
to
+28
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| params.extend(nested_params) | ||
| else: | ||
| values = "VALUES (" + ", ".join(self.values) + ")" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,42 +25,40 @@ def many_to_many_pre_save(sender, instance, **kwargs): | |
| """ | ||
| Updates denormalised many-to-many fields for the model | ||
| """ | ||
| 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) | ||
|
Comment on lines
-28
to
+55
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def many_to_many_post_save(sender, instance, created, **kwargs): | ||
| if created: | ||
| def check_resave(): | ||
| 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) | ||
|
Comment on lines
-60
to
+61
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| if check_resave(): | ||
| instance.save() | ||
|
|
@@ -74,9 +72,11 @@ def get_alldenorms(): | |
| for model in gmodels.get_models(include_auto_created=True): | ||
| if not model._meta.proxy: | ||
| for field in model._meta.fields: | ||
| 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) | ||
|
Comment on lines
-77
to
+79
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return alldenorms | ||
|
|
||
|
|
||
|
|
@@ -86,10 +86,7 @@ def __init__(self, skip=None): | |
| self.skip = skip | ||
|
|
||
| def get_quote_name(self, using): | ||
| if using: | ||
| cconnection = connections[using] | ||
| else: | ||
| cconnection = connection | ||
| cconnection = connections[using] if using else connection | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return cconnection.ops.quote_name | ||
|
|
||
| def setup(self, **kwargs): | ||
|
|
@@ -117,7 +114,7 @@ def update(self, instance): | |
| # for a many to many field the decorated | ||
| # function should return a list of either model instances | ||
| # or primary keys | ||
| old_pks = set([x.pk for x in attr.all()]) | ||
| old_pks = {x.pk for x in attr.all()} | ||
|
Comment on lines
-120
to
+117
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| new_pks = set([]) | ||
|
|
||
| for x in new_value: | ||
|
|
@@ -164,7 +161,7 @@ def get_triggers(self, using): | |
| Creates a list of all triggers needed to keep track of changes | ||
| to fields this denorm depends on. | ||
| """ | ||
| trigger_list = list() | ||
| trigger_list = [] | ||
|
Comment on lines
-167
to
+164
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Get the triggers of all DenormDependency instances attached | ||
| # to our callback. | ||
|
|
@@ -227,7 +224,7 @@ def get_triggers(self, using): | |
| Creates a list of all triggers needed to keep track of changes | ||
| to fields this denorm depends on. | ||
| """ | ||
| trigger_list = list() | ||
| trigger_list = [] | ||
|
Comment on lines
-230
to
+227
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Get the triggers of all DenormDependency instances attached | ||
| # to our callback. | ||
|
|
@@ -362,22 +359,39 @@ def m2m_triggers(self, content_type, fk_name, related_field, using): | |
| values=(self.get_related_decrement_value(using),), | ||
| where=(' AND '.join(related_dec_where), related_where_params), | ||
| ) | ||
| trigger_list = [ | ||
| triggers.Trigger(related_field, "after", "update", [related_increment, related_decrement], content_type, | ||
| return [ | ||
| triggers.Trigger( | ||
| related_field, | ||
| "after", | ||
| "update", | ||
| [related_increment, related_decrement], | ||
| content_type, | ||
| using, | ||
| self.skip, | ||
| ), | ||
| triggers.Trigger( | ||
| related_field, | ||
| "after", | ||
| "insert", | ||
| [related_increment], | ||
| content_type, | ||
| using, | ||
| self.skip), | ||
| triggers.Trigger(related_field, "after", "insert", [related_increment], content_type, using, self.skip), | ||
| triggers.Trigger(related_field, "after", "delete", [related_decrement], content_type, using, self.skip), | ||
| self.skip, | ||
| ), | ||
| triggers.Trigger( | ||
| related_field, | ||
| "after", | ||
| "delete", | ||
| [related_decrement], | ||
| content_type, | ||
| using, | ||
| self.skip, | ||
| ), | ||
| ] | ||
| return trigger_list | ||
|
Comment on lines
-365
to
-372
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def get_triggers(self, using): | ||
| from .db import triggers | ||
| if using: | ||
| cconnection = connections[using] | ||
| else: | ||
| cconnection = connection | ||
|
|
||
| cconnection = connections[using] if using else connection | ||
|
Comment on lines
-376
to
+394
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| qn = self.get_quote_name(using) | ||
|
|
||
| try: # Django>=1.9 | ||
|
|
@@ -559,9 +573,12 @@ def rebuildall(verbose=False, model_name=None, field_name=None): | |
| except AttributeError: # In Django 1.5 | ||
| current_model_name = denorm.model.__name__ | ||
| current_app_model = '%s.%s' % (current_app_label, current_model_name) | ||
| if model_name is None or model_name in (current_app_label, current_model_name, current_app_model): | ||
| if field_name is None or field_name == denorm.fieldname: | ||
| models.setdefault(denorm.model, []).append(denorm) | ||
| if ( | ||
| model_name is None | ||
| or model_name | ||
| in (current_app_label, current_model_name, current_app_model) | ||
| ) and (field_name is None or field_name == denorm.fieldname): | ||
| models.setdefault(denorm.model, []).append(denorm) | ||
|
Comment on lines
-562
to
+581
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| i = 0 | ||
| for model, denorms in models.items(): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,7 @@ def get_triggers(self, using): | |
| return [] | ||
|
|
||
| def get_quote_name(self, using): | ||
| if using: | ||
| cconnection = connections[using] | ||
| else: | ||
| cconnection = connection | ||
| cconnection = connections[using] if using else connection | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return cconnection.ops.quote_name | ||
|
|
||
| def setup(self, this_model): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,8 +95,7 @@ def deconstruct(self): | |
| return name, super_path, args, kwargs | ||
|
|
||
| def deco(func): | ||
| dbfield = DenormDBField(func, *args, **kwargs) | ||
| return dbfield | ||
| return DenormDBField(func, *args, **kwargs) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return deco | ||
|
|
||
|
|
||
|
|
@@ -315,7 +314,7 @@ def __set__(self, obj, value): | |
| cached = self.field.cache.get(key) | ||
| if not cached: | ||
| cached = self.field.func(obj) | ||
| self.field.cache.set(key, cached, 60 * 60 * 24 * 30) | ||
| self.field.cache.set(key, cached, 60**2 * 24 * 30) | ||
|
Comment on lines
-318
to
+317
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| obj.__dict__[self.field.name] = cached | ||
|
|
||
|
|
||
|
|
@@ -335,6 +334,5 @@ def contribute_to_class(self, cls, name, *args, **kwargs): | |
|
|
||
| def cached(cache, *args, **kwargs): | ||
| def deco(func): | ||
| dbfield = CachedField(func, cache, *args, **kwargs) | ||
| return dbfield | ||
| return CachedField(func, cache, *args, **kwargs) | ||
|
Comment on lines
-338
to
+337
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return deco | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -314,20 +314,6 @@ def test_middleware(self): | |
| # FIXME, this test currently does not work with a transactional | ||
| # database, so it's skipped for now. | ||
| return | ||
| # 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") | ||
|
Comment on lines
-317
to
-330
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| def test_countfield(self): | ||
| f1 = models.Forum.objects.create(title="forumone") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines
63-68refactored with the following changes:assign-if-exp)