You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2014/05/09 23:42:27 UTC

[07/18] git commit: [#7257] ticket:562 Made indexer extension work with multiple models

[#7257] ticket:562 Made indexer extension work with multiple models


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/f2cdeed8
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/f2cdeed8
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/f2cdeed8

Branch: refs/heads/db/7257
Commit: f2cdeed869f3f51cc8c40d42103eaca71d8444ca
Parents: e5fa710
Author: Ferens Dmitriy <fe...@gmail.com>
Authored: Fri Mar 28 18:06:51 2014 +0200
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Fri May 9 18:56:47 2014 +0000

----------------------------------------------------------------------
 Allura/allura/model/session.py           | 51 +++++++++++++++++++--------
 Allura/allura/tests/unit/test_session.py |  9 ++---
 2 files changed, 41 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/f2cdeed8/Allura/allura/model/session.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/session.py b/Allura/allura/model/session.py
index 2b38440..b2abf3b 100644
--- a/Allura/allura/model/session.py
+++ b/Allura/allura/model/session.py
@@ -17,6 +17,7 @@
 
 import logging
 import pymongo
+from collections import defaultdict
 
 from ming import Session
 from ming.orm.base import state
@@ -53,23 +54,39 @@ class ManagedSessionExtension(SessionExtension):
 
 
 class IndexerSessionExtension(ManagedSessionExtension):
-    task_add = None
-    task_del = None
 
-    def after_flush(self, obj=None):
-        if (self.task_add or self.task_del) is None:
-            raise Exception('Both "task_add" and "task_del" must be defined')
+    TASKS = {
+        'allura.model.project.Project': {'add': index_tasks.add_projects,
+                                         'del': index_tasks.del_projects}
+    }
+
+    def _objects_by_types(self, obj_list):
+        result = defaultdict(list)
+        for obj in obj_list:
+            class_path = '%s.%s' % (type(obj).__module__, type(obj).__name__)
+            result[class_path].append(obj)
+        return result
 
-        modified = self.objects_added + self.objects_modified
-        if modified:
-            self.task_add.post([o._id for o in modified])
-        if self.objects_deleted:
-            self.task_del.post([o.index_id() for o in self.objects_deleted])
+    def _add_to_index(self, tasks, obj_list):
+        task = tasks.get('add')
+        if task: task.post([o._id for o in obj_list])
 
+    def _del_from_index(self, tasks, obj_list):
+        task = tasks.get('del')
+        if task: task.post([o.index_id() for o in obj_list])
 
-class ProjectIndexerSessionExtension(IndexerSessionExtension):
-    task_add = index_tasks.add_projects
-    tasd_del = index_tasks.del_projects
+    def after_flush(self, obj=None):
+        actions = [
+            ((self.objects_added + self.objects_modified), self._add_to_index),
+            ((self.objects_deleted), self._del_from_index)
+        ]
+        for obj_list, action in actions:
+            if obj_list:
+                types_objects_map = self._objects_by_types(obj_list)
+                for class_path, obj_list in types_objects_map.iteritems():
+                    tasks = self.TASKS.get(class_path, {})
+                    action(tasks, obj_list)
+                        
 
 
 class ArtifactSessionExtension(ManagedSessionExtension):
@@ -216,10 +233,14 @@ def substitute_extensions(session, extensions=None):
 main_doc_session = Session.by_name('main')
 project_doc_session = Session.by_name('project')
 task_doc_session = Session.by_name('task')
-main_orm_session = ThreadLocalORMSession(main_doc_session)
+main_orm_session = ThreadLocalORMSession(
+    doc_session=main_doc_session,
+    extensions=[IndexerSessionExtension]
+    )
 project_orm_session = ThreadLocalORMSession(
     doc_session=project_doc_session,
-    extensions=[ProjectIndexerSessionExtension])
+    extensions=[IndexerSessionExtension]
+)
 task_orm_session = ThreadLocalORMSession(task_doc_session)
 artifact_orm_session = ThreadLocalORMSession(
     doc_session=project_doc_session,

http://git-wip-us.apache.org/repos/asf/allura/blob/f2cdeed8/Allura/allura/tests/unit/test_session.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/test_session.py b/Allura/allura/tests/unit/test_session.py
index 649dd4a..29a66ca 100644
--- a/Allura/allura/tests/unit/test_session.py
+++ b/Allura/allura/tests/unit/test_session.py
@@ -75,8 +75,9 @@ class TestIndexerSessionExtension(TestCase):
         session = mock.Mock()
         self.ExtensionClass = IndexerSessionExtension
         self.extension = self.ExtensionClass(session)
-        self.extension.task_add = mock.Mock()
-        self.extension.task_del = mock.Mock()
+        self.tasks = {'add': mock.Mock(), 'del': mock.Mock()}
+        self.extension.TASKS = mock.Mock()
+        self.extension.TASKS.get.return_value = self.tasks
 
     def _mock_indexable(self, **kw):
         m = mock.Mock(**kw)
@@ -91,8 +92,8 @@ class TestIndexerSessionExtension(TestCase):
         self.extension.objects_modified = modified
         self.extension.objects_deleted = deleted
         self.extension.after_flush()
-        self.extension.task_add.post.assert_called_once_with([1, 2, 3, 4, 5])
-        self.extension.task_del.post.assert_called_once_with(map(id, deleted))
+        self.tasks['add'].post.assert_called_once_with([1, 2, 3, 4, 5])
+        self.tasks['del'].post.assert_called_once_with(map(id, deleted))
 
 
 class TestBatchIndexer(TestCase):