From: Michael Tremer Date: Mon, 3 Dec 2018 22:31:14 +0000 (+0000) Subject: wiki: Add wrapper to send emails to watchers X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aba5e58a5be5900c67f37c10e2b3623c72a369a3;p=ipfire.org.git wiki: Add wrapper to send emails to watchers Signed-off-by: Michael Tremer --- diff --git a/src/backend/wiki.py b/src/backend/wiki.py index 9fc79f52..5e058ff6 100644 --- a/src/backend/wiki.py +++ b/src/backend/wiki.py @@ -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):