Commits

Lukasz Balcerzak committed f259c91

Temp class based signals handler - will be rolled back

  • Participants
  • Parent commits 4ae6d16

Comments (0)

Files changed (3)

File example_project/settings.py

 
 
     # External
-    'actstream',
     'djalog',
     'django_extensions',
     'django_sorting',

File projector/actions.py

 
 import logging
 
-class ProjectAction(object):
+
+class ProjectorBaseAction(object):
+    """
+    Base Action class. Must define following attributes:
+
+    ``alias``: unique identifier of this action
+
+    ``verb``: short action's verb
+
+    ``signal``: signal with which this action would be connected
+
+    ``sender``: sender class or object
+
+    Action classes must implement required, static method called ``action``
+    which in fact is standard Django signal handler.
+    """
+
+    @staticmethod
+    def action(sender, **kwargs):
+        raise NotImplementedError
+
+    @classmethod
+    def connect_signal(cls):
+        cls.signal.connect(cls.action, sender=cls.sender)
+
+
+class ProjectCreatedAction(ProjectorBaseAction):
     alias = "project_created"
     verb = "created"
     signal = post_save
     @staticmethod
     def action(sender, instance, created, **kwargs):
         logging.debug("Sending ProjectAction")
+        if created and not instance.parent:
+            instance.create_action(ProjectCreatedAction.verb)
+
+
+class ProjectForkedAction(ProjectorBaseAction):
+    alias = "project_forked"
+    verb = "forked"
+    signal = post_save
+    sender = Project
+
+    @staticmethod
+    def action(sender, instance, created, **kwargs):
         if created and instance.parent:
-            instance.parent.create_action("forked", author=instance.author)
-        elif created:
-            instance.create_action("created")
-
-    def connect_signal(self):
-        self.signal.connect(ProjectAction.action, sender=ProjectAction.sender)
+            instance.parent.create_action(ProjectForkedAction.verb,
+                author=instance.author)
 
 
 def action_project_created(sender, instance, created, **kwargs):
     """
     #post_save.connect(action_project_created, sender=Project)
     actions = (
-        ProjectAction,
+        ProjectCreatedAction,
+        ProjectForkedAction,
     )
-    for ActionClass in actions:
-        action = ActionClass()
-        action.connect_signal()
+    for cls in actions:
+        cls.connect_signal()
 

File projector/tests/test_activity.py

         self.assertEqual(actions[0].verb, 'created')
         self.assertEqual(actions[0].is_public, self.project.public)
 
+    def test_project_forked(self):
+        fork = self.project.fork(self.jack)
+
+        actions = fork.get_actions().all()
+        self.assertEqual(actions.count(), 0)
+
+        actions = self.project.get_actions().all()
+        self.assertEqual(actions.count(), 2)
+        self.assertEqual(actions[0].verb, 'forked')
+        self.assertEqual(actions[1].verb, 'created')
+
     def test_project_action_with_custom_params(self):
         verb = "custom action taken"
         author = self.jack # other than author
         self.assertEqual(action.action_object, action_object)
         self.assertEqual(action.project, self.project)
 
-    def test_project_forked(self):
-        fork = self.project.fork(self.jack)
-
-        actions = fork.get_actions().all()
-        self.assertEqual(actions.count(), 0)
-
-        actions = self.project.get_actions().all()
-        self.assertEqual(actions.count(), 2)
-        self.assertEqual(actions[0].verb, 'forked')
-        self.assertEqual(actions[1].verb, 'created')
-