changeset 230:29b3586181cb

simple item actions now use flatland based forms
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 15 May 2011 00:31:21 +0200
parents c956811fba9a
children 6f2dc5677639
files MoinMoin/apps/frontend/views.py MoinMoin/templates/copy.html MoinMoin/templates/delete.html MoinMoin/templates/destroy.html MoinMoin/templates/rename.html MoinMoin/templates/revert.html
diffstat 6 files changed, 157 insertions(+), 161 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Sat May 14 23:06:29 2011 +0200
+++ b/MoinMoin/apps/frontend/views.py	Sun May 15 00:31:21 2011 +0200
@@ -295,34 +295,51 @@
         return redirect(url_for('frontend.show_item', item_name=item_name))
 
 
+class CommentForm(TextChaizedForm):
+    comment = String.using(label=L_('Comment'), optional=True).with_properties(placeholder=L_("Comment about your change"))
+    submit = String.using(default=L_('OK'), optional=True)
+
+class TargetCommentForm(CommentForm):
+    target = String.using(label=L_('Target')).with_properties(placeholder=L_("The name of the target item")).validated_by(Present())
+
+class RevertItemForm(CommentForm):
+    name = 'revert_item'
+
+class DeleteItemForm(CommentForm):
+    name = 'delete_item'
+
+class DestroyItemForm(CommentForm):
+    name = 'destroy_item'
+
+class CopyItemForm(TargetCommentForm):
+    name = 'copy_item'
+
+class RenameItemForm(TargetCommentForm):
+    name = 'rename_item'
+
+
 @frontend.route('/+revert/<int:rev>/<itemname:item_name>', methods=['GET', 'POST'])
 def revert_item(item_name, rev):
     try:
         item = Item.create(item_name, rev_no=rev)
     except AccessDeniedError:
         abort(403)
-    form = TextChaizedForm.from_flat(request.form)
-    TextCha(form).amend_form()
     if request.method == 'GET':
-        return render_template(item.revert_template, rev_no=rev,
-                               item=item, item_name=item_name,
-                               form=form,
-                               gen=make_generator(),
-                              )
+        form = RevertItemForm.from_defaults()
+        TextCha(form).amend_form()
     elif request.method == 'POST':
-        if 'button_ok' in request.form:
-            valid = form.validate()
-            if not valid:
-                comment = request.values.get('comment')
-                return render_template(item.revert_template,
-                                       item=item, item_name=item_name,
-                                       rev_no=rev,
-                                       form=form,
-                                       gen=make_generator(),
-                                       comment=comment,
-                                      )
+        form = RevertItemForm.from_flat(request.form)
+        TextCha(form).amend_form()
+        valid = form.validate()
+        if valid:
             item.revert()
-        return redirect(url_for('frontend.show_item', item_name=item_name))
+            return redirect(url_for('frontend.show_item', item_name=item_name))
+    return render_template(item.revert_template,
+                           item=item, item_name=item_name,
+                           rev_no=rev,
+                           form=form,
+                           gen=make_generator(),
+                          )
 
 
 @frontend.route('/+copy/<itemname:item_name>', methods=['GET', 'POST'])
@@ -331,31 +348,24 @@
         item = Item.create(item_name)
     except AccessDeniedError:
         abort(403)
-    form = TextChaizedForm.from_flat(request.form)
-    TextCha(form).amend_form()
     if request.method == 'GET':
-        return render_template(item.copy_template,
-                               item=item, item_name=item_name,
-                               form=form, target=item.name,
-                               gen=make_generator(),
-                              )
-    if request.method == 'POST':
-        if 'button_ok' in request.form:
-            target = request.form.get('target')
-            comment = request.form.get('comment')
-            valid = form.validate()
-            if not valid:
-                return render_template(item.copy_template,
-                                       item=item, item_name=item_name,
-                                       form=form, target=target,
-                                       gen=make_generator(),
-                                       comment=comment,
-                                      )
+        form = CopyItemForm.from_defaults()
+        TextCha(form).amend_form()
+        form['target'] = item.name
+    elif request.method == 'POST':
+        form = CopyItemForm.from_flat(request.form)
+        TextCha(form).amend_form()
+        valid = form.validate()
+        if valid:
+            target = form['target'].value
+            comment = form['comment'].value
             item.copy(target, comment)
-            redirect_to = target
-        else:
-            redirect_to = item_name
-        return redirect(url_for('frontend.show_item', item_name=redirect_to))
+            return redirect(url_for('frontend.show_item', item_name=target))
+    return render_template(item.copy_template,
+                           item=item, item_name=item_name,
+                           form=form,
+                           gen=make_generator(),
+                          )
 
 
 @frontend.route('/+rename/<itemname:item_name>', methods=['GET', 'POST'])
