Content server: Make the book size useable in custom book list templates. Fixes #1846839 [Custom book list template doesn't work with {size}](https://bugs.launchpad.net/calibre/+bug/1846839)

This commit is contained in:
Kovid Goyal 2019-10-05 12:50:05 +05:30
parent a87c59e8c8
commit e93a01aa3d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 17 additions and 1 deletions

View File

@ -24,7 +24,7 @@ from calibre.library.field_metadata import category_icon_map
from polyglot.builtins import iteritems, itervalues, range, filter, unicode_type
from polyglot.urllib import quote
IGNORED_FIELDS = frozenset('cover ondevice path marked au_map size'.split())
IGNORED_FIELDS = frozenset('cover ondevice path marked au_map'.split())
def encode_datetime(dateval):

View File

@ -135,6 +135,20 @@ def render_field(field, mi, book_id): # {{{
val = 1
return fmt_sidx(val, use_roman=interface_data.use_roman_numerals_for_series_number)
def process_size(field, fm, name, val):
val = val or 0
mb = 1024 * 1024
def fmt(val, suffix):
ans = f'{val:.1f}'
if ans.endsWith('.0'):
ans = ans[:-2]
return ans + suffix
if val < mb:
return fmt(val / 1024, 'KB')
return fmt(val / mb, 'MB')
name = fm.name or field
datatype = fm.datatype
if field is 'comments' or datatype is 'comments':
@ -160,6 +174,8 @@ def render_field(field, mi, book_id): # {{{
func = process_series
elif field.endswith('_index'):
func = process_series_index
elif field is 'size':
func = process_size
ans = None
if func:
ans = func(field, fm, name, val)