Browse Source

Updated: using sqlite database. Still issues existing names in the
database.

Charlie Root 7 years ago
parent
commit
01bff823eb
3 changed files with 29 additions and 13 deletions
  1. 4 0
      FROM.txt
  2. 7 0
      sql_gunicorn.sh
  3. 18 13
      sql_todo.py

+ 4 - 0
FROM.txt

@@ -30,6 +30,10 @@ curl 127.0.0.1:11022/todos
 curl 127.0.0.1:11022/todos/1
 curl 127.0.0.1:11022/todos/test -d "task=Further testing of delete path" -X PUT -v
 curl 127.0.0.1:11022/todos/3 -X DELETE -v
+# and now! 
+curl 127.0.0.1:11022/todos -d "name=fish" -d "task=Invoke fish" -X POST -v
+# Needs json header.
+curl -H "Content-Type: application/json" 127.0.0.1:11022/todos -d '{"name":"fish2", "task":"Inflate fish"}' -X POST -v
 
 
 

+ 7 - 0
sql_gunicorn.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+gunicorn --workers=2 --bind 127.0.0.1:11022 sql_todo:app
+
+# For testing multiple workers
+# gunicorn --workers=5 --bind 127.0.0.1:11022 full_todo:app
+

+ 18 - 13
sql_todo.py

@@ -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)