changeset 29:e2585a9da30a

Added support for redirects between wiki pages.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 05 Jan 2013 14:07:32 -0800
parents 1b72f32b19a7
children 420ff74c2e28
files wikked/static/js/wikked/app.js wikked/static/js/wikked/models.js wikked/static/tpl/read-page.html wikked/wiki.py
diffstat 4 files changed, 60 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/static/js/wikked/app.js	Sat Jan 05 11:58:51 2013 -0800
+++ b/wikked/static/js/wikked/app.js	Sat Jan 05 14:07:32 2013 -0800
@@ -30,10 +30,15 @@
             'special/:page':         "showSpecialPage"
         },
         readPage: function(path) {
+            path_clean = this.stripQuery(path);
+            no_redirect = this.getQueryVariable('no_redirect', path);
             var view = new Views.PageReadView({ 
                 el: $('#app'), 
-                model: new Models.PageReadModel({ path: path })
+                model: new Models.PageReadModel({ path: path_clean })
             });
+            if (no_redirect) {
+                view.model.set('no_redirect', true);
+            }
             view.model.setApp(this);
             view.model.fetch();
             this.navigate('/read/' + path);
@@ -146,13 +151,30 @@
             view.model.fetch();
             this.navigate('/special/' + page);
         },
-        getQueryVariable: function(variable) {
-            var query = window.location.search.substring(1);
-            var vars = query.split("&");
+        stripQuery: function(url) {
+            q = url.indexOf("?");
+            if (q < 0)
+                return url;
+            return url.substring(0, q);
+        },
+        getQueryVariable: function(variable, url) {
+            if (url === undefined) {
+                url = window.location.search.substring(1);
+            } else {
+                q = url.indexOf("?");
+                if (q < 0)
+                    return false;
+                url = url.substring(q + 1);
+            }
+            var vars = url.split("&");
             for (var i = 0; i < vars.length; i++) {
                 var pair = vars[i].split("=");
                 if (pair[0] == variable) {
-                    return unescape(pair[1]);
+                    if (pair.length > 1) {
+                        return unescape(pair[1]);
+                    } else {
+                        return true;
+                    }
                 }
             }
             return false;
--- a/wikked/static/js/wikked/models.js	Sat Jan 05 11:58:51 2013 -0800
+++ b/wikked/static/js/wikked/models.js	Sat Jan 05 14:07:32 2013 -0800
@@ -188,6 +188,22 @@
     var PageReadModel = exports.PageReadModel = MasterPageModel.extend({
         urlRoot: '/api/read/',
         action: 'read',
+        initialize: function() {
+            PageReadModel.__super__.initialize.apply(this, arguments);
+            this.on('change', this._onChange, this);
+        },
+        _onChange: function() {
+            if (this.getMeta('redirect') && !this.get('no_redirect')) {
+                var oldPath = this.get('path');
+                this.set('path', this.getMeta('redirect'));
+                this.fetch({
+                    success: function(model) {
+                        model.set('redirected_from', oldPath);
+                    }
+                });
+                this.app.navigate('/read/' + this.getMeta('redirect'), { replace: true });
+            }
+        },
         _onChangePath: function(path) {
             PageReadModel.__super__._onChangePath.apply(this, arguments);
             this.footer.addExtraUrl('Pages Linking Here', '/#/inlinks/' + path, 1);
--- a/wikked/static/tpl/read-page.html	Sat Jan 05 11:58:51 2013 -0800
+++ b/wikked/static/tpl/read-page.html	Sat Jan 05 14:07:32 2013 -0800
@@ -3,6 +3,12 @@
         {{#ifnot meta.notitle}}
         <h1>{{meta.title}}</h1>
         {{/ifnot}}
+        {{#if redirected_from}}
+        <small>Redirected from <a href="/#/read/{{redirected_from}}?no_redirect">{{redirected_from}}</a></small>
+        {{/if}}
+        {{#if meta.redirect}}
+        <small>Redirects to <a href="/#/read/{{meta.redirect}}">{{meta.redirect}}</a></small>
+        {{/if}}
         {{content}}
     </div>
 </article>
--- a/wikked/wiki.py	Sat Jan 05 11:58:51 2013 -0800
+++ b/wikked/wiki.py	Sat Jan 05 14:07:32 2013 -0800
@@ -95,6 +95,15 @@
         self.url = url
         self._meta = None
 
+        self._promoted_meta = [
+                'title',
+                'redirect',
+                'notitle'
+                ]
+        self._coerce_promoted_meta = {
+                'redirect': Page.title_to_url
+                }
+
     @property
     def title(self):
         self._ensureMeta()
@@ -141,6 +150,8 @@
         for name in self._promoted_meta:
             if name in self._meta['user']:
                 meta[name] = self._meta['user'][name]
+                if name in self._coerce_promoted_meta:
+                    meta[name] = self._coerce_promoted_meta[name](meta[name])
         return meta
 
     def getHistory(self):
@@ -202,12 +213,6 @@
             self.wiki.logger.debug("Updated cached %s for page '%s'." % (cache_key, self.url))
             self.wiki.cache.write(cache_key, data)
 
-    _promoted_meta = [
-            'title',
-            'redirect',
-            'notitle'
-            ]
-
     @staticmethod
     def title_to_url(title):
         return re.sub(r'[^A-Za-z0-9_\.\-\(\)/]+', '-', title.lower())