changeset 67:048ef0681e42

Added ability to revert a page. Cleaned up some code to submit a page.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 12 Feb 2013 20:52:26 -0800
parents 16e7d101cf53
children 4cb946982fca
files wikked/wiki.py
diffstat 1 files changed, 44 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/wiki.py	Fri Feb 08 17:45:34 2013 -0800
+++ b/wikked/wiki.py	Tue Feb 12 20:52:26 2013 -0800
@@ -175,30 +175,61 @@
         """ Updates or creates a page for a given URL.
         """
         # Validate the parameters.
+        if 'text' not in page_fields:
+            raise ValueError(
+                    "No text specified for editing page '%s'." % url)
         if 'author' not in page_fields:
             raise ValueError(
-                "No author specified for editing page '%s'." % url)
+                    "No author specified for editing page '%s'." % url)
         if 'message' not in page_fields:
             raise ValueError(
-                "No commit message specified for editing page '%s'." % url)
+                    "No commit message specified for editing page '%s'." % url)
 
         # Save the new/modified text.
-        do_commit = False
         path = self.fs.getPhysicalPagePath(url)
-        if 'text' in page_fields:
-            self.fs.setPage(path, page_fields['text'])
-            do_commit = True
+        self.fs.setPage(path, page_fields['text'])
 
         # Commit the file to the source-control.
-        if do_commit:
-            commit_meta = {
-                    'author': page_fields['author'],
-                    'message': page_fields['message']
-                    }
-            self.scm.commit([path], commit_meta)
+        commit_meta = {
+                'author': page_fields['author'],
+                'message': page_fields['message']
+                }
+        self.scm.commit([path], commit_meta)
 
         # Update the DB and index with the new/modified page.
-        self.db.update([self.getPage(url)])
+        self.db.update([self.getPage(url, factory=Page.factory)])
+        self.index.update([self.getPage(url)])
+
+    def revertPage(self, url, page_fields):
+        """ Reverts the page with the given URL to an older revision.
+        """
+        # Validate the parameters.
+        if 'rev' not in page_fields:
+            raise ValueError(
+                    "No revision specified for reverting page '%s'." % url)
+        if 'author' not in page_fields:
+            raise ValueError(
+                    "No author specified for reverting page '%s'." % url)
+        if 'message' not in page_fields:
+            raise ValueError(
+                    "No commit message specified for reverting page '%s'." % url)
+
+        # Get the revision.
+        path = self.fs.getPhysicalPagePath(url)
+        rev_text = self.scm.getRevision(path, page_fields['rev'])
+
+        # Write to the file and commit.
+        self.fs.setPage(path, rev_text)
+
+        # Commit to source-control.
+        commit_meta = {
+                'author': page_fields['author'],
+                'message': page_fields['message']
+                }
+        self.scm.commit([path], commit_meta)
+
+        # Update the DB and index with the modified page.
+        self.db.update([self.getPage(url, factory=Page.factory)])
         self.index.update([self.getPage(url)])
 
     def pageExists(self, url, from_db=None):