Skip to content

Commit

Permalink
Split extract_from_model and created extract_from_instance to allow f…
Browse files Browse the repository at this point in the history
…or easier extending
  • Loading branch information
honzakral committed Jun 11, 2009
1 parent 9aa0f3a commit cd614dd
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions djangomarkup/models.py
Expand Up @@ -73,29 +73,35 @@ def delete_for_object(self, instance):
object_id=pk,
).delete()

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

dirty = False
for f in fields:
st, created = self.get_or_create(
content_type=content_type,
object_id=instance.pk,
field=f,
defaults={
'processor': processor,
'content': getattr(instance, f),
}
)
if created:
setattr(instance, f, st.render())
dirty = True
if dirty:
instance.save(force_update=True)

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

ct = ContentType.objects.get_for_model(model)

for m in model._default_manager.all():
dirty = False
for f in fields:
st, created = self.get_or_create(
content_type=ct,
object_id=m.pk,
field=f,
defaults={
'processor': processor,
'content': getattr(m, f),
}
)
if created:
setattr(m, f, st.render())
dirty = True
if dirty:
m.save(force_update=True)
self.extract_from_instance(m, processor, fields, ct)


class SourceText(models.Model):
Expand Down

0 comments on commit cd614dd

Please sign in to comment.