def get_todo(id): """Return a todo.""" todo = Todos.get_from_id(id) if request.method == 'POST': data = request.get_json() new_title = data.get('title') new_desc = data.get('desc') done = data.get('done') tags = set(data.get('tags')) if new_title: todo.title = new_title if new_desc: todo.desc = new_desc if done: todo.done = not todo.done if tags: for tag in tags: add_tag_todo(tag, todo) todo['tags'] = todo['tags'] + tags todo.save_partial() todo.reload() return jsonify(stringify_datetime(stringify_id(todo)))
def add_todo(): todo_text = request.form['text'] db_session.add(Todos(title=todo_text)) db_session.commit() todos = db_session.query(Todos).order_by(desc(Todos.id)).all() return jsonify(todos_schema.dump(todos).data)
def add_todo(): if request.method == 'POST': data = request.get_json() title = data.get('title') author = data.get('author') desc = data.get('desc') tags = data.get('tags', []) if tags: id = Todos.add_one(title, author, desc).inserted_id todo = Todos.find_by_id(id) for tag in tags: add_tag_todo(tag, todo) add_todo_tags(tags, todo) todo.save_partial() todo.reload() return jsonify(stringify_datetime(stringify_id(todo)))
def get_todos_done(): return jsonify(stringify_datetimes(stringify_ids(Todos.find_done())))
def get_todos_by_author(author): """All todos by author.""" return jsonify( stringify_datetimes(stringify_ids(Todos.find_by_author(author))))
def get_todos(): """All todos""" return jsonify(stringify_datetimes(stringify_ids(Todos.find({}))))
from flask import Flask, request, jsonify, render_template from flask_pymongo import PyMongo from model import Tag, Tags, Todo, Todos from bson.objectid import ObjectId from datetime import datetime DATETIME_FORMAT = '%a %b %d, %Y' app = Flask(__name__) app.config.from_object(__name__) app.config["MONGO_URI"] = "mongodb://localhost:27017/chrashlist" mongo = PyMongo(app) client = mongo.cx Todos = Todos(collection=client.chrashlist.todos) Tags = Tags(collection=client.chrashlist.tags) # ENJOY ALL THESE UTILITY FUNCTIONS HAHAHA def stringify_id(doc): """Stringify ObjectId because flask.jsonify can't handle them.""" doc['_id'] = str(doc['_id']) return doc def stringify_ids(collection): """Stringify a bunch of ObjectIds.""" return [stringify_id(doc) for doc in collection]
def create_new_todo(todo_title): todo = Todos(title=todo_title) db_session.add(todo) db_session.commit() return todo