Debian Codegraph @

src/calibre/srv/ajax.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
3
4 from functools import partial
5 from itertools import cycle
6
7 from calibre import force_unicode
8 from calibre.db.view import sanitize_sort_field_name
9 from calibre.ebooks.metadata import title_sort
10 from calibre.ebooks.metadata.book.json_codec import JsonCodec
11 from calibre.library.field_metadata import category_icon_map
12 from calibre.srv.content import get as get_content
13 from calibre.srv.content import icon as get_icon
14 from calibre.srv.errors import BookNotFound, HTTPBadRequest, HTTPNotFound
15 from calibre.srv.routes import endpoint, json
16 from calibre.srv.utils import custom_fields_to_display, decode_name, encode_name, get_db, http_date
17 from calibre.utils.config import prefs, tweaks
18 from calibre.utils.date import isoformat, timestampfromdt
19 from calibre.utils.icu import numeric_sort_key as sort_key
20 from calibre.utils.localization import _
21
22
23 def ensure_val(x, *allowed):
24 if x not in allowed:
25 x = allowed[0]
26 return x
27
28
29 def get_pagination(query, num=100, offset=0):
30 try:
31 num = int(query.get('num', num))
32 except Exception:
33 raise HTTPNotFound('Invalid num')
34 try:
35 offset = int(query.get('offset', offset))
36 except Exception:
37 raise HTTPNotFound('Invalid offset')
38 return num, offset
39
40
41 def category_icon(category, meta): # {{{
42 if category in category_icon_map:
43 icon = category_icon_map[category]
44 elif meta['is_custom']:
45 icon = category_icon_map['custom:']
46 elif meta['kind'] == 'user':
47 icon = category_icon_map['user:']
48 else:
49 icon = 'blank.png'
50 return icon
51
52
53 # }}}
54
55 # Book metadata {{{
56
57
58 def book_to_json(ctx, rd, db, book_id, get_category_urls=True, device_compatible=False, device_for_template=None):
59 mi = db.get_metadata(book_id, get_cover=False)
60 codec = JsonCodec(db.field_metadata)
61 if not device_compatible:
62 try:
63 mi.rating = mi.rating / 2.0
64 except Exception:
65 mi.rating = 0.0
66 data = codec.encode_book_metadata(mi)
67 for x in ('publication_type', 'size', 'db_id', 'lpath', 'mime', 'rights', 'book_producer'):
68 data.pop(x, None)
69
70 get = partial(ctx.url_for, get_content, book_id=book_id, library_id=db.server_library_id)
71 data['cover'] = get(what='cover')
72 data['thumbnail'] = get(what='thumb')
73
74 if not device_compatible:
75 mi.format_metadata = {k.lower(): dict(v) for k, v in mi.format_metadata.items()}
76 for v in mi.format_metadata.values():
77 mtime = v.get('mtime', None)
78 if mtime is not None:
79 v['mtime'] = isoformat(mtime, as_utc=True)
80 data['format_metadata'] = mi.format_metadata
81 fmts = {x.lower() for x in mi.format_metadata}
82 pf = prefs['output_format'].lower()
83 other_fmts = list(fmts)
84 try:
85 fmt = pf if pf in fmts else other_fmts[0]
86 except Exception:
87 fmt = None
88 if fmts and fmt:
89 other_fmts = [x for x in fmts if x != fmt]
90 data['formats'] = sorted(fmts)
91 if fmt:
92 data['main_format'] = {fmt: get(what=fmt)}
93 else:
94 data['main_format'] = None
95 data['other_formats'] = {fmt: get(what=fmt) for fmt in other_fmts}
96
97 if get_category_urls:
98 category_urls = data['category_urls'] = {}
99 all_cats = ctx.get_categories(rd, db)
100 for key in mi.all_field_keys():
101 fm = mi.metadata_for_field(key)
102 if fm and fm['is_category'] and not fm['is_csp'] and key != 'formats' and fm['datatype'] != 'rating':
103 categories = mi.get(key) or []
104 if isinstance(categories, (str, bytes)):
105 categories = [categories]
106 category_urls[key] = dbtags = {}
107 for category in categories:
108 for tag in all_cats.get(key, ()):
109 if tag.original_name == category:
110 dbtags[category] = ctx.url_for(
111 books_in,
112 encoded_category=encode_name(tag.category or key),
113 encoded_item=encode_name(tag.original_name if tag.id is None else str(tag.id)),
114 library_id=db.server_library_id,
115 )
116 break
117 else:
118 series = data.get('series', None) or ''
119 if series:
120 tsorder = tweaks['save_template_title_series_sorting']
121 series = title_sort(series, order=tsorder)
122 data['_series_sort_'] = series
123 if device_for_template:
124 import posixpath
125
126 from calibre.customize.ui import device_plugins
127 from calibre.devices.utils import create_upload_path
128 from calibre.utils.filenames import ascii_filename as sanitize
129
130 for device_class in device_plugins():
131 if device_class.__class__.__name__ == device_for_template:
132 template = device_class.save_template()
133 data['_filename_'] = create_upload_path(mi, str(book_id), template, sanitize, path_type=posixpath)
134 break
135
136 return data, mi.last_modified
137
138
139 @endpoint('/ajax/book/{book_id}/{library_id=None}', postprocess=json)
140 def book(ctx, rd, book_id, library_id):
141 """
142 Return the metadata of the book as a JSON dictionary.
143
144 Query parameters: ?category_urls=true&id_is_uuid=false&device_for_template=None
145
146 If category_urls is true the returned dictionary also contains a
147 mapping of category (field) names to URLs that return the list of books in the
148 given category.
149
150 If id_is_uuid is true then the book_id is assumed to be a book uuid instead.
151 """
152 db = get_db(ctx, rd, library_id)
153 with db.safe_read_lock:
154 id_is_uuid = rd.query.get('id_is_uuid', 'false')
155 oid = book_id
156 if id_is_uuid == 'true':
157 book_id = db.lookup_by_uuid(book_id)
158 else:
159 try:
160 book_id = int(book_id)
161 if not db.has_id(book_id):
162 book_id = None
163 except Exception:
164 book_id = None
165 if book_id is None or not ctx.has_id(rd, db, book_id):
166 raise BookNotFound(oid, db)
167 category_urls = rd.query.get('category_urls', 'true').lower()
168 device_compatible = rd.query.get('device_compatible', 'false').lower()
169 device_for_template = rd.query.get('device_for_template', None)
170
171 data, last_modified = book_to_json(
172 ctx,
173 rd,
174 db,
175 book_id,
176 get_category_urls=category_urls == 'true',
177 device_compatible=device_compatible == 'true',
178 device_for_template=device_for_template,
179 )
180 rd.outheaders['Last-Modified'] = http_date(timestampfromdt(last_modified))
181 return data
182
183
184 @endpoint('/ajax/books/{library_id=None}', postprocess=json)
185 def books(ctx, rd, library_id):
186 """
187 Return the metadata for the books as a JSON dictionary.
188
189 Query parameters: ?ids=all&category_urls=true&id_is_uuid=false&device_for_template=None
190
191 If category_urls is true the returned dictionary also contains a
192 mapping of category (field) names to URLs that return the list of books in the
193 given category.
194
195 If id_is_uuid is true then the book_id is assumed to be a book uuid instead.
196 """
197 db = get_db(ctx, rd, library_id)
198 with db.safe_read_lock:
199 id_is_uuid = rd.query.get('id_is_uuid', 'false')
200 ids = rd.query.get('ids')
201 if ids is None or ids == 'all':
202 ids = db.all_book_ids()
203 else:
204 ids = ids.split(',')
205 if id_is_uuid == 'true':
206 ids = {db.lookup_by_uuid(x) for x in ids}
207 ids.discard(None)
208 else:
209 try:
210 ids = {int(x) for x in ids}
211 except Exception:
212 raise HTTPNotFound('ids must a comma separated list of integers')
213 last_modified = None
214 category_urls = rd.query.get('category_urls', 'true').lower() == 'true'
215 device_compatible = rd.query.get('device_compatible', 'false').lower() == 'true'
216 device_for_template = rd.query.get('device_for_template', None)
217 ans = {}
218 allowed_book_ids = ctx.allowed_book_ids(rd, db)
219 for book_id in ids:
220 if book_id not in allowed_book_ids:
221 ans[book_id] = None
222 continue
223 data, lm = book_to_json(
224 ctx,
225 rd,
226 db,
227 book_id,
228 get_category_urls=category_urls,
229 device_compatible=device_compatible,
230 device_for_template=device_for_template,
231 )
232 last_modified = lm if last_modified is None else max(lm, last_modified)
233 ans[book_id] = data
234 if last_modified is not None:
235 rd.outheaders['Last-Modified'] = http_date(timestampfromdt(last_modified))
236 return ans
237
238
239 # }}}
240
241 # Categories (Tag Browser) {{{
242
243
244 @endpoint('/ajax/categories/{library_id=None}', postprocess=json)
245 def categories(ctx, rd, library_id):
246 """
247 Return the list of top-level categories as a list of dictionaries. Each
248 dictionary is of the form::
249 {
250 'name': Display Name,
251 'url':URL that gives the JSON object corresponding to all entries in this category,
252 'icon': URL to icon of this category,
253 'is_category': False for the All Books and Newest categories, True for everything else
254 }
255
256 """
257 db = get_db(ctx, rd, library_id)
258 with db.safe_read_lock:
259 ans = {}
260 categories = ctx.get_categories(rd, db, vl=rd.query.get('vl') or '')
261 category_meta = db.field_metadata
262 library_id = db.server_library_id
263
264 def getter(x):
265 return category_meta[x]['name']
266
267 displayed_custom_fields = custom_fields_to_display(db)
268
269 for category in sorted(categories, key=lambda x: sort_key(getter(x))):
270 if len(categories[category]) == 0:
271 continue
272 if category in ('formats', 'identifiers'):
273 continue
274 meta = category_meta.get(category, None)
275 if meta is None:
276 continue
277 if category_meta.is_ignorable_field(category) and category not in displayed_custom_fields:
278 continue
279 display_name = meta['name']
280 if category.startswith('@'):
281 category = category.partition('.')[0]
282 display_name = category[1:]
283 url = force_unicode(category)
284 icon = category_icon(category, meta)
285 ans[url] = (display_name, icon)
286
287 ans = [{'url': k, 'name': v[0], 'icon': v[1], 'is_category': True} for k, v in ans.items()]
288 ans.sort(key=lambda x: sort_key(x['name']))
289 for name, url, icon in [
290 (_('All books'), 'allbooks', 'book.png'),
291 (_('Newest'), 'newest', 'forward.png'),
292 ]:
293 ans.insert(0, {'name': name, 'url': url, 'icon': icon, 'is_category': False})
294
295 for c in ans:
296 c['url'] = ctx.url_for(globals()['category'], encoded_name=encode_name(c['url']), library_id=library_id)
297 c['icon'] = ctx.url_for(get_icon, which=c['icon'])
298
299 return ans
300
301
302 @endpoint('/ajax/category/{encoded_name}/{library_id=None}', postprocess=json)
303 def category(ctx, rd, encoded_name, library_id):
304 """
305 Return a dictionary describing the category specified by name. The
306
307 Optional: ?num=100&offset=0&sort=name&sort_order=asc
308
309 The dictionary looks like::
310
311 {
312 'category_name': Category display name,
313 'base_url': Base URL for this category,
314 'total_num': Total numberof items in this category,
315 'offset': The offset for the items returned in this result,
316 'num': The number of items returned in this result,
317 'sort': How the returned items are sorted,
318 'sort_order': asc or desc
319 'subcategories': List of sub categories of this category.
320 'items': List of items in this category,
321 }
322
323 Each subcategory is a dictionary of the same form as those returned by
324 /ajax/categories
325
326 Each item is a dictionary of the form::
327
328 {
329 'name': Display name,
330 'average_rating': Average rating for books in this item,
331 'count': Number of books in this item,
332 'url': URL to get list of books in this item,
333 'has_children': If True this item contains sub categories, look
334 for an entry corresponding to this item in subcategories int he
335 main dictionary,
336 }
337
338 :param sort: How to sort the returned items. Choices are: name, rating,
339 popularity
340 :param sort_order: asc or desc
341
342 To learn how to create subcategories see
343 https://manual.calibre-ebook.com/sub_groups.html
344 """
345
346 db = get_db(ctx, rd, library_id)
347 with db.safe_read_lock:
348 num, offset = get_pagination(rd.query)
349 sort, sort_order = rd.query.get('sort'), rd.query.get('sort_order')
350 sort = ensure_val(sort, 'name', 'rating', 'popularity')
351 sort_order = ensure_val(sort_order, 'asc', 'desc')
352 try:
353 dname = decode_name(encoded_name)
354 except Exception:
355 raise HTTPNotFound(f'Invalid encoding of category name {encoded_name!r}')
356 base_url = ctx.url_for(globals()['category'], encoded_name=encoded_name, library_id=db.server_library_id)
357
358 if dname in ('newest', 'allbooks'):
359 sort, sort_order = 'timestamp', 'desc'
360 rd.query['sort'], rd.query['sort_order'] = sort, sort_order
361 return books_in(ctx, rd, encoded_name, encode_name('0'), library_id)
362
363 fm = db.field_metadata
364 categories = ctx.get_categories(rd, db)
365 hierarchical_categories = db.pref('categories_using_hierarchy', ())
366
367 subcategory = dname
368 toplevel = subcategory.partition('.')[0]
369 if toplevel == subcategory:
370 subcategory = None
371 if toplevel not in categories or toplevel not in fm:
372 raise HTTPNotFound(f'Category {toplevel!r} not found')
373
374 # Find items and sub categories
375 subcategories = []
376 meta = fm[toplevel]
377 item_names = {}
378 children = set()
379
380 if meta['kind'] == 'user':
381 fullname = (toplevel + '.' + subcategory) if subcategory is not None else toplevel
382 try:
383 # User categories cannot be applied to books, so this is the
384 # complete set of items, no need to consider sub categories
385 items = categories[fullname]
386 except Exception:
387 raise HTTPNotFound(f'User category {fullname!r} not found')
388
389 parts = fullname.split('.')
390 for candidate in categories:
391 cparts = candidate.split('.')
392 if len(cparts) == len(parts) + 1 and cparts[:-1] == parts:
393 subcategories.append({'name': cparts[-1], 'url': candidate, 'icon': category_icon(toplevel, meta)})
394
395 category_name = toplevel[1:].split('.')
396 # When browsing by user categories we ignore hierarchical normal
397 # columns, so children can be empty
398
399 elif toplevel in hierarchical_categories:
400 items = []
401
402 category_names = [x.original_name.split('.') for x in categories[toplevel] if '.' in x.original_name]
403
404 if subcategory is None:
405 children = {x[0] for x in category_names}
406 category_name = [meta['name']]
407 items = [x for x in categories[toplevel] if '.' not in x.original_name]
408 else:
409 subcategory_parts = subcategory.split('.')[1:]
410 category_name = [meta['name']] + subcategory_parts
411
412 lsp = len(subcategory_parts)
413 children = {'.'.join(x) for x in category_names if len(x) == lsp + 1 and x[:lsp] == subcategory_parts}
414 items = [x for x in categories[toplevel] if x.original_name in children]
415 item_names = {x: x.original_name.rpartition('.')[-1] for x in items}
416 # Only mark the subcategories that have children themselves as
417 # subcategories
418 children = {'.'.join(x[: lsp + 1]) for x in category_names if len(x) > lsp + 1 and x[:lsp] == subcategory_parts}
419 subcategories = [{'name': x.rpartition('.')[-1], 'url': toplevel + '.' + x, 'icon': category_icon(toplevel, meta)} for x in children]
420 else:
421 items = categories[toplevel]
422 category_name = meta['name']
423
424 for x in subcategories:
425 x['url'] = ctx.url_for(globals()['category'], encoded_name=encode_name(x['url']), library_id=db.server_library_id)
426 x['icon'] = ctx.url_for(get_icon, which=x['icon'])
427 x['is_category'] = True
428
429 sort_keygen = {
430 'name': lambda x: sort_key(x.sort or x.original_name),
431 'popularity': lambda x: x.count,
432 'rating': lambda x: x.avg_rating,
433 }
434 items.sort(key=sort_keygen[sort], reverse=sort_order == 'desc')
435 total_num = len(items)
436 items = items[offset : offset + num]
437 items = [
438 {
439 'name': item_names.get(x, x.original_name),
440 'average_rating': x.avg_rating,
441 'count': x.count,
442 'url': ctx.url_for(
443 books_in,
444 encoded_category=encode_name(x.category or toplevel),
445 encoded_item=encode_name(x.original_name if x.id is None else str(x.id)),
446 library_id=db.server_library_id,
447 ),
448 'has_children': x.original_name in children,
449 }
450 for x in items
451 ]
452
453 return {
454 'category_name': category_name,
455 'base_url': base_url,
456 'total_num': total_num,
457 'offset': offset,
458 'num': len(items),
459 'sort': sort,
460 'sort_order': sort_order,
461 'subcategories': subcategories,
462 'items': items,
463 }
464
465
466 @endpoint('/ajax/books_in/{encoded_category}/{encoded_item}/{library_id=None}', postprocess=json)
467 def books_in(ctx, rd, encoded_category, encoded_item, library_id):
468 """
469 Return the books (as list of ids) present in the specified category.
470
471 Optional: ?num=100&offset=0&sort=title&sort_order=asc&get_additional_fields=
472 """
473 db = get_db(ctx, rd, library_id)
474 with db.safe_read_lock:
475 try:
476 dname, ditem = map(decode_name, (encoded_category, encoded_item))
477 except Exception:
478 raise HTTPNotFound(f'Invalid encoded param: {encoded_category!r} ({encoded_item!r})')
479 num, offset = get_pagination(rd.query)
480 sort, sort_order = rd.query.get('sort', 'title'), rd.query.get('sort_order')
481 sort_order = ensure_val(sort_order, 'asc', 'desc')
482 sfield = sanitize_sort_field_name(db.field_metadata, sort)
483 if sfield not in db.field_metadata.sortable_field_keys():
484 raise HTTPNotFound(f'{sort} is not a valid sort field')
485
486 if dname in ('allbooks', 'newest'):
487 ids = ctx.allowed_book_ids(rd, db)
488 elif dname == 'search':
489 try:
490 ids = ctx.search(rd, db, f'search:"{ditem}"')
491 except Exception:
492 raise HTTPNotFound(f'Search: {ditem!r} not understood')
493 else:
494 try:
495 cid = int(ditem)
496 except Exception:
497 raise HTTPNotFound(f'Category id {ditem!r} not an integer')
498
499 if dname == 'news':
500 dname = 'tags'
501 ids = db.get_books_for_category(dname, cid) & ctx.allowed_book_ids(rd, db)
502
503 ids = db.multisort(fields=[(sfield, sort_order == 'asc')], ids_to_sort=ids)
504 total_num = len(ids)
505 ids = ids[offset : offset + num]
506
507 result = {
508 'total_num': total_num,
509 'sort_order': sort_order,
510 'offset': offset,
511 'num': len(ids),
512 'sort': sort,
513 'base_url': ctx.url_for(books_in, encoded_category=encoded_category, encoded_item=encoded_item, library_id=db.server_library_id),
514 'book_ids': ids,
515 }
516
517 get_additional_fields = rd.query.get('get_additional_fields')
518 if get_additional_fields:
519 additional_fields = {}
520 for field in get_additional_fields.split(','):
521 field = field.strip()
522 if field:
523 flist = additional_fields[field] = []
524 for id_ in ids:
525 flist.append(db.field_for(field, id_, default_value=None))
526 if additional_fields:
527 result['additional_fields'] = additional_fields
528 return result
529
530
531 # }}}
532
533 # Search {{{
534
535
536 def search_result(ctx, rd, db, query, num, offset, sort, sort_order, vl=''):
537 from calibre.db.search import TemplatesNotAllowed
538
539 multisort = [
540 (sanitize_sort_field_name(db.field_metadata, s), ensure_val(o, 'asc', 'desc') == 'asc') for s, o in zip(sort.split(','), cycle(sort_order.split(',')))
541 ]
542 skeys = db.field_metadata.sortable_field_keys()
543 for sfield, sorder in multisort:
544 if sfield not in skeys:
545 raise HTTPNotFound(f'{sort} is not a valid sort field')
546
547 try:
548 ids, parse_error = ctx.search(rd, db, query, vl=vl, report_restriction_errors=True)
549 except TemplatesNotAllowed:
550 raise HTTPBadRequest(_('templates are not allowed in search expressions'))
551 ids = db.multisort(fields=multisort, ids_to_sort=ids)
552 total_num = len(ids)
553 ids = ids[offset : offset + num]
554 num_books = db.number_of_books_in_virtual_library(vl) if query else total_num
555 ans = {
556 'total_num': total_num,
557 'sort_order': sort_order,
558 'num_books_without_search': num_books,
559 'offset': offset,
560 'num': len(ids),
561 'sort': sort,
562 'base_url': ctx.url_for(search, library_id=db.server_library_id),
563 'query': query,
564 'library_id': db.server_library_id,
565 'book_ids': ids,
566 'vl': vl,
567 }
568 if parse_error is not None:
569 ans['bad_restriction'] = str(parse_error)
570 return ans
571
572
573 @endpoint('/ajax/search/{library_id=None}', postprocess=json)
574 def search(ctx, rd, library_id):
575 """
576 Return the books matching the specified search query.
577 The returned object is a dict with the field book_ids which
578 is a list of matched book ids. For all the other fields in the object, see
579 :func:`search_result`.
580
581 Optional: ?num=100&offset=0&sort=title&sort_order=asc&query=&vl=
582 """
583 db = get_db(ctx, rd, library_id)
584 query = rd.query.get('query')
585 num, offset = get_pagination(rd.query)
586 with db.safe_read_lock:
587 return search_result(
588 ctx,
589 rd,
590 db,
591 query,
592 num,
593 offset,
594 rd.query.get('sort', 'title'),
595 rd.query.get('sort_order', 'asc'),
596 rd.query.get('vl') or '',
597 )
598
599
600 # }}}
601
602
603 @endpoint('/ajax/library-info', postprocess=json)
604 def library_info(ctx, rd):
605 "Return info about available libraries"
606 library_map, default_library = ctx.library_info(rd)
607 return {'library_map': library_map, 'default_library': default_library}