18-views.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. Flask Module Docs: http://flask.pocoo.org/docs/api/#flask.Module
  3. This file is used for both the routing and logic of your
  4. application.
  5. """
  6. from google.appengine.api import mail
  7. from flask import Blueprint, url_for, render_template, request, redirect
  8. from models import Todo
  9. from forms import TodoForm, EmailForm
  10. views = Blueprint('views', __name__)
  11. @views.route('/')
  12. def index():
  13. """Render website's index page."""
  14. return render_template('index.html')
  15. @views.route('/todo/')
  16. def todo_list():
  17. """Simple todo page."""
  18. form = TodoForm()
  19. todos = Todo.all().order('-created_at')
  20. return render_template('todo.html', form=form, todos=todos)
  21. @views.route('/todo/add', methods=["POST"])
  22. def add_todo():
  23. """Add a todo."""
  24. form = TodoForm()
  25. if request.method == 'POST' and form.validate_on_submit():
  26. todo = Todo(text=form.todo.data)
  27. todo.save()
  28. return redirect(url_for('todo_list'))
  29. @views.route('/email/')
  30. def email():
  31. """Render a form for sending email."""
  32. form = EmailForm()
  33. return render_template('email.html', form=form)
  34. @views.route('/email/someone/', methods=['POST'])
  35. def email_someone():
  36. """
  37. This function actually emails the message.
  38. Make sure you change the from_address variable if you want to use this
  39. functionality -- otherwise it won't work.
  40. """
  41. form = EmailForm()
  42. if request.method == 'POST' and form.validate_on_submit():
  43. from_address = form.name.data + '@<YOURAPPID>.appspotmail.com'
  44. to_address = form.recipient.data
  45. subject = "%s <%s>" % (form.name.data, form.email.data)
  46. message = ("From: %s\n\n"
  47. "Email: %s\n\n"
  48. "Message: %s") % (form.name.data, form.email.data,
  49. form.message.data)
  50. mail.send_mail(sender=from_address, to=to_address,
  51. subject=subject, body=message)
  52. status = 'success'
  53. else:
  54. status = 'failed'
  55. return redirect(url_for('email_status', status=status))
  56. @views.route('/email/<status>/')
  57. def email_status(status):
  58. """Render a success or failed status for the email."""
  59. return render_template('email_status.html', status=status)
  60. @views.route('/qunit/')
  61. def qunit():
  62. """Render a QUnit page for JavaScript tests."""
  63. return render_template('test_js.html')
  64. @views.after_request
  65. def add_header(response):
  66. """Add header to force latest IE rendering engine and Chrome Frame."""
  67. response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
  68. return response
  69. @views.app_errorhandler(404)
  70. def page_not_found(error):
  71. """Custom 404 page."""
  72. return render_template('404.html'), 404