@@ -364,31 +374,24 @@
         item = Item.create(item_name)
     except AccessDeniedError:
         abort(403)
-    form = TextChaizedForm.from_flat(request.form)
-    TextCha(form).amend_form()
     if request.method == 'GET':
-        return render_template(item.rename_template,
-                               item=item, item_name=item_name,
-                               form=form,
-                               gen=make_generator(),
-                              )
-    if request.method == 'POST':
-        if 'button_ok' in request.form:
-            target = request.form.get('target')
-            comment = request.form.get('comment')
-            valid = form.validate()
-            if not valid:
-                return render_template(item.rename_template,
-                                       item=item, item_name=item_name,
-                                       form=form,
-                                       gen=make_generator(),
-                                       comment=comment,
-                                      )
+        form = RenameItemForm.from_defaults()
+        TextCha(form).amend_form()
+        form['target'] = item.name
+    elif request.method == 'POST':
+        form = RenameItemForm.from_flat(request.form)
+        TextCha(form).amend_form()
+        valid = form.validate()
+        if valid:
+            target = form['target'].value
+            comment = form['comment'].value
             item.rename(target, comment)
-            redirect_to = target
-        else:
-            redirect_to = item_name
-        return redirect(url_for('frontend.show_item', item_name=redirect_to))
+            return redirect(url_for('frontend.show_item', item_name=target))
+    return render_template(item.rename_template,
+                           item=item, item_name=item_name,
+                           form=form,
+                           gen=make_generator(),
+                          )
 
 
 @frontend.route('/+delete/<itemname:item_name>', methods=['GET', 'POST'])
@@ -397,28 +400,22 @@
         item = Item.create(item_name)
     except AccessDeniedError:
         abort(403)
-    form = TextChaizedForm.from_flat(request.form)
-    TextCha(form).amend_form()
     if request.method == 'GET':
-        return render_template(item.delete_template,
-                               item=item, item_name=item_name,
-                               form=form,
-                               gen=make_generator(),
-                              )
+        form = DeleteItemForm.from_defaults()
+        TextCha(form).amend_form()
     elif request.method == 'POST':
-        if 'button_ok' in request.form:
-            valid = form.validate()
-            if not valid:
-                comment = request.values.get('comment')
-                return render_template(item.delete_template,
-                                       item=item, item_name=item_name,
-                                       form=form,
-                                       gen=make_generator(),
-                                       comment=comment,
-                                      )
-            comment = request.form.get('comment')
+        form = DeleteItemForm.from_flat(request.form)
+        TextCha(form).amend_form()
+        valid = form.validate()
+        if valid:
+            comment = form['comment'].value
             item.delete(comment)
-        return redirect(url_for('frontend.show_item', item_name=item_name))
+            return redirect(url_for('frontend.show_item', item_name=item_name))
+    return render_template(item.delete_template,
+                           item=item, item_name=item_name,
+                           form=form,
+                           gen=make_generator(),
+                          )
 
 
 @frontend.route('/+destroy/<int:rev>/<itemname:item_name>', methods=['GET', 'POST'])
@@ -435,29 +432,23 @@
         item = Item.create(item_name, rev_no=_rev)
     except AccessDeniedError:
         abort(403)
-    form = TextChaizedForm.from_flat(request.form)
-    TextCha(form).amend_form()
     if request.method == 'GET':
-        return render_template(item.destroy_template,
-                               item=item, item_name=item_name,
-                               rev_no=rev,
-                               form=form,
-                               gen=make_generator(),
-                              )
-    if request.method == 'POST':
-        if 'button_ok' in request.form:
-            comment = request.form.get('comment')
-            valid = form.validate()
-            if not valid:
-                return render_template(item.destroy_template,
-                                       item=item, item_name=item_name,
-                                       rev_no=rev,
-                                       form=form,
-                                       gen=make_generator(),
-                                       comment=comment,
-                                      )
+        form = DestroyItemForm.from_defaults()
+        TextCha(form).amend_form()
+    elif request.method == 'POST':
+        form = DestroyItemForm.from_flat(request.form)
+        TextCha(form).amend_form()
+        valid = form.validate()
+        if valid:
+            comment = form['comment'].value
             item.destroy(comment=comment, destroy_item=destroy_item)
