Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions denorm/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Stevens, W. Richard. I{Unix Network Programming} (Addison-Wesley, 1990).
"""

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 63-68 refactored with the following changes:


__version__ = "1.0.1"
__author__ = "Brian Clapper, bmc@clapper.org"
__url__ = "http://www.clapper.org/software/python/daemon/"
Expand Down Expand Up @@ -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
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _redirectFileDescriptors refactored with the following changes:

# Only close TTYs.
try:
os.ttyname(fd)
Expand Down
22 changes: 8 additions & 14 deletions denorm/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Trigger.__init__ refactored with the following changes:

try:
from django.contrib.contenttypes.fields import GenericRelation
except ImportError:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TriggerSet.__init__ refactored with the following changes:


def cursor(self):
return self.connection.cursor()
Expand Down
2 changes: 1 addition & 1 deletion denorm/db/mysql/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TriggerActionInsert.sql refactored with the following changes:

params.extend(nested_params)
else:
values = "VALUES (" + ", ".join(self.values) + ")"
Expand Down
121 changes: 69 additions & 52 deletions denorm/denorms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function many_to_many_pre_save refactored with the following changes:



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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function many_to_many_post_save.check_resave refactored with the following changes:

  • Use any() instead of for loop (use-any)


if check_resave():
instance.save()
Expand All @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_alldenorms refactored with the following changes:

return alldenorms


Expand All @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Denorm.get_quote_name refactored with the following changes:

return cconnection.ops.quote_name

def setup(self, **kwargs):
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Denorm.update refactored with the following changes:

new_pks = set([])

for x in new_value:
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseCallbackDenorm.get_triggers refactored with the following changes:


# Get the triggers of all DenormDependency instances attached
# to our callback.
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseCacheKeyDenorm.get_triggers refactored with the following changes:


# Get the triggers of all DenormDependency instances attached
# to our callback.
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AggregateDenorm.m2m_triggers refactored with the following changes:


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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AggregateDenorm.get_triggers refactored with the following changes:

qn = self.get_quote_name(using)

try: # Django>=1.9
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function rebuildall refactored with the following changes:


i = 0
for model, denorms in models.items():
Expand Down
5 changes: 1 addition & 4 deletions denorm/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DenormDependency.get_quote_name refactored with the following changes:

return cconnection.ops.quote_name

def setup(self, this_model):
Expand Down
8 changes: 3 additions & 5 deletions denorm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function denormalized.deco refactored with the following changes:

return deco


Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CacheWrapper.__set__ refactored with the following changes:

obj.__dict__[self.field.name] = cached


Expand All @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cached.deco refactored with the following changes:

return deco
14 changes: 0 additions & 14 deletions test_denorm_project/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestDenormalisation.test_middleware refactored with the following changes:

This removes the following comments ( why? ):

# FIXME, set and de-set middleware values


def test_countfield(self):
f1 = models.Forum.objects.create(title="forumone")
Expand Down