0002-base-use-deferrable-to-create-ResourceLog-objects-31.patch
passerelle/base/models.py | ||
---|---|---|
34 | 34 |
from django.utils.translation import ugettext_lazy as _ |
35 | 35 |
from model_utils.managers import InheritanceManager as ModelUtilsInheritanceManager |
36 | 36 | |
37 | ||
37 | 38 |
import passerelle |
38 | 39 |
from passerelle.forms import GenericConnectorForm |
39 | 40 |
from passerelle.utils import ImportSiteError |
40 | 41 |
from passerelle.utils.api import endpoint |
42 |
from passerelle.utils.defer import deferrable |
|
41 | 43 |
from passerelle.utils.jsonresponse import APIError |
42 | 44 |
from passerelle.utils.sftp import SFTP, SFTPField |
43 | 45 | |
... | ... | |
867 | 869 |
extra = JSONField(verbose_name='extras', default=dict) |
868 | 870 |
transaction_id = models.UUIDField(null=True, db_index=True) |
869 | 871 | |
872 |
@classmethod |
|
873 |
@deferrable |
|
874 |
def safe_create(cls, **attr): |
|
875 |
cls.objects.create(**attr) |
|
876 | ||
870 | 877 |
class Meta: |
871 | 878 |
ordering = ('id',) |
872 | 879 |
permissions = (('see_resourcelog', 'Can see resource logs'),) |
... | ... | |
1020 | 1027 |
(exc_type, exc_value, dummy) = sys.exc_info() |
1021 | 1028 |
attr['extra']['error_summary'] = traceback.format_exception_only(exc_type, exc_value) |
1022 | 1029 | |
1023 |
ResourceLog.objects.create(**attr)
|
|
1030 |
ResourceLog.safe_create(**attr)
|
|
1024 | 1031 | |
1025 | 1032 |
admins = settings.ADMINS |
1026 | 1033 |
logging_parameters = self.connector.logging_parameters |
tests/test_proxylogger.py | ||
---|---|---|
389 | 389 |
base_logger.debug('test', extra={'payload': b'\xff\xff'}) |
390 | 390 |
log = ResourceLog.objects.latest('id') |
391 | 391 |
assert log.extra == {'payload': '\\xff\\xff'} |
392 | ||
393 | ||
394 |
def test_log_in_transaction(transactional_db, connector): |
|
395 |
from django.db import transaction |
|
396 | ||
397 |
from passerelle.utils.defer import deferrable_scope |
|
398 | ||
399 |
qs = ResourceLog.objects.filter(message='coucou') |
|
400 |
# without deferrable_scope logs are lost |
|
401 |
assert not qs.exists() |
|
402 |
try: |
|
403 |
with transaction.atomic(): |
|
404 |
connector.logger.info('coucou') |
|
405 |
raise Exception |
|
406 |
assert qs.exists() |
|
407 |
except Exception: |
|
408 |
pass |
|
409 |
assert not qs.exists() |
|
410 | ||
411 |
# with deferrable_scope logs are kept |
|
412 |
try: |
|
413 |
with deferrable_scope: |
|
414 |
with transaction.atomic(): |
|
415 |
connector.logger.info('coucou') |
|
416 |
raise Exception |
|
417 |
assert qs.exists() |
|
418 |
except Exception: |
|
419 |
pass |
|
420 |
assert qs.exists() |
|
392 |
- |