Skip to content

Commit

Permalink
Added library filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Casvt committed Nov 28, 2023
1 parent bb1f313 commit f5767c7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
28 changes: 25 additions & 3 deletions backend/volumes.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ class Library:
'publisher': 'publisher, title, year, volume_number'
}

filters = {
'wanted': 'WHERE issues_downloaded_monitored < issue_count_monitored',
'monitored': 'WHERE monitored = 1'
}

def __format_lib_output(self, library: List[dict]) -> List[dict]:
"""Format the library entries for API response
Expand All @@ -530,18 +535,26 @@ def __format_lib_output(self, library: List[dict]) -> List[dict]:
entry['cover'] = f'{ui_vars["url_base"]}/api/volumes/{entry["id"]}/cover'
return library

def get_volumes(self, sort: str='title') -> List[dict]:
def get_volumes(self,
sort: str='title',
filter: Union[str, None] = None
) -> List[dict]:
"""Get all volumes in the library
Args:
sort (str, optional): How to sort the list.
`title`, `year`, `volume_number`, `recently_added` and `publisher` allowed.
Defaults to 'title'.
filter (Union[str, None], optional): Apply a filter to the list.
`wanted` and `monitored` allowed.
Defaults to None.
Returns:
List[dict]: The list of volumes in the library.
"""
sort = self.sorting_orders[sort]
filter = self.filters.get(filter, '')

volumes = [
dict(v) for v in get_db('dict').execute(f"""
Expand Down Expand Up @@ -576,6 +589,7 @@ def get_volumes(self, sort: str='title') -> List[dict]:
SELECT COUNT(issue_id) FROM issues_with_files WHERE monitored = 1
) AS issues_downloaded_monitored
FROM volumes
{filter}
ORDER BY {sort};
"""
)
Expand All @@ -585,7 +599,11 @@ def get_volumes(self, sort: str='title') -> List[dict]:

return volumes

def search(self, query: str, sort: str='title') -> List[dict]:
def search(self,
query: str,
sort: str='title',
filter: Union[str, None] = None
) -> List[dict]:
"""Search in the library with a query
Args:
Expand All @@ -594,12 +612,16 @@ def search(self, query: str, sort: str='title') -> List[dict]:
`title`, `year`, `volume_number`, `recently_added` and `publisher` allowed.
Defaults to 'title'.
filter (Union[str, None], optional): Apply a filter to the list.
`wanted` and `monitored` allowed.
Defaults to None.
Returns:
List[dict]: The resulting list of matching volumes in the library
"""
volumes = [
v
for v in self.get_volumes(sort)
for v in self.get_volumes(sort, filter)
if query.lower() in v['title'].lower()
]

Expand Down
15 changes: 12 additions & 3 deletions frontend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ def extract_key(request, key: str, check_existence: bool=True) -> Any:
raise InvalidKeyValue(key, value)

elif key == 'sort':
if not value in library.sorting_orders.keys():
if not value in library.sorting_orders:
raise InvalidKeyValue(key, value)

elif key == 'filter':
if not value in library.filters and value:
raise InvalidKeyValue(key, value)
value = value or None

elif key in ('root_folder_id', 'new_root_folder', 'offset', 'limit'):
try:
value = int(value)
Expand Down Expand Up @@ -156,6 +161,9 @@ def extract_key(request, key: str, check_existence: bool=True) -> Any:
if key == 'sort':
value = 'title'

elif key == 'filter':
value = None

elif key == 'monitor':
value = True

Expand Down Expand Up @@ -424,10 +432,11 @@ def api_volumes():
if request.method == 'GET':
query = extract_key(request, 'query', False)
sort = extract_key(request, 'sort', False)
filter = extract_key(request, 'filter', False)
if query:
volumes = library.search(query, sort)
volumes = library.search(query, sort, filter)
else:
volumes = library.get_volumes(sort)
volumes = library.get_volumes(sort, filter)

return return_api(volumes)

Expand Down
1 change: 1 addition & 0 deletions frontend/static/js/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function convertSize(size) {
const default_values = {
'lib_sorting': 'title',
'lib_view': 'posters',
'lib_filter': '',
'theme': 'light',
'translated_filter': 'all',
'api_key': null,
Expand Down
10 changes: 8 additions & 2 deletions frontend/static/js/volumes.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ function populateLibrary(volumes, api_key, view) {
function fetchLibrary(api_key) {
const sort = document.querySelector('#sort-button').value;
const view = document.querySelector('#view-button').value;
const filter = document.querySelector('#filter-button').value;
const query = document.querySelector('#search-input').value;
let url;
if (query === '')
url = `${url_base}/api/volumes?api_key=${api_key}&sort=${sort}`;
url = `${url_base}/api/volumes?api_key=${api_key}&sort=${sort}&filter=${filter}`;
else
url = `${url_base}/api/volumes?api_key=${api_key}&sort=${sort}&query=${query}`;
url = `${url_base}/api/volumes?api_key=${api_key}&sort=${sort}&query=${query}&filter=${filter}`;
fetch(url)
.then(response => response.json())
.then(json => populateLibrary(json.result, api_key, view));
Expand Down Expand Up @@ -182,6 +183,7 @@ function searchAll(api_key) {

document.querySelector('#sort-button').value = getLocalStorage('lib_sorting')['lib_sorting'];
document.querySelector('#view-button').value = getLocalStorage('lib_view')['lib_view'];
document.querySelector('#filter-button').value = getLocalStorage('lib_filter')['lib_filter'];
usingApiKey()
.then(api_key => {
fetchLibrary(api_key);
Expand All @@ -198,5 +200,9 @@ usingApiKey()
setLocalStorage({'lib_view': document.querySelector('#view-button').value});
fetchLibrary(api_key);
});
addEventListener('#filter-button', 'change', e => {
setLocalStorage({'lib_filter': document.querySelector('#filter-button').value});
fetchLibrary(api_key);
});
});
document.querySelector('#search-container').setAttribute('action', 'javascript:searchLibrary();');
5 changes: 5 additions & 0 deletions frontend/templates/volumes.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
</button>
</div>
<div class="tool-bar">
<select id="filter-button" aria-label="Apply a filter">
<option value="">No Filter</option>
<option value="wanted">Wanted</option>
<option value="monitored">Monitored</option>
</select>
<select id="view-button" aria-label="Change the view">
<option value="posters">Posters</option>
<option value="table">Table</option>
Expand Down

0 comments on commit f5767c7

Please sign in to comment.