2018-09-18 13:03:15 +02:00
|
|
|
# Tested with Python 3.6
|
|
|
|
# See DSpace Solr docs for tips about parameters
|
|
|
|
# https://wiki.duraspace.org/display/DSPACE/Solr
|
|
|
|
|
2018-09-18 15:59:28 +02:00
|
|
|
from config import SOLR_CORE
|
2018-09-23 15:47:00 +02:00
|
|
|
from database import database_connection
|
2018-09-18 13:03:15 +02:00
|
|
|
import falcon
|
2018-09-23 12:24:30 +02:00
|
|
|
from solr import solr_connection
|
2018-09-18 13:03:15 +02:00
|
|
|
|
2018-09-23 15:47:00 +02:00
|
|
|
db = database_connection()
|
|
|
|
solr = solr_connection()
|
2018-09-18 13:03:15 +02:00
|
|
|
|
|
|
|
class ItemResource:
|
2018-09-23 15:23:33 +02:00
|
|
|
def on_get(self, req, resp, item_id):
|
2018-09-18 13:03:15 +02:00
|
|
|
"""Handles GET requests"""
|
|
|
|
|
2018-09-23 15:47:00 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
# get item views (and catch the TypeError if item doesn't have any views)
|
|
|
|
cursor.execute('SELECT views FROM itemviews WHERE id={0}'.format(item_id))
|
|
|
|
try:
|
|
|
|
views = cursor.fetchone()['views']
|
|
|
|
except:
|
|
|
|
views = 0
|
|
|
|
|
|
|
|
# get item downloads (and catch the TypeError if item doesn't have any downloads)
|
|
|
|
cursor.execute('SELECT downloads FROM itemdownloads WHERE id={0}'.format(item_id))
|
|
|
|
try:
|
|
|
|
downloads = cursor.fetchone()['downloads']
|
|
|
|
except:
|
|
|
|
downloads = 0
|
|
|
|
|
|
|
|
cursor.close()
|
2018-09-18 13:03:15 +02:00
|
|
|
|
|
|
|
statistics = {
|
|
|
|
'id': item_id,
|
|
|
|
'views': views,
|
|
|
|
'downloads': downloads
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.media = statistics
|
|
|
|
|
|
|
|
api = falcon.API()
|
2018-09-23 15:23:33 +02:00
|
|
|
api.add_route('/item/{item_id:int}', ItemResource())
|
2018-09-23 10:33:26 +02:00
|
|
|
|
|
|
|
# vim: set sw=4 ts=4 expandtab:
|