trac-mq / t11148 / t11148_r11784_IEntityListener_compat_wiki.diff

# HG changeset patch
# Parent b40d09fa09786188ad2a4aa37c92705b1a50fdc8
Trac #11148 : 'IWikiChangeListener' notifications powered by 'ListenerNotifier.notify' [ok]

diff -r b40d09fa0978 trac/wiki/api.py
--- a/trac/wiki/api.py	Sat Apr 27 23:56:47 2013 -0500
+++ b/trac/wiki/api.py	Sun Apr 28 00:00:24 2013 -0500
@@ -36,6 +36,8 @@
     interface.
     """
 
+    _entity_listener_prefix = 'wiki_page'
+
     def wiki_page_added(page):
         """Called whenever a new Wiki page is added."""
 
diff -r b40d09fa0978 trac/wiki/model.py
--- a/trac/wiki/model.py	Sat Apr 27 23:56:47 2013 -0500
+++ b/trac/wiki/model.py	Sun Apr 28 00:00:24 2013 -0500
@@ -21,10 +21,11 @@
 from datetime import datetime
 
 from trac.core import *
+from trac.core import ListenerNotifier
 from trac.resource import Resource
 from trac.util.datefmt import from_utimestamp, to_utimestamp, utc
 from trac.util.translation import _
-from trac.wiki.api import WikiSystem, validate_page_name
+from trac.wiki.api import WikiSystem, validate_page_name, IWikiChangeListener
 
 
 class WikiPage(object):
@@ -110,12 +111,18 @@
 
         # Let change listeners know about the deletion
         if not self.exists:
-            for listener in WikiSystem(self.env).change_listeners:
-                listener.wiki_page_deleted(self)
+#            for listener in WikiSystem(self.env).change_listeners:
+#                listener.wiki_page_deleted(self)
+            ListenerNotifier(self.env).notify(
+                             IWikiChangeListener.wiki_page_deleted,
+                             entity=self)
         else:
-            for listener in WikiSystem(self.env).change_listeners:
-                if hasattr(listener, 'wiki_page_version_deleted'):
-                    listener.wiki_page_version_deleted(self)
+#            for listener in WikiSystem(self.env).change_listeners:
+#                if hasattr(listener, 'wiki_page_version_deleted'):
+#                    listener.wiki_page_version_deleted(self)
+            ListenerNotifier(self.env).notify(
+                             IWikiChangeListener.wiki_page_version_deleted,
+                             entity=self)
 
     def save(self, author, comment, remote_addr, t=None, db=None):
         """Save a new version of a page.
@@ -153,12 +160,22 @@
         self.comment = comment
         self.time = t
 
-        for listener in WikiSystem(self.env).change_listeners:
-            if self.version == 1:
-                listener.wiki_page_added(self)
-            else:
-                listener.wiki_page_changed(self, self.version, t, comment,
-                                           author, remote_addr)
+#        for listener in WikiSystem(self.env).change_listeners:
+#            if self.version == 1:
+#                listener.wiki_page_added(self)
+#            else:
+#                listener.wiki_page_changed(self, self.version, t, comment,
+#                                           author, remote_addr)
+        if self.version == 1:
+            ListenerNotifier(self.env).notify(
+                             IWikiChangeListener.wiki_page_added,
+                             entity=self)
+        else:
+            ListenerNotifier(self.env).notify(
+                             IWikiChangeListener.wiki_page_changed,
+                             entity=self, version=self.version, 
+                             t=t, comment=comment, author=author, 
+                             ipnr=remote_addr)
 
         self.old_readonly = self.readonly
         self.old_text = self.text
@@ -192,9 +209,12 @@
         self.name = self.resource.id = new_name
         self.env.log.info('Renamed page %s to %s', old_name, new_name)
 
