mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-22 22:35:06 +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:
parent
4600288ee4
commit
eaca5354d3
6
app.py
6
app.py
@ -22,16 +22,16 @@ class AllItemsResource:
|
|||||||
|
|
||||||
# get statistics, ordered by id, and use limit and offset to page through results
|
# 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))
|
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
|
# create a list to hold dicts of item stats
|
||||||
statistics = list()
|
statistics = list()
|
||||||
|
|
||||||
# iterate over results and build statistics object
|
# 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'] })
|
statistics.append({ 'id': item['id'], 'views': item['views'], 'downloads': item['downloads'] })
|
||||||
|
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
message = {
|
message = {
|
||||||
'currentPage': page,
|
'currentPage': page,
|
||||||
'totalPages': pages,
|
'totalPages': pages,
|
||||||
|
Loading…
Reference in New Issue
Block a user