grouped object creations in a transaction, use a cache for retrieving tags and templates
This commit is contained in:
parent
aba33fce63
commit
f1a2d54f1a
|
@ -1,7 +1,8 @@
|
|||
from exceptions import JournalException
|
||||
from models import (Journal, Tag, Template, ObjectData, StringData)
|
||||
from models import (Journal, Tag, Template)
|
||||
|
||||
from django.db import models
|
||||
|
||||
from django.db import models, transaction
|
||||
from django.conf import settings
|
||||
|
||||
__all__ = ('record', 'error_record', 'Journal')
|
||||
|
@ -12,7 +13,28 @@ def unicode_truncate(s, length, encoding='utf-8'):
|
|||
encoded = s.encode(encoding)[:length]
|
||||
return encoded.decode(encoding, 'ignore')
|
||||
|
||||
def record(tag, tpl, using='default', **kwargs):
|
||||
|
||||
__TAG_CACHE = dict()
|
||||
|
||||
|
||||
def get_tag(name, using=None):
|
||||
if name not in __TAG_CACHE:
|
||||
__TAG_CACHE[name], created = \
|
||||
Tag.objects.using(using).get_or_create(name=name)
|
||||
return __TAG_CACHE[name]
|
||||
|
||||
|
||||
__TEMPLATE_CACHE = dict()
|
||||
|
||||
|
||||
def get_template(content, using=None):
|
||||
if content not in __TEMPLATE_CACHE:
|
||||
__TEMPLATE_CACHE[content], created = \
|
||||
Template.objects.using(using).get_or_create(content=content)
|
||||
return __TEMPLATE_CACHE[content]
|
||||
|
||||
|
||||
def record(tag, template, using=None, **kwargs):
|
||||
'''Record an event in the journal. The modification is done inside the
|
||||
current transaction.
|
||||
|
||||
|
@ -23,22 +45,23 @@ def record(tag, tpl, using='default', **kwargs):
|
|||
kwargs:
|
||||
a mapping of object or data to interpolate in the format string
|
||||
'''
|
||||
tag, created = Tag.objects.using(using).get_or_create(name=tag)
|
||||
template, created = Template.objects.using(using).get_or_create(content=tpl)
|
||||
try:
|
||||
message = tpl.format(**kwargs)
|
||||
except (KeyError, IndexError), e:
|
||||
raise JournalException(
|
||||
'Missing variable for the template message', tpl, e)
|
||||
journal = Journal.objects.using(using).create(tag=tag, template=template,
|
||||
message=unicode_truncate(message, 128))
|
||||
for tag, value in kwargs.iteritems():
|
||||
tag, created = Tag.objects.using(using).get_or_create(name=tag)
|
||||
if isinstance(value, models.Model):
|
||||
journal.objectdata_set.create(tag=tag, content_object=value)
|
||||
else:
|
||||
journal.stringdata_set.create(tag=tag, content=unicode(value))
|
||||
return journal
|
||||
with transaction.commit_on_success(using=using):
|
||||
tag = get_tag(tag, using=using)
|
||||
template = get_template(template, using=using)
|
||||
try:
|
||||
message = template.content.format(**kwargs)
|
||||
except (KeyError, IndexError), e:
|
||||
raise JournalException(
|
||||
'Missing variable for the template message', template, e)
|
||||
journal = Journal.objects.using(using).create(tag=tag, template=template,
|
||||
message=unicode_truncate(message, 128))
|
||||
for tag, value in kwargs.iteritems():
|
||||
tag = get_tag(tag, using=using)
|
||||
if isinstance(value, models.Model):
|
||||
journal.objectdata_set.create(tag=tag, content_object=value)
|
||||
else:
|
||||
journal.stringdata_set.create(tag=tag, content=unicode(value))
|
||||
return journal
|
||||
|
||||
def error_record(tag, tpl, **kwargs):
|
||||
'''Records error events.
|
||||
|
|
Loading…
Reference in New Issue