1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| import os
from flask import Flask from flask import render_template, redirect, url_for, request from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
DATABASE = "todo.db" basedir = os.path.abspath(os.path.dirname(__file__)) app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////" + os.path.join( basedir, DATABASE) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Item(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) category_id = db.Column( db.Integer, db.ForeignKey('category.id'), default=1)
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) items = db.relationship('Item', backref='category')
def init_db(): """Insert default categories and demo items. """ db.create_all() todo = Category(name=u'待完成') done = Category(name=u'已完成')
item = Item(body=u'看一小时《战争与和平》') item2 = Item(body=u'浇花', category=todo) item3 = Item(body=u'收快递', category=done)
db.session.add_all([todo, done, item, item2, item3]) db.session.commit()
@app.route('/', methods=['GET', 'POST']) def index(): if not os.path.exists(DATABASE): init_db()
if request.method == 'POST': body = request.form.get('item') category_id = request.form.get('category') category = Category.query.get_or_404(category_id) item = Item(body=body, category=category) db.session.add(item) db.session.commit() return redirect(url_for('category', id=category_id)) return redirect(url_for('category', id=1))
@app.route('/category/<int:id>') def category(id): category = Category.query.get_or_404(id) categories = Category.query.all() items = category.items return render_template( 'index.html', items=items, categories=categories, category_now=category)
@app.route('/edit-item/<int:id>', methods=['GET', 'POST']) def edit_item(id): item = Item.query.get_or_404(id) category = item.category item.body = request.form.get('body') db.session.add(item) db.session.commit() return redirect(url_for('category', id=category.id))
@app.route('/done/<int:id>', methods=['GET', 'POST']) def done(id): item = Item.query.get_or_404(id) category = item.category done_category = Category.query.get_or_404(2) done_item = Item(body=item.body, category=done_category) db.session.add(done_item) db.session.delete(item) db.session.commit() return redirect(url_for('category', id=category.id))
@app.route('/delete-item/<int:id>') def del_item(id): item = Item.query.get_or_404(id) category = item.category if item is None: return redirect(url_for('category', id=1)) db.session.delete(item) db.session.commit() return redirect(url_for('category', id=category.id))
if __name__ == '__main__': app.run(debug=True)
|