Add optional include_last_accessed parameter to get_library

This commit is contained in:
JonnyWong16 2021-03-27 18:03:36 -07:00
parent 88cae2b0dc
commit 3db0a07fd2
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 23 additions and 13 deletions

View file

@ -772,7 +772,7 @@ class Libraries(object):
except Exception as e: except Exception as e:
logger.warn("Tautulli Libraries :: Unable to execute database query for set_config: %s." % e) logger.warn("Tautulli Libraries :: Unable to execute database query for set_config: %s." % e)
def get_details(self, section_id=None, server_id=None): def get_details(self, section_id=None, server_id=None, include_last_accessed=False):
default_return = {'row_id': 0, default_return = {'row_id': 0,
'server_id': '', 'server_id': '',
'section_id': 0, 'section_id': 0,
@ -797,7 +797,8 @@ class Libraries(object):
if server_id is None: if server_id is None:
server_id = plexpy.CONFIG.PMS_IDENTIFIER server_id = plexpy.CONFIG.PMS_IDENTIFIER
library_details = self.get_library_details(section_id=section_id, server_id=server_id) library_details = self.get_library_details(section_id=section_id, server_id=server_id,
include_last_accessed=include_last_accessed)
if library_details: if library_details:
return library_details return library_details
@ -808,7 +809,8 @@ class Libraries(object):
# Let's first refresh the libraries list to make sure the library isn't newly added and not in the db yet # Let's first refresh the libraries list to make sure the library isn't newly added and not in the db yet
refresh_libraries() refresh_libraries()
library_details = self.get_library_details(section_id=section_id, server_id=server_id) library_details = self.get_library_details(section_id=section_id, server_id=server_id,
include_last_accessed=include_last_accessed)
if library_details: if library_details:
return library_details return library_details
@ -819,10 +821,19 @@ class Libraries(object):
# If there is no library data we must return something # If there is no library data we must return something
return default_return return default_return
def get_library_details(self, section_id=None, server_id=None): def get_library_details(self, section_id=None, server_id=None, include_last_accessed=False):
if server_id is None: if server_id is None:
server_id = plexpy.CONFIG.PMS_IDENTIFIER server_id = plexpy.CONFIG.PMS_IDENTIFIER
last_accessed = 'NULL'
join = ''
if include_last_accessed:
last_accessed = 'MAX(session_history.started)'
join = 'LEFT OUTER JOIN session_history_metadata ' \
'ON library_sections.section_id == session_history_metadata.section_id ' \
'LEFT OUTER JOIN session_history ' \
'ON session_history_metadata.id == session_history.id'
monitor_db = database.MonitorDatabase() monitor_db = database.MonitorDatabase()
try: try:
@ -838,12 +849,9 @@ class Libraries(object):
'library_sections.thumb AS library_thumb, custom_thumb_url AS custom_thumb, ' \ 'library_sections.thumb AS library_thumb, custom_thumb_url AS custom_thumb, ' \
'library_sections.art AS library_art, ' \ 'library_sections.art AS library_art, ' \
'custom_art_url AS custom_art, is_active, ' \ 'custom_art_url AS custom_art, is_active, ' \
'do_notify, do_notify_created, keep_history, deleted_section, ' \ 'do_notify, do_notify_created, keep_history, deleted_section, %s AS last_accessed ' \
'MAX(session_history.started) AS last_accessed ' \ 'FROM library_sections %s ' \
'FROM library_sections ' \ 'WHERE %s AND server_id = ? ' % (last_accessed, join, where)
'LEFT OUTER JOIN session_history_metadata ON library_sections.section_id == session_history_metadata.section_id ' \
'LEFT OUTER JOIN session_history ON session_history_metadata.id == session_history.id ' \
'WHERE %s AND server_id = ? ' % where
result = monitor_db.select(query, args=args + [server_id]) result = monitor_db.select(query, args=args + [server_id])
except Exception as e: except Exception as e:
logger.warn("Tautulli Libraries :: Unable to execute database query for get_library_details: %s." % e) logger.warn("Tautulli Libraries :: Unable to execute database query for get_library_details: %s." % e)

View file

@ -952,7 +952,7 @@ class WebInterface(object):
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@addtoapi() @addtoapi()
def get_library(self, section_id=None, **kwargs): def get_library(self, section_id=None, include_last_accessed=False, **kwargs):
""" Get a library's details. """ Get a library's details.
``` ```
@ -960,7 +960,7 @@ class WebInterface(object):
section_id (str): The id of the Plex library section section_id (str): The id of the Plex library section
Optional parameters: Optional parameters:
None include_last_accessed (bool): True to include the last_accessed value for the library.
Returns: Returns:
json: json:
@ -983,9 +983,11 @@ class WebInterface(object):
} }
``` ```
""" """
include_last_accessed = helpers.bool_true(include_last_accessed)
if section_id: if section_id:
library_data = libraries.Libraries() library_data = libraries.Libraries()
library_details = library_data.get_details(section_id=section_id) library_details = library_data.get_details(section_id=section_id,
include_last_accessed=include_last_accessed)
if library_details: if library_details:
return library_details return library_details
else: else: