from __future__ import unicode_literals

from django_fsm_log.conf import settings
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.timezone import now
from django.utils.module_loading import import_by_path

from django_fsm.signals import pre_transition, post_transition

from .managers import StateLogManager


class StateLog(models.Model):
    timestamp = models.DateTimeField(default=now)
    by = models.ForeignKey(getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), blank=True, null=True)
    state = models.CharField(max_length=255, db_index=True)
    transition = models.CharField(max_length=255)

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_index=True)
    content_object = GenericForeignKey('content_type', 'object_id')

    objects = StateLogManager()

    def __unicode__(self):
        return '{} - {} - {}'.format(
            self.timestamp,
            self.content_object,
            self.transition
        )

def connect_transition_receivers():
    backend = import_by_path(settings.DJANGO_FSM_LOG_CACHE_BACKEND)
    pre_transition.connect(backend.pre_transition_callback)
    post_transition.connect(backend.post_transition_callback)


connect_transition_receivers()

@@ -1099,48 +1099,8 @@
  )%0A%0A
-def connect_transition_receivers():%0A    
 back
@@ -1159,20 +1159,16 @@
 ACKEND)%0A
-    
 pre_tran
@@ -1215,20 +1215,16 @@
 llback)%0A
-    
 post_tra
@@ -1277,37 +1277,4 @@
 ck)%0A
-%0A%0Aconnect_transition_receivers()%0A

don't wrap this in a function