Skip to content

Commit

Permalink
created and applied settings configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Arulnathan committed Jan 22, 2014
1 parent 44d366b commit 6bf3d6e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
13 changes: 13 additions & 0 deletions django_fsm_log/backends.py
Original file line number Diff line number Diff line change
@@ -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')

2 changes: 1 addition & 1 deletion django_fsm_log/managers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
13 changes: 7 additions & 6 deletions django_fsm_log/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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,
Expand All @@ -44,16 +45,16 @@ 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,
transition=name,
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)
11 changes: 3 additions & 8 deletions django_fsm_log/settings.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6bf3d6e

Please sign in to comment.