1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-12-22 12:42:19 +01:00

app.py: Iterate directly on cursor

We don't need to create an intermediate variable for the results of
the SQL query because psycopg2's cursor is iterable.

See: http://initd.org/psycopg/docs/cursor.html
This commit is contained in:
Alan Orth 2018-09-27 11:03:44 +03:00
parent 4600288ee4
commit eaca5354d3
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9

6
app.py
View File

@ -22,16 +22,16 @@ class AllItemsResource:
# get statistics, ordered by id, and use limit and offset to page through results
cursor.execute('SELECT id, views, downloads FROM items ORDER BY id ASC LIMIT {} OFFSET {}'.format(limit, offset))
results = cursor.fetchmany(limit)
cursor.close()
# create a list to hold dicts of item stats
statistics = list()
# iterate over results and build statistics object
for item in results:
for item in cursor:
statistics.append({ 'id': item['id'], 'views': item['views'], 'downloads': item['downloads'] })
cursor.close()
message = {
'currentPage': page,
'totalPages': pages,