-        return redirect(url_for('frontend.show_item', item_name=item_name))
+            return redirect(url_for('frontend.show_item', item_name=item_name))
+    return render_template(item.destroy_template,
+                           item=item, item_name=item_name,
+                           rev_no=rev,
+                           form=form,
+                           gen=make_generator(),
+                          )
 
 
 # XXX this has some functional redundancy with "index", solve that later
--- a/MoinMoin/templates/copy.html	Sat May 14 23:06:29 2011 +0200
+++ b/MoinMoin/templates/copy.html	Sun May 15 00:31:21 2011 +0200
@@ -3,19 +3,17 @@
 {% block content %}
 <h1>{{ _("Copy '%(item_name)s'", item_name=item.name) }}</h1>
 <div class="moin-form">
-<form action="{{ url_for('frontend.copy_item', item_name=item.name) }}" method="POST" enctype="multipart/form-data">
-    <dl>
-        <dt><label for="target">{{ _("Target") }}</label></dt>
-        <dd><input type="text" id="target" name="target" size="80" maxlength="200" value="{{ target }}" /></dd>
-        
-        <dt><label for="comment">{{ _("Comment") }}</label></dt>
-        <dd><input type="text" id="comment" name="comment" size="80" maxlength="200" value="{{ comment }}" /></dd>
-    </dl>
-
-    <input class="button" type="submit" name="button_ok" value="{{ _('Copy') }}" />
-    <input class="button" type="submit" name="button_cancel" value="{{ _('Cancel') }}" />
-    <br/>
+{{ gen.form.open(form, method="post", action=url_for('frontend.copy_item', item_name=item.name)) }}
+  {{ forms.render_errors(form) }}
+  <dl>
+    {{ forms.render_field(gen, form['target'], 'text') }}
+    {{ forms.render_field(gen, form['comment'], 'text') }}
     {{ forms.render_textcha(gen, form) }}
-</form>
+  </dl>
+  {{ gen.input(form['submit'], type='submit') }}
+{{ gen.form.close() }}
 </div>
 {% endblock %}
+
+
+
--- a/MoinMoin/templates/delete.html	Sat May 14 23:06:29 2011 +0200
+++ b/MoinMoin/templates/delete.html	Sun May 15 00:31:21 2011 +0200
@@ -1,17 +1,18 @@
 {% import "forms.html" as forms %}
 {% extends theme("layout.html") %}
 {% block content %}
-<div class="moin-form">
 <h1>{{ _("Delete '%(item_name)s'", item_name=item.name) }}</h1>
-<form action="{{ url_for('frontend.delete_item', item_name=item.name) }}" method="POST" enctype="multipart/form-data">
-    <dl>
-        <dt><label for="comment">{{ _("Comment") }}</label></dt>
-        <dd><input type="text" id="comment" name="comment" size="80" maxlength="200" value="" /></dd>
-    </dl>
-    <input class="button" type="submit" name="button_ok" value="{{ _('Delete') }}" />
-    <input class="button" type="submit" name="button_cancel" value="{{ _('Cancel') }}" />
-    <br />
+<div class="moin-form">
+{{ gen.form.open(form, method="post", action=url_for('frontend.delete_item', item_name=item.name)) }}
+  {{ forms.render_errors(form) }}
+  <dl>
+    {{ forms.render_field(gen, form['comment'], 'text') }}
     {{ forms.render_textcha(gen, form) }}
-</form>
+  </dl>
+  {{ gen.input(form['submit'], type='submit') }}
+{{ gen.form.close() }}
 </div>
 {% endblock %}
+
+
+
--- a/MoinMoin/templates/destroy.html	Sat May 14 23:06:29 2011 +0200
+++ b/MoinMoin/templates/destroy.html	Sun May 15 00:31:21 2011 +0200
@@ -4,20 +4,31 @@
 {% if rev_no == None %}
     <h1>{{ _("DESTROY COMPLETE item '%(item_name)s'", item_name=item.name) }}</h1>
     <div class="moin-form">
-    <form action="{{ url_for('frontend.destroy_item', item_name=item.name) }}" method="POST" enctype="multipart/form-data">
+    {{ gen.form.open(form, method="post", action=url_for('frontend.destroy_item', item_name=item.name)) }}
+      {{ forms.render_errors(form) }}
+      <dl>
+        {{ forms.render_field(gen, form['comment'], 'text') }}
+        {{ forms.render_textcha(gen, form) }}
+      </dl>
+      {{ gen.input(form['submit'], type='submit') }}
+    {{ gen.form.close() }}
+    </div>
 {% else %}
     <h1>{{ _("DESTROY REVISION '%(item_name)s' (rev %(rev_no)d)", item_name=item.name, rev_no=rev_no) }}</h1>
     <div class="moin-form">
