Add routes for deletion

This commit is contained in:
Lucas Verney 2015-12-25 22:14:10 +01:00
parent f67495bd38
commit 3ce2bf6efa
4 changed files with 97 additions and 3 deletions

View File

@ -34,7 +34,6 @@ app.install(plugin)
# Routes # Routes
# TODO: Routes for deletion
@app.get("/") @app.get("/")
def index(): def index():
return tools.APIResponse(tools.pretty_json({ return tools.APIResponse(tools.pretty_json({
@ -47,6 +46,10 @@ app.get("/papers/<id:int>/relationships/<name>",
callback=routes.get.fetch_relationship) callback=routes.get.fetch_relationship)
app.get("/papers/<id:int>/<name>", app.get("/papers/<id:int>/<name>",
callback=routes.get.fetch_relationship) callback=routes.get.fetch_relationship)
app.route("/papers/<id:int>", method="DELETE",
callback=routes.delete.delete_paper)
app.route("/papers/<id:int>/relationships/<name>", method="DELETE",
callback=routes.delete.delete_relationship)
app.post("/papers", callback=routes.post.create_paper) app.post("/papers", callback=routes.post.create_paper)

View File

@ -1,4 +1,5 @@
from . import delete
from . import get from . import get
from . import post from . import post
__all__ = ["get", "post"] __all__ = ["delete", "get", "post"]

85
routes/delete.py Normal file
View File

@ -0,0 +1,85 @@
"""
This file contains DELETE routes methods.
"""
import bottle
import database
import json
import tools
def delete_paper(id, db):
"""
Delete a given paper.
.. code-block:: bash
DELETE /papers/1
Accept: application/vnd.api+json
.. code-block:: bash
HTTP 204
:param id: The id of the requested paper to be deleted.
:param db: A database session, injected by the ``Bottle`` plugin.
:returns: An ``HTTPResponse``.
"""
resource = db.query(database.Paper).filter_by(id=id).first()
if resource:
db.delete(resource)
return tools.APIResponse(status=204, body="")
return bottle.HTTPError(404, "Not found")
def delete_relationship(id, name, db):
"""
Delete a given relationship
.. code-block:: bash
DELETE /papers/1/relationships/cite
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": [
{ "type": "cite", "id": "2" },
]
}
.. code-block:: bash
HTTP 204
:param id: The id of the requested paper from which the relationship \
should be deleted.
:param name: The name of the relationship to delete from.
:param db: A database session, injected by the ``Bottle`` plugin.
:returns: An ``HTTPResponse``.
"""
data = json.loads(bottle.request.body.read().decode("utf-8"))
# Validate the request
if "data" not in data:
return bottle.HTTPError(403, "Forbidden")
# Filter data, invalid entries are ignored
data = [i for i in data["data"]
if "type" in i and i["type"] == name and "id" in i]
# Complete replacement (data == []) is forbidden
if len(data) == 0:
return bottle.HTTPError(403, "Forbidden")
# Delete all the requested relationships
for i in data:
relationship = (db.query(database.Association)
.filter_by(left_id=id, right_id=i["id"])
.filter(database.Relationship.name == name)
.first())
if relationship is None:
# An error occurred => 403
return bottle.HTTPError(403, "Forbidden")
db.delete(relationship)
# Return an empty 204 on success
return tools.APIResponse(status=204, body="")

View File

@ -44,7 +44,12 @@ def create_paper(db):
"self": "/papers/1" "self": "/papers/1"
}, },
"relationships": { "relationships": {
TODO "cite": {
"links": {
"related": "/papers/1/relationships/cite"
}
},
} }
} }
} }