from django_fsm_log.conf import settings
from django.core.cache import get_cache


class BaseBackend(object):

    @staticmethod
    def setup_model(model):
        raise NotImplementedError

    @staticmethod
    def pre_transition_callback(*args, **kwargs):
        raise NotImplementedError

    @staticmethod
    def post_transition_callback(*args, **kwargs):
        raise NotImplementedError


class CachedBackend(object):

    @staticmethod
    def setup_model(model):
        from .managers import PendingStateLogManager
        model.add_to_class('pending_objects', PendingStateLogManager())

    @staticmethod
    def pre_transition_callback(sender, instance, name, source, target, **kwargs):
        from .models import StateLog
        StateLog.pending_objects.create(
            by=getattr(instance, 'by', None),
            state=target,
            transition=name,
            content_object=instance,
        )

    @staticmethod
    def post_transition_callback(sender, instance, name, source, target, **kwargs):
        from .models import StateLog
        StateLog.pending_objects.commit_for_object(instance)


class SimpleBackend(object):

    @staticmethod
    def setup_model(model):
        pass

    @staticmethod
    def pre_transition_callback(sender, **kwargs):
        pass

    @staticmethod
    def post_transition_callback(sender, instance, name, source, target, **kwargs):
        from .models import StateLog
        StateLog.objects.create(
            by=getattr(instance, 'by', None),
            state=target,
            transition=name,
            content_object=instance,
        )


if settings.DJANGO_FSM_LOG_STORAGE_METHOD == 'django_fsm_log.backends.CachedBackend':
    cache = get_cache(settings.DJANGO_FSM_LOG_CACHE_BACKEND)
else:
    cache = None

@@ -38,48 +38,8 @@
 ngs%0A
-from django.core.cache import get_cache%0A
 %0A%0Acl
@@ -1674,64 +1674,297 @@
     
-cache = get_cache(settings.DJANGO_FSM_LOG_CACHE_BACKEND)
+try:%0A        from django.core.cache import caches%0A    except ImportError:%0A        from django.core.cache import get_cache  # Deprecated, removed in 1.9.%0A        cache = get_cache(settings.DJANGO_FSM_LOG_CACHE_BACKEND)%0A    else:%0A        cache = caches%5Bsettings.DJANGO_FSM_LOG_CACHE_BACKEND%5D
 %0Aels

Use new django.core.cache.caches (for Django 1.9)