-    <form action="{{ url_for('frontend.destroy_item', item_name=item.name, rev=rev_no) }}" method="POST" enctype="multipart/form-data">
+    {{ gen.form.open(form, method="post", action=url_for('frontend.destroy_item', item_name=item.name, rev=rev_no)) }}
+      {{ forms.render_errors(form) }}
+      <dl>
+        {{ forms.render_field(gen, form['comment'], 'text') }}
+        {{ forms.render_textcha(gen, form) }}
+      </dl>
+      {{ gen.input(form['submit'], type='submit') }}
+    {{ gen.form.close() }}
+    </div>
 {% endif %}
-    <dl>
-        <dt><label for="comment">{{ _("Comment") }}</label></dt>
-        <dd><input type="text" id="comment" name="comment" size="80" maxlength="200" value="{{ comment }}" /></dd>
-    </dl>
-    <br/>
-    {{ forms.render_textcha(gen, form) }}
-    <input class="button" type="submit" name="button_ok" value="{{ _('DESTROY') }}" />
-    <input class="button" type="submit" name="button_cancel" value="{{ _('Cancel') }}" />
-</form>
-</div>
 {% endblock %}
+
+
+
+
+
--- a/MoinMoin/templates/rename.html	Sat May 14 23:06:29 2011 +0200
+++ b/MoinMoin/templates/rename.html	Sun May 15 00:31:21 2011 +0200
@@ -3,18 +3,14 @@
 {% block content %}
 <h1>{{ _("Rename '%(item_name)s'", item_name=item.name) }}</h1>
 <div class="moin-form">
-<form action="{{ url_for('frontend.rename_item', item_name=item.name) }}" method="POST" enctype="multipart/form-data">
-    <dl>
-        <dt><label for="target">{{ _("Target") }}</label></dt>
-        <dd><input type="text" id="target" name="target" size="80" maxlength="200" value="{{ item.name }}" /></dd>
-
-        <dt><label for="comment">{{ _("Comment") }}</label></dt>
-        <dd><input type="text" id="comment" name="comment" size="80" maxlength="200" value="{{ comment }}" /></dd>
-    </dl>
-    <input class="button" type="submit" name="button_ok" value="{{ _('Rename') }}" />
-    <input class="button" type="submit" name="button_cancel" value="{{ _('Cancel') }}" />
-    <br />
+{{ gen.form.open(form, method="post", action=url_for('frontend.rename_item', item_name=item.name)) }}
+  {{ forms.render_errors(form) }}
+  <dl>
+    {{ forms.render_field(gen, form['target'], 'text') }}
+    {{ forms.render_field(gen, form['comment'], 'text') }}
     {{ forms.render_textcha(gen, form) }}
-</form>
+  </dl>
+  {{ gen.input(form['submit'], type='submit') }}
+{{ gen.form.close() }}
 </div>
 {% endblock %}
--- a/MoinMoin/templates/revert.html	Sat May 14 23:06:29 2011 +0200
+++ b/MoinMoin/templates/revert.html	Sun May 15 00:31:21 2011 +0200
@@ -3,15 +3,14 @@
 {% block content %}
 <h1>{{ _("Revert '%(item_name)s' (rev %(rev_no)d)", item_name=item.name, rev_no=rev_no) }}</h1>
 <div class="moin-form">
-<form action="{{ url_for('frontend.revert_item', item_name=item.name, rev=rev_no) }}" method="POST" enctype="multipart/form-data">
-    <dl>
-        <dt><label for="comment">{{ _("Comment") }}</label></dt>
-        <dd><input type="text" id="comment" name="comment" size="80" maxlength="200" value="{{ comment }}" /></dd>
-    </dl>
+{{ gen.form.open(form, method="post", action=url_for('frontend.revert_item', item_name=item.name, rev=rev_no)) }}
+  {{ forms.render_errors(form) }}
+  <dl>
+    {{ forms.render_field(gen, form['comment'], 'text') }}
     {{ forms.render_textcha(gen, form) }}
-    <br />
-    <input class="button" type="submit" name="button_ok" value="{{ _('Revert') }}" />
-    <input class="button" type="submit" name="button_cancel" value="{{ _('Cancel') }}" />
-</form>
+  </dl>
+  {{ gen.input(form['submit'], type='submit') }}
+{{ gen.form.close() }}
 </div>
 {% endblock %}
+