]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Add wrapper to send emails to watchers
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Dec 2018 22:31:14 +0000 (22:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 3 Dec 2018 22:31:14 +0000 (22:31 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/wiki.py

index 9fc79f52cdbfa95ed303dc31661a4dfce4b05313..5e058ff62d9080360da7017cde58bb1248d17419 100644 (file)
@@ -60,9 +60,15 @@ class Wiki(misc.Object):
        def create_page(self, page, author, content, changes=None, address=None):
                page = Page.sanitise_page_name(page)
 
-               return self._get_page("INSERT INTO wiki(page, author_uid, markdown, changes, address) \
+               # Write page to the database
+               page = self._get_page("INSERT INTO wiki(page, author_uid, markdown, changes, address) \
                        VALUES(%s, %s, %s, %s, %s) RETURNING *", page, author.uid, content or None, changes, address)
 
+               # Send email to all watchers
+               page._send_watcher_emails(excludes=[author])
+
+               return page
+
        def delete_page(self, page, author, **kwargs):
                # Do nothing if the page does not exist
                if not self.get_page(page):
@@ -360,6 +366,20 @@ class Page(misc.Object):
 
        # Watchers
 
+       @property
+       def watchers(self):
+               res = self.db.query("SELECT uid FROM wiki_watchlist \
+                       WHERE page = %s", self.page)
+
+               for row in res:
+                       # Search for account by UID and skip if none was found
+                       account = self.backend.accounts.get_by_uid(row.uid)
+                       if not account:
+                               continue
+
+                       # Return the account
+                       yield account
+
        def is_watched_by(self, account):
                res = self.db.get("SELECT 1 FROM wiki_watchlist \
                        WHERE page = %s AND uid = %s", self.page, account.uid)
@@ -380,6 +400,21 @@ class Page(misc.Object):
                self.db.execute("DELETE FROM wiki_watchlist \
                        WHERE page = %s AND uid = %s", self.page, account.uid)
 
+       def _send_watcher_emails(self, excludes=[]):
+               # Nothing to do if there was no previous revision
+               if not self.previous_revision:
+                       return
+
+               for watcher in self.watchers:
+                       # Skip everyone who is excluded
+                       if watcher in excludes:
+                               logging.debug("Excluding %s" % watcher)
+                               continue
+
+                       logging.debug("Sending watcher email to %s" % watcher)
+
+                       pass # TODO
+
 
 class File(misc.Object):
        def init(self, id, data):