Last active
December 11, 2015 12:38
-
-
Save techniq/4602100 to your computer and use it in GitHub Desktop.
flask-script pull request 52 test app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, flash, url_for, redirect, render_template | |
from flask.ext.sqlalchemy import SQLAlchemy | |
app = Flask(__name__) | |
app.config.from_pyfile('application.cfg') | |
db = SQLAlchemy(app) | |
from models import Todo | |
@app.route('/') | |
def show_all(): | |
return render_template('show_all.html', | |
todos=Todo.query.order_by(Todo.pub_date.desc()).all() | |
) | |
@app.route('/new', methods=['GET', 'POST']) | |
def new(): | |
if request.method == 'POST': | |
if not request.form['title']: | |
flash('Title is required', 'error') | |
elif not request.form['text']: | |
flash('Text is required', 'error') | |
else: | |
todo = Todo(request.form['title'], request.form['text']) | |
db.session.add(todo) | |
db.session.commit() | |
flash(u'Todo item was successfully created') | |
return redirect(url_for('show_all')) | |
return render_template('new.html') | |
@app.route('/update', methods=['POST']) | |
def update_done(): | |
for todo in Todo.query.all(): | |
todo.done = ('done.%d' % todo.id) in request.form | |
flash('Updated status') | |
db.session.commit() | |
return redirect(url_for('show_all')) | |
if __name__ == "__main__": | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db' | |
SQLALCHEMY_ECHO = False | |
SECRET_KEY = '\xfb\x12\xdf\xa1@i\xd6>V\xc0\xbb\x8fp\x16#Z\x0b\x81\xeb\x16' | |
DEBUG = True | |
FOO='Bar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
from flask import Flask, current_app | |
from flask.ext.script import Manager | |
from app import app, db | |
manager = Manager(app) | |
if __name__ == "__main__": | |
manager.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from app import db | |
from datetime import datetime | |
class Todo(db.Model): | |
__tablename__ = 'todos' | |
id = db.Column('todo_id', db.Integer, primary_key=True) | |
title = db.Column(db.String(60)) | |
text = db.Column(db.String) | |
done = db.Column(db.Boolean) | |
pub_date = db.Column(db.DateTime) | |
def __init__(self, title, text): | |
self.title = title | |
self.text = text | |
self.done = False | |
self.pub_date = datetime.utcnow() | |
def __str__(self): | |
return 'Foo' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment