diff --git a/src/calibre/library/server/ajax.py b/src/calibre/library/server/ajax.py index cd41bb267e..eb0e60baae 100644 --- a/src/calibre/library/server/ajax.py +++ b/src/calibre/library/server/ajax.py @@ -24,6 +24,7 @@ from calibre import force_unicode, isbytestring from calibre.library.field_metadata import category_icon_map class Endpoint(object): # {{{ + 'Manage mime-type json serialization, etc.' def __init__(self, mimetype='application/json; charset=utf-8', @@ -119,7 +120,7 @@ class AjaxServer(object): # Get book metadata {{{ def ajax_book_to_json(self, book_id, get_category_urls=True, - device_compatible=False): + device_compatible=False, device_for_template=None): mi = self.db.get_metadata(book_id, index_is_id=True) if not device_compatible: @@ -191,12 +192,24 @@ class AjaxServer(object): else: series = '' data['_series_sort_'] = series + if device_for_template: + import posixpath + from calibre.devices.utils import create_upload_path + from calibre.utils.filenames import ascii_filename as sanitize + from calibre.customize.ui import device_plugins + + for device_class in device_plugins(): + if device_class.__class__.__name__ == device_for_template: + template = device_class.save_template() + data['_filename_'] = create_upload_path(mi, book_id, + template, sanitize, path_type=posixpath) + break return data, mi.last_modified @Endpoint(set_last_modified=False) def ajax_book(self, book_id, category_urls='true', id_is_uuid='false', - device_compatible='false'): + device_compatible='false', device_for_template=None): ''' Return the metadata of the book as a JSON dictionary. @@ -213,7 +226,8 @@ class AjaxServer(object): book_id = int(book_id) data, last_modified = self.ajax_book_to_json(book_id, get_category_urls=category_urls.lower()=='true', - device_compatible=device_compatible.lower()=='true') + device_compatible=device_compatible.lower()=='true', + device_for_template=device_for_template) except: raise cherrypy.HTTPError(404, 'No book with id: %r'%book_id) @@ -223,7 +237,7 @@ class AjaxServer(object): return data @Endpoint(set_last_modified=False) - def ajax_books(self, ids=None, category_urls='true', id_is_uuid='false'): + def ajax_books(self, ids=None, category_urls='true', id_is_uuid='false', device_for_template=None): ''' Return the metadata for a list of books specified as a comma separated list of ids. The metadata is returned as a dictionary mapping ids to @@ -250,7 +264,7 @@ class AjaxServer(object): for book_id in ids: try: data, last_modified = self.ajax_book_to_json(book_id, - get_category_urls=gcu) + get_category_urls=gcu, device_for_template=device_for_template) except: ans[book_id] = None else: @@ -617,5 +631,4 @@ class AjaxServer(object): 'book_ids':ids } - # }}}