|
@@ -26,6 +26,9 @@ def unauthorized():
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
|
|
|
+app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
+app.config['RESTFUL_JSON'] = { 'separators': ( ', ', ': '), 'indent': 2 }
|
|
|
+
|
|
|
db = SQLAlchemy(app)
|
|
|
api = Api(app)
|
|
|
|
|
@@ -40,12 +43,6 @@ class Todo(db.Model):
|
|
|
def serialize(self):
|
|
|
return {'id': self.id, 'name': self.name, 'detail': self.detail}
|
|
|
|
|
|
-TODOS = {
|
|
|
- 'todo1': {'task': 'build an API'},
|
|
|
- 'todo2': {'task': '?????'},
|
|
|
- 'todo3': {'task': 'profit!'},
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
def abort_if_todo_doesnt_exist(todo_id):
|
|
|
task = Todo.query.filter_by(id=todo_id).first()
|
|
@@ -57,8 +54,9 @@ def abort_if_todo_doesnt_exist(todo_id):
|
|
|
# abort(404, message="Todo {} doesn't exist".format(todo_id))
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
|
|
-parser.add_argument('task')
|
|
|
-
|
|
|
+# http://flask-restful.readthedocs.io/en/0.3.5/reqparse.html
|
|
|
+parser.add_argument('task', required=True)
|
|
|
+parser.add_argument('name', required=True)
|
|
|
|
|
|
# Todo
|
|
|
# shows a single todo item and lets you delete a todo item
|
|
@@ -94,14 +92,20 @@ class TodoList(Resource):
|
|
|
def get(self):
|
|
|
tasks = Todo.query.all()
|
|
|
return [t.serialize() for t in tasks]
|
|
|
- # return TODOS
|
|
|
|
|
|
def post(self):
|
|
|
args = parser.parse_args()
|
|
|
- todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1
|
|
|
- todo_id = 'todo%i' % todo_id
|
|
|
- TODOS[todo_id] = {'task': args['task']}
|
|
|
- return TODOS[todo_id], 201
|
|
|
+ # This doesn't check to see if name already exists in the database.
|
|
|
+ task = Todo(name=args['name'], detail=args['task'])
|
|
|
+ db.session.add(task)
|
|
|
+ db.session.commit()
|
|
|
+ return task.id, 201
|
|
|
+
|
|
|
+# @auth.login_required
|
|
|
+class Hello(Resource):
|
|
|
+ @auth.login_required
|
|
|
+ def get(self):
|
|
|
+ return { 'Working': True }
|
|
|
|
|
|
##
|
|
|
## Actually setup the Api resource routing here
|
|
@@ -110,6 +114,7 @@ api.add_resource(TodoList, '/todos', '/todos/')
|
|
|
# api.add_resource(TodoList, '/todos/')
|
|
|
api.add_resource(TodoPath, '/todos/<todo_id>')
|
|
|
|
|
|
+api.add_resource(Hello, '/')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
app.run(port=11022, debug=True)
|