1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-11-25 23:58:18 +01:00

dspace_statistics_api/app.py: Use UUID

DSpace 6+ uses a UUID for item identifiers instead of an integer so
we need to adapt our PostgreSQL queries to use those. Note that we
can no longer sort results in the "all items" endpoint by ID. Also,
we need to use parameterized psycopg2 queries instead of strings to
support queries with UUIDs properly. To use the Python UUID objects
elsewhere in the code we need to make sure that we cast them to str.
This commit is contained in:
Alan Orth 2020-03-02 11:06:27 +02:00
parent 250fd8164f
commit 5955868b9a

View File

@ -27,9 +27,9 @@ class AllItemsResource:
cursor.execute("SELECT COUNT(id) FROM items") cursor.execute("SELECT COUNT(id) FROM items")
pages = round(cursor.fetchone()[0] / limit) pages = round(cursor.fetchone()[0] / limit)
# get statistics, ordered by id, and use limit and offset to page through results # get statistics and use limit and offset to page through results
cursor.execute( cursor.execute(
"SELECT id, views, downloads FROM items ORDER BY id ASC LIMIT {} OFFSET {}".format( "SELECT id, views, downloads FROM items LIMIT {} OFFSET {}".format(
limit, offset limit, offset
) )
) )
@ -41,7 +41,7 @@ class AllItemsResource:
for item in cursor: for item in cursor:
statistics.append( statistics.append(
{ {
"id": item["id"], "id": str(item["id"]),
"views": item["views"], "views": item["views"],
"downloads": item["downloads"], "downloads": item["downloads"],
} }
@ -61,26 +61,32 @@ class ItemResource:
def on_get(self, req, resp, item_id): def on_get(self, req, resp, item_id):
"""Handles GET requests""" """Handles GET requests"""
import psycopg2.extras
# Adapt Pythons uuid.UUID type to PostgreSQLs uuid
# See: https://www.psycopg.org/docs/extras.html
psycopg2.extras.register_uuid()
with DatabaseManager() as db: with DatabaseManager() as db:
db.set_session(readonly=True) db.set_session(readonly=True)
with db.cursor() as cursor: with db.cursor() as cursor:
cursor = db.cursor() cursor = db.cursor()
cursor.execute( cursor.execute(
"SELECT views, downloads FROM items WHERE id={}".format(item_id) "SELECT views, downloads FROM items WHERE id=%s", [str(item_id)]
) )
if cursor.rowcount == 0: if cursor.rowcount == 0:
raise falcon.HTTPNotFound( raise falcon.HTTPNotFound(
title="Item not found", title="Item not found",
description='The item with id "{}" was not found.'.format( description='The item with id "{}" was not found.'.format(
item_id str(item_id)
), ),
) )
else: else:
results = cursor.fetchone() results = cursor.fetchone()
statistics = { statistics = {
"id": item_id, "id": str(item_id),
"views": results["views"], "views": results["views"],
"downloads": results["downloads"], "downloads": results["downloads"],
} }
@ -91,6 +97,6 @@ class ItemResource:
api = application = falcon.API() api = application = falcon.API()
api.add_route("/", RootResource()) api.add_route("/", RootResource())
api.add_route("/items", AllItemsResource()) api.add_route("/items", AllItemsResource())
api.add_route("/item/{item_id:int}", ItemResource()) api.add_route("/item/{item_id:uuid}", ItemResource())
# vim: set sw=4 ts=4 expandtab: # vim: set sw=4 ts=4 expandtab: