0001-add-compatiblity-with-django-1.11-21145.patch
debian/control | ||
---|---|---|
3 | 3 |
Section: python |
4 | 4 |
Priority: optional |
5 | 5 |
Build-Depends: python-setuptools (>= 0.6b3), python-all (>= 2.6), debhelper (>= 7.4.3), |
6 |
python-django (>= 1.5)
|
|
6 |
python-django (>= 1.8)
|
|
7 | 7 |
Standards-Version: 3.9.1 |
8 | 8 |
X-Python-Version: >= 2.7 |
9 | 9 | |
... | ... | |
11 | 11 |
Architecture: all |
12 | 12 |
Depends: ${misc:Depends}, |
13 | 13 |
python (>= 2.6), |
14 |
python-django (>= 1.5),
|
|
14 |
python-django (>= 1.8),
|
|
15 | 15 |
python-django-model-utils |
16 | 16 |
Description: Django application to keep a structured -- i.e. not just log |
17 | 17 |
strings -- journal of events in your project |
django_journal/__init__.py | ||
---|---|---|
1 |
import logging |
|
2 | ||
3 |
from exceptions import JournalException |
|
4 |
from models import (Journal, Tag, Template) |
|
5 | ||
6 |
import django.db.models |
|
7 |
from django.conf import settings |
|
8 | ||
9 |
from decorator import atomic |
|
10 | ||
11 |
__all__ = ('record', 'error_record', 'Journal') |
|
12 | ||
13 | ||
14 |
def unicode_truncate(s, length, encoding='utf-8'): |
|
15 |
'''Truncate an unicode string so that its UTF-8 encoding is less than |
|
16 |
length.''' |
|
17 |
encoded = s.encode(encoding)[:length] |
|
18 |
return encoded.decode(encoding, 'ignore') |
|
19 | ||
20 |
@atomic |
|
21 |
def record(tag, template, using=None, **kwargs): |
|
22 |
'''Record an event in the journal. The modification is done inside the |
|
23 |
current transaction. |
|
24 | ||
25 |
tag: |
|
26 |
a string identifier giving the type of the event |
|
27 |
tpl: |
|
28 |
a format string to describe the event |
|
29 |
kwargs: |
|
30 |
a mapping of object or data to interpolate in the format string |
|
31 |
''' |
|
32 |
template = unicode(template) |
|
33 |
tag = Tag.objects.using(using).get_cached(name=tag) |
|
34 |
template = Template.objects.using(using).get_cached(content=template) |
|
35 |
try: |
|
36 |
message = template.content.format(**kwargs) |
|
37 |
except (KeyError, IndexError), e: |
|
38 |
raise JournalException( |
|
39 |
'Missing variable for the template message', template, e) |
|
40 |
try: |
|
41 |
logger = logging.getLogger('django.journal.%s' % tag) |
|
42 |
if tag.name == 'error' or tag.name.startswith('error-'): |
|
43 |
logger.error(message) |
|
44 |
elif tag.name == 'warning' or tag.name.startswith('warning-'): |
|
45 |
logger.warning(message) |
|
46 |
else: |
|
47 |
logger.info(message) |
|
48 |
except: |
|
49 |
try: |
|
50 |
logging.getLogger('django.journal').exception('Unable to log msg') |
|
51 |
except: |
|
52 |
pass # we tried, really, we tried |
|
53 |
journal = Journal.objects.using(using).create(tag=tag, template=template, |
|
54 |
message=unicode_truncate(message, 128)) |
|
55 |
for name, value in kwargs.iteritems(): |
|
56 |
if value is None: |
|
57 |
continue |
|
58 |
tag = Tag.objects.using(using).get_cached(name=name) |
|
59 |
if isinstance(value, django.db.models.Model): |
|
60 |
journal.objectdata_set.create(tag=tag, content_object=value) |
|
61 |
else: |
|
62 |
journal.stringdata_set.create(tag=tag, content=unicode(value)) |
|
63 |
return journal |
|
64 | ||
65 |
def error_record(tag, tpl, **kwargs): |
|
66 |
'''Records error events. |
|
67 | ||
68 |
You must use this function when logging error events. It uses another |
|
69 |
database alias than the default one to be immune to transaction rollback |
|
70 |
when logging in the middle of a transaction which is going to |
|
71 |
rollback. |
|
72 |
''' |
|
73 |
if kwargs.get('using') is None: |
|
74 |
kwargs['using'] = getattr(settings, 'JOURNAL_DB_FOR_ERROR_ALIAS', 'default') |
|
75 |
return record(tag, tpl, **kwargs) |
django_journal/admin.py | ||
---|---|---|
3 | 3 |
import django.contrib.admin as admin |
4 | 4 |
from django.contrib.contenttypes.models import ContentType |
5 | 5 |
from django.utils.html import escape |
6 | ||
7 |
from models import Journal, Tag, ObjectData, StringData |
|
8 | ||
9 | 6 |
from django.db import models |
10 | 7 |
from django.utils.translation import ugettext_lazy as _ |
11 |
from django.utils.html import escape |
|
12 | 8 |
from django.core.urlresolvers import reverse, NoReverseMatch |
13 | 9 | |
14 |
import actions |
|
10 |
from .models import Journal, Tag, ObjectData, StringData |
|
11 |
from .actions import export_as_csv |
|
12 | ||
15 | 13 | |
16 | 14 |
class ModelAdminFormatter(Formatter): |
17 | 15 |
def __init__(self, model_admin=None, filter_link=True, |
... | ... | |
71 | 69 |
) |
72 | 70 |
date_hierarchy = 'time' |
73 | 71 |
search_fields = ('message','tag__name','time') |
74 |
actions = [ actions.export_as_csv ]
|
|
72 |
actions = [ export_as_csv ] |
|
75 | 73 | |
76 | 74 |
class Media: |
77 | 75 |
css = { |
django_journal/journal.py | ||
---|---|---|
1 |
import logging |
|
2 | ||
3 |
from django.conf import settings |
|
4 |
import django.db.models |
|
5 | ||
6 |
from .decorator import atomic |
|
7 |
from .exceptions import JournalException |
|
8 |
from .models import Journal, Tag, Template |
|
9 | ||
10 | ||
11 |
def unicode_truncate(s, length, encoding='utf-8'): |
|
12 |
'''Truncate an unicode string so that its UTF-8 encoding is less than |
|
13 |
length.''' |
|
14 |
encoded = s.encode(encoding)[:length] |
|
15 |
return encoded.decode(encoding, 'ignore') |
|
16 | ||
17 | ||
18 |
@atomic |
|
19 |
def record(tag, template, using=None, **kwargs): |
|
20 |
'''Record an event in the journal. The modification is done inside the |
|
21 |
current transaction. |
|
22 | ||
23 |
tag: |
|
24 |
a string identifier giving the type of the event |
|
25 |
tpl: |
|
26 |
a format string to describe the event |
|
27 |
kwargs: |
|
28 |
a mapping of object or data to interpolate in the format string |
|
29 |
''' |
|
30 |
template = unicode(template) |
|
31 |
tag = Tag.objects.using(using).get_cached(name=tag) |
|
32 |
template = Template.objects.using(using).get_cached(content=template) |
|
33 |
try: |
|
34 |
message = template.content.format(**kwargs) |
|
35 |
except (KeyError, IndexError), e: |
|
36 |
raise JournalException( |
|
37 |
'Missing variable for the template message', template, e) |
|
38 |
try: |
|
39 |
logger = logging.getLogger('django.journal.%s' % tag) |
|
40 |
if tag.name == 'error' or tag.name.startswith('error-'): |
|
41 |
logger.error(message) |
|
42 |
elif tag.name == 'warning' or tag.name.startswith('warning-'): |
|
43 |
logger.warning(message) |
|
44 |
else: |
|
45 |
logger.info(message) |
|
46 |
except: |
|
47 |
try: |
|
48 |
logging.getLogger('django.journal').exception('Unable to log msg') |
|
49 |
except: |
|
50 |
pass # we tried, really, we tried |
|
51 |
journal = Journal.objects.using(using).create(tag=tag, template=template, |
|
52 |
message=unicode_truncate(message, 128)) |
|
53 |
for name, value in kwargs.iteritems(): |
|
54 |
if value is None: |
|
55 |
continue |
|
56 |
tag = Tag.objects.using(using).get_cached(name=name) |
|
57 |
if isinstance(value, django.db.models.Model): |
|
58 |
journal.objectdata_set.create(tag=tag, content_object=value) |
|
59 |
else: |
|
60 |
journal.stringdata_set.create(tag=tag, content=unicode(value)) |
|
61 |
return journal |
|
62 | ||
63 | ||
64 |
def error_record(tag, tpl, **kwargs): |
|
65 |
'''Records error events. |
|
66 | ||
67 |
You must use this function when logging error events. It uses another |
|
68 |
database alias than the default one to be immune to transaction rollback |
|
69 |
when logging in the middle of a transaction which is going to |
|
70 |
rollback. |
|
71 |
''' |
|
72 |
if kwargs.get('using') is None: |
|
73 |
kwargs['using'] = getattr(settings, 'JOURNAL_DB_FOR_ERROR_ALIAS', 'default') |
|
74 |
return record(tag, tpl, **kwargs) |
django_journal/middleware.py | ||
---|---|---|
1 |
import django_journal |
|
1 |
from django_journal import journal |
|
2 | ||
2 | 3 | |
3 | 4 |
class JournalMiddleware(object): |
4 | 5 |
'''Add record and error_record methods to the request object to log |
... | ... | |
15 | 16 |
kwargs['user'] = user |
16 | 17 |
if 'ip' not in kwargs: |
17 | 18 |
kwargs['ip'] = ip |
18 |
django_journal.record(tag, template, using=using,**kwargs)
|
|
19 |
journal.record(tag, template, using=using,**kwargs) |
|
19 | 20 |
def error_record(tag, template, using=None, **kwargs): |
20 | 21 |
if 'user' not in kwargs: |
21 | 22 |
kwargs['user'] = user |
22 | 23 |
if 'ip' not in kwargs: |
23 | 24 |
kwargs['ip'] = ip |
24 |
django_journal.error_record(tag, template, using=using, **kwargs)
|
|
25 |
journal.error_record(tag, template, using=using, **kwargs) |
|
25 | 26 |
request.record = record |
26 | 27 |
request.error_record = error_record |
27 | 28 |
return None |
django_journal/models.py | ||
---|---|---|
1 | 1 |
import string |
2 | 2 | |
3 | 3 |
from django.db import models |
4 |
from django.contrib.contenttypes import generic
|
|
4 |
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
5 | 5 |
from django.utils.translation import ugettext_lazy as _ |
6 | 6 | |
7 | 7 |
import managers |
... | ... | |
143 | 143 |
verbose_name=_('content type')) |
144 | 144 |
object_id = models.PositiveIntegerField(db_index=True, |
145 | 145 |
verbose_name=_('object id')) |
146 |
content_object = generic.GenericForeignKey('content_type',
|
|
146 |
content_object = GenericForeignKey('content_type', |
|
147 | 147 |
'object_id') |
148 | 148 | |
149 | 149 |
class Meta: |
setup.py | ||
---|---|---|
116 | 116 |
'test': test |
117 | 117 |
}, |
118 | 118 |
install_requires=[ |
119 |
'django >= 1.7,<1.9',
|
|
119 |
'django >= 1.8,<2.0',
|
|
120 | 120 |
'django-model-utils', |
121 | 121 |
]) |
tests/test_main.py | ||
---|---|---|
3 | 3 |
from django.db import transaction |
4 | 4 | |
5 | 5 | |
6 |
from django_journal import record, error_record |
|
6 |
from django_journal.journal import record, error_record
|
|
7 | 7 |
from django_journal.actions import export_as_csv_generator |
8 | 8 |
from django_journal.models import Journal |
9 | 9 |
tox.ini | ||
---|---|---|
1 | 1 |
[tox] |
2 | 2 |
toxworkdir = /tmp/tox-{env:USER}/django-journal/{env:BRANCH_NAME:} |
3 |
envlist = py2-coverage |
|
3 |
envlist = py2-coverage-{django111,django18}
|
|
4 | 4 | |
5 | 5 | |
6 | 6 |
[testenv] |
7 | 7 |
basepython = python2 |
8 | 8 |
usedevelop = True |
9 | 9 |
deps = |
10 |
django18: django<1.9 |
|
11 |
django111: django<2 |
|
10 | 12 |
psycopg2 |
11 | 13 |
pytest-cov |
12 | 14 |
pytest |
13 |
- |