<s> from __future__ import unicode_literals <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . models import signals <EOL> from django . dispatch import receiver <EOL> from djangobmf . conf import settings <EOL> from djangobmf . signals import activity_create <EOL> from djangobmf . signals import activity_update <EOL> from djangobmf . signals import activity_addfile <EOL> from djangobmf . signals import activity_workflow <EOL> from djangobmf . tasks import djangobmf_user_watch <EOL> from djangobmf . utils . serializers import DjangoBMFEncoder <EOL> import json <EOL> from . base import BMFModel <EOL> from . base import BMFModelBase <EOL> from . activity import ACTION_COMMENT <EOL> from . activity import ACTION_CREATED <EOL> from . activity import ACTION_UPDATED <EOL> from . activity import ACTION_WORKFLOW <EOL> from . activity import ACTION_FILE <EOL> from . activity import Activity as AbstractActivity <EOL> from . configuration import Configuration as AbstractConfiguration <EOL> from . document import Document as AbstractDocument <EOL> from . notification import Notification as AbstractNotification <EOL> from . numberrange import NumberRange as AbstractNumberRange <EOL> from . renderer import Renderer as AbstractRenderer <EOL> from . report import Report as AbstractReport <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class Activity ( AbstractActivity ) : <EOL> class Meta ( AbstractActivity . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> class Configuration ( AbstractConfiguration ) : <EOL> class Meta ( AbstractConfiguration . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> class Document ( AbstractDocument ) : <EOL> class Meta ( AbstractDocument . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> class Notification ( AbstractNotification ) : <EOL> class Meta ( AbstractNotification . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> class NumberRange ( AbstractNumberRange ) : <EOL> class Meta ( AbstractNumberRange . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> class Renderer ( AbstractRenderer ) : <EOL> class Meta ( AbstractRenderer . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> class Report ( AbstractReport ) : <EOL> class Meta ( AbstractReport . Meta ) : <EOL> abstract = False <EOL> app_label = settings . APP_LABEL <EOL> @ receiver ( activity_create ) <EOL> def object_created ( sender , instance , ** kwargs ) : <EOL> if instance . _bmfmeta . has_logging : <EOL> history = Activity ( <EOL> user = instance . created_by , <EOL> parent_ct = ContentType . objects . get_for_model ( sender ) , <EOL> parent_id = instance . pk , <EOL> action = ACTION_CREATED , <EOL> ) <EOL> history . save ( ) <EOL> @ receiver ( activity_update ) <EOL> def object_changed ( sender , instance , ** kwargs ) : <EOL> if instance . _bmfmeta . has_logging and len ( instance . _bmfmeta . observed_fields ) > <NUM_LIT:0> : <EOL> changes = [ ] <EOL> for key in instance . _bmfmeta . observed_fields : <EOL> try : <EOL> if instance . _bmfmeta . changelog [ key ] != getattr ( instance , key ) : <EOL> changes . append ( ( key , instance . _bmfmeta . changelog [ key ] , getattr ( instance , key ) ) ) <EOL> except KeyError : <EOL> pass <EOL> if len ( changes ) > <NUM_LIT:0> : <EOL> history = Activity ( <EOL> user = instance . modified_by , <EOL> parent_ct = ContentType . objects . get_for_model ( sender ) , <EOL> parent_id = instance . pk , <EOL> action = ACTION_UPDATED , <EOL> text = json . dumps ( changes , cls = DjangoBMFEncoder ) , <EOL> ) <EOL> history . save ( ) <EOL> @ receiver ( activity_workflow ) <EOL> def new_state ( sender , instance , ** kwargs ) : <EOL> if instance . _bmfmeta . has_logging : <EOL> history = Activity ( <EOL> user = instance . modified_by , <EOL> parent_ct = ContentType . objects . get_for_model ( sender ) , <EOL> parent_id = instance . pk , <EOL> action = ACTION_WORKFLOW , <EOL> text = json . dumps ( { <EOL> '<STR_LIT>' : instance . _bmfmeta . workflow . initial , <EOL> '<STR_LIT>' : instance . _bmfmeta . workflow . key , <EOL> } , cls = DjangoBMFEncoder ) , <EOL> ) <EOL> history . save ( ) <EOL> @ receiver ( activity_addfile ) <EOL> def new_file ( sender , instance , file , ** kwargs ) : <EOL> if instance . _bmfmeta . has_logging : <EOL> history = Activity ( <EOL> user = instance . modified_by , <EOL> parent_ct = ContentType . objects . get_for_model ( sender ) , <EOL> parent_id = instance . pk , <EOL> action = ACTION_FILE , <EOL> text = json . dumps ( { <EOL> '<STR_LIT>' : file . pk , <EOL> '<STR_LIT:size>' : file . size , <EOL> '<STR_LIT:name>' : '<STR_LIT:%s>' % file , <EOL> } , cls = DjangoBMFEncoder ) , <EOL> ) <EOL> history . save ( ) <EOL> def activity_post_save ( sender , instance , * args , ** kwargs ) : <EOL> djangobmf_user_watch ( instance . pk ) <EOL> signals . post_save . connect ( activity_post_save , sender = Activity ) </s>
