Permalink
Browse files

Enable edit mode for examples.

  • Loading branch information...
1 parent 33173e1 commit 0be674ee0548c507ed5261a65403435d1113caae @kamalgill committed Jan 9, 2013
@@ -1,4 +1,8 @@
/* Main stylesheet */
-/* Your customizations here */
+/* @section forms */
+.btn.cancel { margin-left: 0.5rem; }
+.nowrap { white-space: nowrap; }
+
+/* Your customizations here */
@@ -0,0 +1,44 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <h3>Edit Example</h3>
+ <form id="edit-example" class="well form-horizontal"
+ action="{{ url_for('edit_example', example_id=example.key.id()) }}" method="post" accept-charset="utf-8">
+ <fieldset>
+ {{ form.hidden_tag() }}
+ <input type="hidden" name="mode" value="edit" />
+ <div class="control-group">
+ <div class="control-label">{{ form.example_name.label }}</div>
+ <div class="controls">
+ {{ form.example_name|safe }}
+ {% if form.example_name.errors %}
+ <ul class="errors">
+ {% for error in form.example_name.errors %}
+ <li>{{ error }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+ </div>
+ </div>
+ <div class="control-group">
+ <div class="control-label">{{ form.example_description.label }}</div>
+ <div class="controls">
+ {{ form.example_description|safe }}
+ {% if form.example_description.errors %}
+ <ul class="errors">
+ {% for error in form.example_description.errors %}
+ <li>{{ error }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+ </div>
+ </div>
+ <div class="control-group">
+ <div class="controls">
+ <input class="btn btn-primary" type="submit" value="Save Changes"/>
+ <a class="btn cancel" href="{{ url_for('list_examples') }}">Cancel</a>
+ </div>
+ </div>
+ </fieldset>
+ </form>
+{% endblock content %}
@@ -19,6 +19,7 @@ <h1 id="">All Examples</h1>
<th>Added by</th>
<th>Added on</th>
<th>&nbsp;</th>
+ <th>&nbsp;</th>
</tr>
</thead>
<tbody>
@@ -29,15 +30,18 @@ <h1 id="">All Examples</h1>
<td>{{ example.example_name }}</td>
<td>{{ example.example_description }}</td>
<td>{{ example.added_by }}</td>
- <td>{{ example.timestamp.strftime('%b %d, %Y %I:%M %p') }}</td>
+ <td class="nowrap">{{ example.timestamp.strftime('%b %d, %Y %I:%M %p') }}</td>
+ <td>
+ <a href="{{ url_for('edit_example', example_id=example_id) }}" class="btn">Edit</a>
+ </td>
<td>
<form method="post" action="{{ url_for('delete_example', example_id=example_id) }}">
<button class="btn">Delete</button>
</form>
</td>
</tr>
{% else %}
- <tr><td colspan="6">No examples yet</td></tr>
+ <tr><td colspan="7">No examples yet</td></tr>
{% endfor %}
</tbody>
</table>
View
@@ -4,7 +4,6 @@
URL dispatch route mappings and error handlers
"""
-
from flask import render_template
from application import app
@@ -28,8 +27,11 @@
# Contrived admin-only view example
app.add_url_rule('/admin_only', 'admin_only', view_func=views.admin_only)
-# Delete an example (post method only)
-app.add_url_rule('/examples/delete/<int:example_id>', view_func=views.delete_example, methods=['POST'])
+# Edit an example
+app.add_url_rule('/examples/<int:example_id>/edit', 'edit_example', view_func=views.edit_example, methods=['GET', 'POST'])
+
+# Delete an example
+app.add_url_rule('/examples/<int:example_id>/delete', view_func=views.delete_example, methods=['POST'])
## Error handlers
View
@@ -8,12 +8,10 @@
must be passed *username* as the argument.
"""
-
-
from google.appengine.api import users
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
-from flask import render_template, flash, url_for, redirect
+from flask import request, render_template, flash, url_for, redirect
from models import ExampleModel
from decorators import login_required, admin_required
@@ -52,6 +50,20 @@ def list_examples():
@login_required
+def edit_example(example_id):
+ example = ExampleModel.get_by_id(example_id)
+ form = ExampleForm(obj=example)
+ if request.method == "POST":
+ if form.validate_on_submit():
+ example.example_name = form.data.get('example_name')
+ example.example_description = form.data.get('example_description')
+ example.put()
+ flash(u'Example %s successfully saved.' % example_id, 'success')
+ return redirect(url_for('list_examples'))
+ return render_template('edit_example.html', example=example, form=form)
+
+
+@login_required
def delete_example(example_id):
"""Delete an example object"""
example = ExampleModel.get_by_id(example_id)

0 comments on commit 0be674e

Please sign in to comment.