-        for listener in WikiSystem(self.env).change_listeners:
-            if hasattr(listener, 'wiki_page_renamed'):
-                listener.wiki_page_renamed(self, old_name)
+#        for listener in WikiSystem(self.env).change_listeners:
+#            if hasattr(listener, 'wiki_page_renamed'):
+#                listener.wiki_page_renamed(self, old_name)
+        ListenerNotifier(self.env).notify(
+                         IWikiChangeListener.wiki_page_renamed,
+                         entity=self, old_name=old_name)
 
     def get_history(self, db=None):
         """Retrieve the edit history of a wiki page.
diff -r b40d09fa0978 trac/wiki/tests/model.py
--- a/trac/wiki/tests/model.py	Sat Apr 27 23:56:47 2013 -0500
+++ b/trac/wiki/tests/model.py	Sun Apr 28 00:00:24 2013 -0500
@@ -12,6 +12,7 @@
 from trac.attachment import Attachment
 from trac.core import *
 from trac.test import EnvironmentStub
+from trac.tests.core import GenericEntitiesChangeListenerMock
 from trac.util.datefmt import utc, to_utimestamp
 from trac.wiki import WikiPage, IWikiChangeListener
 
@@ -115,6 +116,10 @@
         listener = TestWikiChangeListener(self.env)
         self.assertEqual(page, listener.added[0])
 
+        generic_listener = GenericEntitiesChangeListenerMock(self.env)
+        self.assertEqual([{'action' : 'added', 'entity' : page}], 
+                         generic_listener.details)
+
     def test_update_page(self):
         t = datetime(2001, 1, 1, 1, 1, 1, 0, utc)
         t2 = datetime(2002, 1, 1, 1, 1, 1, 0, utc)
@@ -149,6 +154,13 @@
         self.assertEqual((page, 2, t2, 'Changing', 'kate', '192.168.0.101'),
                          listener.changed[0])
 
+        generic_listener = GenericEntitiesChangeListenerMock(self.env)
+        self.assertEqual([{'action' : 'changed', 'entity' : page,
+                           'version' : 2, 't' : t2 , 'comment' : 'Changing',
+                           'author' : 'kate', 'ipnr' : '192.168.0.101',
+                            'old_values': None}], 
+                         generic_listener.details)
+
         page = WikiPage(self.env, 'TestPage')
         history = list(page.get_history())
         self.assertEqual(2, len(history))
@@ -174,6 +186,10 @@
         listener = TestWikiChangeListener(self.env)
         self.assertEqual(page, listener.deleted[0])
 
+        generic_listener = GenericEntitiesChangeListenerMock(self.env)
+        self.assertEqual([{'action' : 'deleted', 'entity' : page}], 
+                         generic_listener.details)
+
     def test_delete_page_version(self):
         self.env.db_transaction.executemany(
             "INSERT INTO wiki VALUES(%s,%s,%s,%s,%s,%s,%s,%s)",
@@ -194,6 +210,10 @@
         listener = TestWikiChangeListener(self.env)
         self.assertEqual(page, listener.deleted_version[0])
 
+        generic_listener = GenericEntitiesChangeListenerMock(self.env)
+        self.assertEqual([{'action' : 'version_deleted', 'entity' : page}], 
+                         generic_listener.details)
+
     def test_delete_page_last_version(self):
         self.env.db_transaction(
             "INSERT INTO wiki VALUES(%s,%s,%s,%s,%s,%s,%s,%s)",
@@ -212,6 +232,10 @@
         listener = TestWikiChangeListener(self.env)
         self.assertEqual(page, listener.deleted[0])
 
+        generic_listener = GenericEntitiesChangeListenerMock(self.env)
+        self.assertEqual([{'action' : 'deleted', 'entity' : page}], 
+                         generic_listener.details)
+
     def test_rename_page(self):
         data = (1, 42, 'joe', '::1', 'Bla bla', 'Testing', 0)
         self.env.db_transaction(
@@ -247,6 +271,33 @@
         listener = TestWikiChangeListener(self.env)
         self.assertEqual((page, 'TestPage'), listener.renamed[0])
 
+        generic_listener = GenericEntitiesChangeListenerMock(self.env)
+        self.assertEqual({'action' : 'added', 'entity' : attachment},
+                         generic_listener.details[0])
+
+        from trac.resource import Resource
+
+        # Match everything but attachment
+        event_args = generic_listener.details[1]
+        expected_resource = Resource('wiki', 'PageRenamed').child('attachment',
+                                                                  'foo.txt')
+        resource = event_args.pop('entity').resource
+        self.assertEqual({'action' : 'reparented', 'old_parent_realm' : 'wiki', 
+                          'old_parent_id' : 'TestPage'}, 
+                         event_args)
+        self.assertEqual(expected_resource, resource)
+
+        self.assertEqual({'action' : 'renamed', 'entity' : page,
+                          'old_name' : 'TestPage'},
+                         generic_listener.details[2])
+
+        # Match everything but attachment
+        event_args = generic_listener.details[3]
+        resource = event_args.pop('entity').resource
+        self.assertEqual({'action' : 'deleted'}, event_args)
+        self.assertEqual(expected_resource, resource)
+
+
     def test_invalid_page_name(self):
         invalid_names = ('../Page', 'Page/..', 'Page/////SubPage',
                          'Page/./SubPage', '/PagePrefix', 'PageSuffix/')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.