Exemple #1
0
def get_todo(current_user, todo_id):
    todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first()

    if not todo:
        return jsonify({'message': 'No todo found'})

    return TodoSchema().jsonify(todo)
Exemple #2
0
def create_todos(current_user):
    new_todo = Todo(
        text=request.json['text'],
        complete=request.json['complete'],
        user_id=current_user.id
    )

    db.session.add(new_todo)
    db.session.commit()

    return TodoSchema().jsonify(new_todo)
Exemple #3
0
def update_todo(current_user, todo_id):
    todo = Todo.query.filter_by(id=todo_id, user_id=current_user.id).first()

    if not todo:
        return jsonify({'message': 'No todo found'})

    todo.text = request.json['text']
    todo.complete = request.json['complete']

    db.session.commit()

    return TodoSchema().jsonify(todo)
Exemple #4
0
 def post(self):
     parser = reqparse.RequestParser()
     parser = parser.add_argument("title")
     parser = parser.add_argument("completed", default=False)
     params = parser.parse_args()
     try:
         todo = TodoSchema().load({
             "user_id": 0,
             "title": params["title"],
             "completed": params["completed"]
         })
         dao.add_item(todo)
     except ValidationError as e:
         return make_jsend_response(message=e.args, code=422)
     return make_jsend_response(data=todo.uuid)
Exemple #5
0
import yaml
from redis import Redis
from flask import Flask, request, g
from flask_cors import CORS
from flask_restplus import Resource, Api, Namespace, reqparse
from marshmallow.exceptions import ValidationError
from pymongo import MongoClient

from models.todo import Todo, TodoSchema
from models.todo_dao import MockDao, MongoDAO
from library.utils import make_url, replace_env, make_jsend_response
from library.cache import redis_cachable, invalidate_key

TODOS_DB = "/todos/database/todos_db.json"

todo_schema = TodoSchema(only=["uuid", "title", "completed", "user_id"])
todos_schema = TodoSchema(only=["uuid", "title", "completed", "user_id"],
                          many=True)

with open("/config/todos/default_config.yml", "r") as f:
    config = yaml.load(f, yaml.SafeLoader)
replace_env(config)
url = make_url(config["database"]["mongo"], include_db=False)
client = MongoClient(url)
collection = client.todos.todos_collection

dao = MongoDAO(collection, TodoSchema)

redis_config = config["database"]["redis"]
r = Redis(
    host=redis_config["host"],
Exemple #6
0
def get_todos(current_user):
    return TodoSchema(many=True).jsonify(Todo.query.filter_by(user_id=current_user.id))
Exemple #7
0
def get_all_todos(dao=MongoDAO(collection, TodoSchema)):
    return TodoSchema(many=True).dump(dao.get_all())