changeset 1662:465b22852fae

Let Item subclasses handle +show.
author Cheer Xiao <xiaqqaix@gmail.com>
date Sun, 12 Aug 2012 15:14:07 +0800
parents 64fe4e3c449b
children 28fcf53d11cd
files MoinMoin/apps/frontend/views.py MoinMoin/items/__init__.py MoinMoin/templates/modify_select_itemtype.html MoinMoin/templates/show_nonexistent.html
diffstat 4 files changed, 41 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Thu Aug 09 22:54:37 2012 +0800
+++ b/MoinMoin/apps/frontend/views.py	Sun Aug 12 15:14:07 2012 +0800
@@ -344,7 +344,9 @@
     return partial(add_presenter, view=view, add_trail=add_trail, abort404=abort404)
 
 
-@frontend.route('/<itemname:item_name>', defaults=dict(rev=CURRENT), methods=['GET'])
+# The first form accepts POST to allow modifying behavior like modify_item.
+# The second form only accpets GET since modifying a historical revision is not allowed (yet).
+@frontend.route('/<itemname:item_name>', defaults=dict(rev=CURRENT), methods=['GET', 'POST'])
 @frontend.route('/+show/+<rev>/<itemname:item_name>', methods=['GET'])
 def show_item(item_name, rev):
     flaskg.user.add_trail(item_name)
@@ -354,24 +356,7 @@
         item = Item.create(item_name, rev_id=rev)
     except AccessDenied:
         abort(403)
-    show_revision = rev != CURRENT
-    show_navigation = False # TODO
-    first_rev = last_rev = None # TODO
-    if isinstance(item, NonExistent):
-        status = 404
-    else:
-        status = 200
-    content = render_template('show.html',
-                              item=item, item_name=item.name,
-                              rev=item.rev,
-                              contenttype=item.contenttype,
-                              first_rev_id=first_rev,
-                              last_rev_id=last_rev,
-                              data_rendered=Markup(item.content._render_data()),
-                              show_revision=show_revision,
-                              show_navigation=show_navigation,
-                             )
-    return Response(content, status)
+    return item.do_show(rev)
 
 
 @frontend.route('/+show/<itemname:item_name>')
--- a/MoinMoin/items/__init__.py	Thu Aug 09 22:54:37 2012 +0800
+++ b/MoinMoin/items/__init__.py	Sun Aug 12 15:14:07 2012 +0800
@@ -27,6 +27,8 @@
 from flatland import Form
 from flatland.validation import Validator
 
+from jinja2 import Markup
+
 from whoosh.query import Term, And, Prefix
 
 from MoinMoin.forms import RequiredText, OptionalText, JSON, Tags, Submit
@@ -562,6 +564,21 @@
                                data_rendered='',
                                )
 
+    def do_show(self, revid):
+        show_revision = revid != CURRENT
+        show_navigation = False # TODO
+        first_rev = last_rev = None # TODO
+        return render_template(self.show_template,
+                               item=self, item_name=self.name,
+                               rev=self.rev,
+                               contenttype=self.contenttype,
+                               first_rev_id=first_rev,
+                               last_rev_id=last_rev,
+                               data_rendered=Markup(self.content._render_data()),
+                               show_revision=show_revision,
+                               show_navigation=show_navigation,
+                              )
+
     def do_modify(self):
         method = request.method
         if method == 'GET':
@@ -607,6 +624,7 @@
                                search_form=None,
                               )
 
+    show_template = 'show.html'
     modify_template = 'modify.html'
 
 
@@ -638,11 +656,21 @@
     def _convert(self, doc):
         abort(404)
 
+    def do_show(self, revid):
+        # First, check if the current user has the required privileges
+        if not flaskg.user.may.create(self.name):
+            return render_template('show_nonexistent.html',
+                                   item_name=self.name,
+                                  )
+        return Response(self._select_itemtype(), 404)
+
     def do_modify(self):
         # First, check if the current user has the required privileges
         if not flaskg.user.may.create(self.name):
             abort(403)
+        return self._select_itemtype()
 
+    def _select_itemtype(self):
         # TODO Construct this list from the item_registry. Two more fields (ie.
         # display name and description) are needed in the registry then to
         # support the automatic construction.
--- a/MoinMoin/templates/modify_select_itemtype.html	Thu Aug 09 22:54:37 2012 +0800
+++ b/MoinMoin/templates/modify_select_itemtype.html	Sun Aug 12 15:14:07 2012 +0800
@@ -1,8 +1,8 @@
 {% extends theme("layout.html") %}
 {% block content %}
-<h1>{{ _("Create new item?") }}</h1>
+<h1>{{ _("Item not found, create it now?") }}</h1>
 <p>
-{{ _("This item does not exist (yet), but you can try creating it now. Please select the type of the item you want to create.") }}
+{{ _("Item '%(name)s' does not exist (yet), but you can try creating it now. Please select the type of the item you want to create.", name=item_name) }}
 </p>
 <table class="zebra">
     {% for itname, itlabel, itdesc in itemtypes %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/templates/show_nonexistent.html	Sun Aug 12 15:14:07 2012 +0800
@@ -0,0 +1,7 @@
+{% extends theme("layout.html") %}
+{% block content %}
+<h1>{{ _("Item not found") }}</h1>
+<p>
+{{ _("Item '%(name)s' does not exist.", name=item_name) }}
+</p>
+{% endblock %}