Skip to content

Commit

Permalink
speed optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed Dec 21, 2009
1 parent 1b98f67 commit 640c7be
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions djangomarkup/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def delete_for_object(self, instance):
object_id=pk,
).delete()

def extract_from_instance(self, instance, processor, fields, content_type=None, force_save=False):
def extract_from_instance(self, instance, processor, fields, content_type=None, force_save=False, force_create=False):
if not content_type:
content_type = ContentType.objects.get_for_model(instance)

Expand All @@ -82,20 +82,34 @@ def extract_from_instance(self, instance, processor, fields, content_type=None,
val = getattr(instance, f)
if not val:
continue
st, created = self.get_or_create(
content_type=content_type,
object_id=instance.pk,
field=f,
defaults={
'processor': processor,
'content': val,
}

if force_create:
created = True
st = self.create(
content_type=content_type,
object_id=instance.pk,
field=f,
processor=processor,
content=val,
)
else:
st, created = self.get_or_create(
content_type=content_type,
object_id=instance.pk,
field=f,
defaults={
'processor': processor,
'content': val,
}
)
if created:
setattr(instance, f, st.render())
dirty = True

if dirty or force_save:
instance.save(force_update=True)
instance.__class__.objects.filter(pk=instance.pk).update(
**dict( (f, getattr(instance, f)) for f in fields )
)

def extract_from_model(self, model, processor, fields):
if not fields:
Expand Down

0 comments on commit 640c7be

Please sign in to comment.