Server: Show file sizes in the tooltips for the download buttons. Fixes #1708762 [Feature request: display file sizes in content server details screen](https://bugs.launchpad.net/calibre/+bug/1708762)

This commit is contained in:
Kovid Goyal 2017-08-05 16:43:31 +05:30
parent 3482f15c15
commit a054ad6541
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 19 additions and 8 deletions

View File

@ -66,7 +66,15 @@ def add_field(field, db, book_id, ans, field_metadata):
def book_as_json(db, book_id): def book_as_json(db, book_id):
db = db.new_api db = db.new_api
with db.safe_read_lock: with db.safe_read_lock:
ans = {'formats':db._formats(book_id)} fmts = db._formats(book_id, verify_formats=False)
ans = []
fm = {}
for fmt in fmts:
m = db.format_metadata(book_id, fmt)
if m and m.get('size', 0) > 0:
ans.append(fmt)
fm[fmt] = m['size']
ans = {'formats': ans, 'format_sizes': fm}
if not ans['formats'] and not db.has_id(book_id): if not ans['formats'] and not db.has_id(book_id):
return None return None
fm = db.field_metadata fm = db.field_metadata

View File

@ -7,13 +7,13 @@ from elementmaker import E
from gettext import gettext as _ from gettext import gettext as _
from ajax import ajax, encode_query_component from ajax import ajax, encode_query_component
from book_list.globals import get_session_data
from book_list.item_list import create_item, create_item_list from book_list.item_list import create_item, create_item_list
from book_list.library_data import ( from book_list.library_data import (
book_metadata, cover_url, current_library_id, current_virtual_library, book_metadata, cover_url, current_library_id, current_virtual_library,
download_url, library_data, load_status, set_book_metadata download_url, library_data, load_status, set_book_metadata
) )
from book_list.router import back, home, open_book from book_list.router import back, home, open_book
from book_list.globals import get_session_data
from book_list.theme import get_color, get_font_size from book_list.theme import get_color, get_font_size
from book_list.top_bar import add_button, create_top_bar, set_title from book_list.top_bar import add_button, create_top_bar, set_title
from book_list.ui import query_as_href, set_panel_handler, show_panel from book_list.ui import query_as_href, set_panel_handler, show_panel
@ -23,8 +23,8 @@ from dom import add_extra_css, build_rule, clear, svgicon
from modals import create_custom_dialog, error_dialog from modals import create_custom_dialog, error_dialog
from session import get_interface_data from session import get_interface_data
from utils import ( from utils import (
conditional_timeout, debounce, fmt_sidx, parse_url_params, safe_set_inner_html, conditional_timeout, debounce, fmt_sidx, human_readable, parse_url_params,
sandboxed_html safe_set_inner_html, sandboxed_html
) )
from widgets import create_button, create_spinner from widgets import create_button, create_spinner
@ -74,7 +74,7 @@ def href_for_search(name, val):
def on_fmt_click(ev): def on_fmt_click(ev):
fmt = ev.currentTarget.dataset.format fmt = ev.currentTarget.dataset.format
book_id = int(ev.currentTarget.dataset.bookId) book_id = int(ev.currentTarget.dataset.bookId)
title = this title, sz = this
create_custom_dialog(title, def(parent, close_modal): create_custom_dialog(title, def(parent, close_modal):
@ -87,7 +87,8 @@ def on_fmt_click(ev):
E.div(class_='button-box', E.div(class_='button-box',
create_button(_('Read'), 'book', action.bind(None, read_format)), create_button(_('Read'), 'book', action.bind(None, read_format)),
'\xa0', '\xa0',
create_button(_('Download'), 'cloud-download', action.bind(None, download_format)), create_button(_('Download'), 'cloud-download', action.bind(None, download_format),
tooltip=_('File size: {}').format(human_readable(sz))),
) )
)) ))
) )
@ -194,7 +195,8 @@ def render_metadata(mi, table, book_id): # {{{
td.appendChild(E.a( td.appendChild(E.a(
fmt, class_='simple-link', href='javascript:void(0)', fmt, class_='simple-link', href='javascript:void(0)',
title=_('Read or download this book in the {} format').format(fmt), title=_('Read or download this book in the {} format').format(fmt),
onclick=on_fmt_click.bind(mi.title), data_format=fmt, data_book_id='' + book_id)) onclick=on_fmt_click.bind(v'[mi.title, mi.format_sizes[fmt] || 0]'),
data_format=fmt, data_book_id='' + book_id))
if fmt is not val[-1]: if fmt is not val[-1]:
td.appendChild(document.createTextNode(', ')) td.appendChild(document.createTextNode(', '))
@ -424,7 +426,8 @@ def render_book(container_id, book_id):
read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book')) read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book'))
fmt = preferred_format(book_id) fmt = preferred_format(book_id)
download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt), download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt),
_('Download this book in the {} format').format(fmt), download_filename=f'{metadata.title}.{fmt.toLowerCase()}') _('Download this book in the {0} format ({1})').format(fmt, human_readable(metadata.format_sizes[fmt] or 0)),
download_filename=f'{metadata.title}.{fmt.toLowerCase()}')
row = E.div(read_button, '\xa0\xa0\xa0', download_button, style='margin-bottom: 1ex') row = E.div(read_button, '\xa0\xa0\xa0', download_button, style='margin-bottom: 1ex')
if not metadata.formats or not metadata.formats.length: if not metadata.formats or not metadata.formats.length:
row.style.display = 'none' row.style.display = 'none'