Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0be674ee05
Fetching contributors…

Cannot retrieve contributors at this time

92 lines (70 sloc) 2.891 kb
"""
views.py
URL route handlers
Note that any handler params must match the URL route params.
For example the *say_hello* handler, handling the URL route '/hello/<username>',
must be passed *username* as the argument.
"""
from google.appengine.api import users
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
from flask import request, render_template, flash, url_for, redirect
from models import ExampleModel
from decorators import login_required, admin_required
from forms import ExampleForm
def home():
return redirect(url_for('list_examples'))
def say_hello(username):
"""Contrived example to demonstrate Flask's url routing capabilities"""
return 'Hello %s' % username
@login_required
def list_examples():
"""List all examples"""
examples = ExampleModel.query()
form = ExampleForm()
if form.validate_on_submit():
example = ExampleModel(
example_name = form.example_name.data,
example_description = form.example_description.data,
added_by = users.get_current_user()
)
try:
example.put()
example_id = example.key.id()
flash(u'Example %s successfully saved.' % example_id, 'success')
return redirect(url_for('list_examples'))
except CapabilityDisabledError:
flash(u'App Engine Datastore is currently in read-only mode.', 'info')
return redirect(url_for('list_examples'))
return render_template('list_examples.html', examples=examples, form=form)
@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)
try:
example.key.delete()
flash(u'Example %s successfully deleted.' % example_id, 'success')
return redirect(url_for('list_examples'))
except CapabilityDisabledError:
flash(u'App Engine Datastore is currently in read-only mode.', 'info')
return redirect(url_for('list_examples'))
@admin_required
def admin_only():
"""This view requires an admin account"""
return 'Super-seekrit admin page.'
def warmup():
"""App Engine warmup handler
See http://code.google.com/appengine/docs/python/config/appconfig.html#Warming_Requests
"""
return ''
Jump to Line
Something went wrong with that request. Please try again.