Add datatables processing to collections and playlists tables

This commit is contained in:
JonnyWong16 2020-09-30 22:09:19 -07:00
parent 1061c334ae
commit f151bb1451
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
4 changed files with 107 additions and 56 deletions

View file

@ -21,10 +21,10 @@ collections_table_options = {
"columnDefs": [ "columnDefs": [
{ {
"targets": [0], "targets": [0],
"data": "title", "data": "titleSort",
"createdCell": function (td, cellData, rowData, row, col) { "createdCell": function (td, cellData, rowData, row, col) {
if (cellData !== '') { if (cellData !== '') {
$(td).html('<a href="' + page('info', rowData['ratingKey']) + '"><i class="fa fa-blank fa-fw"></i>' + cellData + '</a>'); $(td).html('<a href="' + page('info', rowData['ratingKey']) + '"><i class="fa fa-blank fa-fw"></i>' + rowData['title'] + '</a>');
} }
}, },
"width": "50%", "width": "50%",

View file

@ -581,6 +581,57 @@ def process_json_kwargs(json_kwargs):
return params return params
def process_datatable_rows(rows, json_data, default_sort, sort_keys=None):
if sort_keys is None:
sort_keys = {}
results = []
total_count = len(rows)
# Search results
search_value = json_data['search']['value'].lower()
if search_value:
searchable_columns = [d['data'] for d in json_data['columns'] if d['searchable']]
for row in rows:
for k, v in row.items():
if k in searchable_columns and search_value in v.lower():
results.append(row)
break
else:
results = rows
filtered_count = len(results)
# Sort results
results = sorted(results, key=lambda k: k[default_sort].lower())
sort_order = json_data['order']
for order in reversed(sort_order):
sort_key = json_data['columns'][int(order['column'])]['data']
reverse = True if order['dir'] == 'desc' else False
results = sorted(results, key=lambda k: sort_helper(k, sort_key, sort_keys), reverse=reverse)
# Paginate results
results = results[json_data['start']:(json_data['start'] + json_data['length'])]
data = {
'results': results,
'total_count': total_count,
'filtered_count': filtered_count
}
return data
def sort_helper(k, sort_key, sort_keys):
v = k[sort_key]
if sort_key in sort_keys:
v = sort_keys[sort_key].get(k[sort_key], v)
if isinstance(v, str):
v = v.lower()
return v
def sanitize_out(*dargs, **dkwargs): def sanitize_out(*dargs, **dkwargs):
""" Helper decorator that sanitized the output """ Helper decorator that sanitized the output
""" """

View file

@ -193,16 +193,36 @@ def get_collections_list(section_id=None, **kwargs):
'recordsTotal': 0, 'recordsTotal': 0,
'draw': 0, 'draw': 0,
'data': 'null', 'data': 'null',
'error': 'Unable to execute database query.'} 'error': 'Unable to get collections: missing section_id.'}
return default_return return default_return
collections = get_collections(section_id) collections = get_collections(section_id)
data = {'recordsFiltered': len(collections), # Get datatables JSON data
'recordsTotal': len(collections), json_data = helpers.process_json_kwargs(json_kwargs=kwargs['json_data'])
'data': collections,
'draw': kwargs.get('draw', 0) sort_keys = {
} 'collectionMode': {
-1: 'Library Default',
0: 'Hide collection',
1: 'Hide items in this collection',
2: 'Show this collection and its items'
},
'collectionSort': {
0: 'Release date',
1: 'Alphabetical'
}
}
results = helpers.process_datatable_rows(
collections, json_data, default_sort='titleSort', sort_keys=sort_keys)
data = {
'recordsFiltered': results['filtered_count'],
'recordsTotal': results['total_count'],
'data': results['results'],
'draw': int(json_data['draw'])
}
return data return data
@ -251,16 +271,23 @@ def get_playlists_list(section_id=None, **kwargs):
'recordsTotal': 0, 'recordsTotal': 0,
'draw': 0, 'draw': 0,
'data': 'null', 'data': 'null',
'error': 'Unable to execute database query.'} 'error': 'Unable to get playlists: missing section_id.'}
return default_return return default_return
playlists = get_playlists(section_id) playlists = get_playlists(section_id)
data = {'recordsFiltered': len(playlists), # Get datatables JSON data
'recordsTotal': len(playlists), json_data = helpers.process_json_kwargs(json_kwargs=kwargs['json_data'])
'data': playlists,
'draw': kwargs.get('draw', 0) results = helpers.process_datatable_rows(
} playlists, json_data, default_sort='title')
data = {
'recordsFiltered': results['filtered_count'],
'recordsTotal': results['total_count'],
'data': results['results'],
'draw': int(json_data['draw'])
}
return data return data

View file

@ -846,9 +846,9 @@ class WebInterface(object):
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@addtoapi() @addtoapi("get_collections_table")
def get_collections_list(self, section_id=None, **kwargs): def get_collections_list(self, section_id=None, **kwargs):
""" Get the data on the Tautulli media info tables. """ Get the data on the Tautulli collections tables.
``` ```
Required parameters: Required parameters:
@ -869,36 +869,23 @@ class WebInterface(object):
# Check if datatables json_data was received. # Check if datatables json_data was received.
# If not, then build the minimal amount of json data for a query # If not, then build the minimal amount of json data for a query
if not kwargs.get('json_data'): if not kwargs.get('json_data'):
# Alias 'title' to 'sort_title'
if kwargs.get('order_column') == 'title':
kwargs['order_column'] = 'sort_title'
# TODO: Find some one way to automatically get the columns # TODO: Find some one way to automatically get the columns
dt_columns = [("added_at", True, False), dt_columns = [("titleSort", True, True),
("sort_title", True, True), ("collectionMode", True, True),
("container", True, True), ("collectionSort", True, True),
("bitrate", True, True), ("childCount", True, False)]
("video_codec", True, True), kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "titleSort")
("video_resolution", True, True),
("video_framerate", True, True),
("audio_codec", True, True),
("audio_channels", True, True),
("file_size", True, False),
("last_played", True, False),
("play_count", True, False)]
kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "sort_title")
result = libraries.get_collections_list(section_id=section_id, result = libraries.get_collections_list(section_id=section_id, **kwargs)
kwargs=kwargs)
return result return result
@cherrypy.expose @cherrypy.expose
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth(member_of("admin")) @requireAuth(member_of("admin"))
@addtoapi() @addtoapi("get_playlists_table")
def get_playlists_list(self, section_id=None, **kwargs): def get_playlists_list(self, section_id=None, **kwargs):
""" Get the data on the Tautulli media info tables. """ Get the data on the Tautulli playlists tables.
``` ```
Required parameters: Required parameters:
@ -919,27 +906,13 @@ class WebInterface(object):
# Check if datatables json_data was received. # Check if datatables json_data was received.
# If not, then build the minimal amount of json data for a query # If not, then build the minimal amount of json data for a query
if not kwargs.get('json_data'): if not kwargs.get('json_data'):
# Alias 'title' to 'sort_title'
if kwargs.get('order_column') == 'title':
kwargs['order_column'] = 'sort_title'
# TODO: Find some one way to automatically get the columns # TODO: Find some one way to automatically get the columns
dt_columns = [("added_at", True, False), dt_columns = [("title", True, True),
("sort_title", True, True), ("leafCount", True, True),
("container", True, True), ("duration", True, True)]
("bitrate", True, True), kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "title")
("video_codec", True, True),
("video_resolution", True, True),
("video_framerate", True, True),
("audio_codec", True, True),
("audio_channels", True, True),
("file_size", True, False),
("last_played", True, False),
("play_count", True, False)]
kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "sort_title")
result = libraries.get_playlists_list(section_id=section_id, result = libraries.get_playlists_list(section_id=section_id, **kwargs)
kwargs=kwargs)
return result return result