diff --git a/django_fsm_log/backends.py b/django_fsm_log/backends.py new file mode 100644 index 0000000..2e5c1fe --- /dev/null +++ b/django_fsm_log/backends.py @@ -0,0 +1,13 @@ +from django.core.exceptions import ImproperlyConfigured +from django.core.cache import get_cache +from django.conf import settings +from .settings import DJANGO_FSM_LOG_USE_CACHE + +if DJANGO_FSM_LOG_USE_CACHE: + if hasattr(settings, 'DJANGO_FSM_LOG_CACHE_BACKEND'): + cache = get_cache(settings.DJANGO_FSM_LOG_CACHE_BACKEND) +elif hasattr(settings, 'DJANGO_FSM_LOG_CACHE_BACKEND'): + raise ImproperlyConfigured +else: + cache = get_cache('default') + diff --git a/django_fsm_log/managers.py b/django_fsm_log/managers.py index baaf831..2f2c2ad 100644 --- a/django_fsm_log/managers.py +++ b/django_fsm_log/managers.py @@ -1,7 +1,7 @@ from django.db import models from django.db.models.query import QuerySet from django.contrib.contenttypes.models import ContentType -from django.core.cache import cache +from .backends import cache class StateLogQuerySet(QuerySet): diff --git a/django_fsm_log/models.py b/django_fsm_log/models.py index c7caa7a..b7e20f3 100644 --- a/django_fsm_log/models.py +++ b/django_fsm_log/models.py @@ -5,11 +5,10 @@ from django.contrib.contenttypes.models import ContentType from django.db import models from django.utils.timezone import now -from django.core.cache import cache from django_fsm.signals import pre_transition, post_transition -from . import settings +from .backends import DJANGO_FSM_LOG_USE_CACHE from .managers import StateLogManager @@ -34,7 +33,9 @@ def __unicode__(self): def pre_transition_callback(sender, instance, name, source, target, **kwargs): - if settings.DJANGO_FSM_LOG_PENDING_STATELOGS: + if not DJANGO_FSM_LOG_USE_CACHE: + pass + else: StateLog.objects.create_pending( by=getattr(instance, 'by', None), state=target, @@ -44,9 +45,7 @@ def pre_transition_callback(sender, instance, name, source, target, **kwargs): def post_transition_callback(sender, instance, name, source, target, **kwargs): - if settings.DJANGO_FSM_LOG_PENDING_STATELOGS: - StateLog.objects.commit_pending_for_object(instance) - else: + if not DJANGO_FSM_LOG_USE_CACHE: state_log = StateLog( by=getattr(instance, 'by', None), state=target, @@ -54,6 +53,8 @@ def post_transition_callback(sender, instance, name, source, target, **kwargs): content_object=instance, ) state_log.save() + else: + StateLog.objects.commit_pending_for_object(instance) pre_transition.connect(pre_transition_callback) post_transition.connect(post_transition_callback) diff --git a/django_fsm_log/settings.py b/django_fsm_log/settings.py index 003abb0..44a3af1 100644 --- a/django_fsm_log/settings.py +++ b/django_fsm_log/settings.py @@ -1,11 +1,6 @@ -"""Settings for django-fsm-log""" -import logging from django.conf import settings -LOG = logging.getLogger(__name__) - -if not hasattr(settings, 'CACHES'): - LOG.warning("No cache backend set in django. You will not be able to access pending StateLogs") - DJANGO_FSM_LOG_PENDING_STATELOGS = False +if hasattr(settings, 'DJANGO_FSM_LOG_USE_CACHE'): + DJANGO_FSM_LOG_USE_CACHE = settings.DJANGO_FSM_LOG_USE_CACHE else: - DJANGO_FSM_LOG_PENDING_STATELOGS = True + DJANGO_FSM_LOG_USE_CACHE = False