diff --git a/data/interfaces/default/js/tables/collections_table.js b/data/interfaces/default/js/tables/collections_table.js index 03375056..e0ea9547 100644 --- a/data/interfaces/default/js/tables/collections_table.js +++ b/data/interfaces/default/js/tables/collections_table.js @@ -21,10 +21,10 @@ collections_table_options = { "columnDefs": [ { "targets": [0], - "data": "title", + "data": "titleSort", "createdCell": function (td, cellData, rowData, row, col) { if (cellData !== '') { - $(td).html('' + cellData + ''); + $(td).html('' + rowData['title'] + ''); } }, "width": "50%", diff --git a/plexpy/helpers.py b/plexpy/helpers.py index e839d6e8..96e6c94f 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -581,6 +581,57 @@ def process_json_kwargs(json_kwargs): 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): """ Helper decorator that sanitized the output """ diff --git a/plexpy/libraries.py b/plexpy/libraries.py index 9168128e..b211eb17 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -193,16 +193,36 @@ def get_collections_list(section_id=None, **kwargs): 'recordsTotal': 0, 'draw': 0, 'data': 'null', - 'error': 'Unable to execute database query.'} + 'error': 'Unable to get collections: missing section_id.'} return default_return collections = get_collections(section_id) - data = {'recordsFiltered': len(collections), - 'recordsTotal': len(collections), - 'data': collections, - 'draw': kwargs.get('draw', 0) - } + # Get datatables JSON data + json_data = helpers.process_json_kwargs(json_kwargs=kwargs['json_data']) + + 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 @@ -251,16 +271,23 @@ def get_playlists_list(section_id=None, **kwargs): 'recordsTotal': 0, 'draw': 0, 'data': 'null', - 'error': 'Unable to execute database query.'} + 'error': 'Unable to get playlists: missing section_id.'} return default_return playlists = get_playlists(section_id) - data = {'recordsFiltered': len(playlists), - 'recordsTotal': len(playlists), - 'data': playlists, - 'draw': kwargs.get('draw', 0) - } + # Get datatables JSON data + json_data = helpers.process_json_kwargs(json_kwargs=kwargs['json_data']) + + 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 diff --git a/plexpy/webserve.py b/plexpy/webserve.py index d692f26a..6dce962f 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -846,9 +846,9 @@ class WebInterface(object): @cherrypy.expose @cherrypy.tools.json_out() @requireAuth(member_of("admin")) - @addtoapi() + @addtoapi("get_collections_table") 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: @@ -869,36 +869,23 @@ class WebInterface(object): # Check if datatables json_data was received. # If not, then build the minimal amount of json data for a query 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 - dt_columns = [("added_at", True, False), - ("sort_title", True, True), - ("container", True, True), - ("bitrate", True, True), - ("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") + dt_columns = [("titleSort", True, True), + ("collectionMode", True, True), + ("collectionSort", True, True), + ("childCount", True, False)] + kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "titleSort") - result = libraries.get_collections_list(section_id=section_id, - kwargs=kwargs) + result = libraries.get_collections_list(section_id=section_id, **kwargs) return result @cherrypy.expose @cherrypy.tools.json_out() @requireAuth(member_of("admin")) - @addtoapi() + @addtoapi("get_playlists_table") 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: @@ -919,27 +906,13 @@ class WebInterface(object): # Check if datatables json_data was received. # If not, then build the minimal amount of json data for a query 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 - dt_columns = [("added_at", True, False), - ("sort_title", True, True), - ("container", True, True), - ("bitrate", True, True), - ("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") + dt_columns = [("title", True, True), + ("leafCount", True, True), + ("duration", True, True)] + kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "title") - result = libraries.get_playlists_list(section_id=section_id, - kwargs=kwargs) + result = libraries.get_playlists_list(section_id=section_id, **kwargs) return result