From c15f91812b83631838feaa38b1ae6ab05f35dbb3 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 3 Sep 2010 16:22:48 +0100 Subject: [PATCH] Add custom column information to standard and mobile versions of the content server --- resources/content_server/gui.js | 11 +++++-- src/calibre/library/server/mobile.py | 47 ++++++++++++++++++++++++---- src/calibre/library/server/xml.py | 34 ++++++++++++++++++++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/resources/content_server/gui.js b/resources/content_server/gui.js index 9c20037207..7dd056b959 100644 --- a/resources/content_server/gui.js +++ b/resources/content_server/gui.js @@ -59,7 +59,14 @@ function render_book(book) { title = title.slice(0, title.length-2); title += ' ({0} MB) '.format(size); } - if (tags) title += '[{0}]'.format(tags); + if (tags) title += 'Tags=[{0}] '.format(tags); + custcols = book.attr("custcols").split(',') + for ( i = 0; i < custcols.length; i++) { + if (custcols[i].length > 0) { + vals = book.attr(custcols[i]).split(':#:', 2); + title += '{0}=[{1}] '.format(vals[0], vals[1]); + } + } title += ''.format(id); title += '
{0}
'.format(comments) // Render authors cell @@ -290,7 +297,7 @@ function layout() { } $(function() { - // document is ready + // document is ready create_table_headers(); // Setup widgets diff --git a/src/calibre/library/server/mobile.py b/src/calibre/library/server/mobile.py index aa7a740972..7642164899 100644 --- a/src/calibre/library/server/mobile.py +++ b/src/calibre/library/server/mobile.py @@ -17,7 +17,7 @@ from calibre.library.server.utils import strftime from calibre.ebooks.metadata import fmt_sidx from calibre.constants import __appname__ from calibre import human_readable -from calibre.utils.date import utcfromtimestamp +from calibre.utils.date import utcfromtimestamp, format_date def CLASS(*args, **kwargs): # class is a reserved word in Python kwargs['class'] = ' '.join(args) @@ -85,7 +85,7 @@ def build_navigation(start, num, total, url_base): # {{{ # }}} -def build_index(books, num, search, sort, order, start, total, url_base): +def build_index(books, num, search, sort, order, start, total, url_base, CKEYS): logo = DIV(IMG(src='/static/calibre.png', alt=__appname__), id='logo') search_box = build_search_box(num, search, sort, order) @@ -123,10 +123,16 @@ def build_index(books, num, search, sort, order, start, total, url_base): series = u'[%s - %s]'%(book['series'], book['series_index']) \ if book['series'] else '' - tags = u'[%s]'%book['tags'] if book['tags'] else '' + tags = u'Tags=[%s]'%book['tags'] if book['tags'] else '' - text = u'\u202f%s %s by %s - %s - %s %s' % (book['title'], series, - book['authors'], book['size'], book['timestamp'], tags) + ctext = '' + for key in CKEYS: + val = book.get(key, None) + if val: + ctext += '%s=[%s] '%tuple(val.split(':#:')) + + text = u'\u202f%s %s by %s - %s - %s %s %s' % (book['title'], series, + book['authors'], book['size'], book['timestamp'], tags, ctext) if last is None: data.text = text @@ -189,6 +195,10 @@ class MobileServer(object): if sort is not None: self.sort(items, sort, (order.lower().strip() == 'ascending')) + CFM = self.db.field_metadata + CKEYS = [key for key in sorted(CFM.get_custom_fields(), + cmp=lambda x,y: cmp(CFM[x]['name'].lower(), + CFM[y]['name'].lower()))] books = [] for record in items[(start-1):(start-1)+num]: book = {'formats':record[FM['formats']], 'size':record[FM['size']]} @@ -209,6 +219,31 @@ class MobileServer(object): book[x] = strftime('%Y/%m/%d %H:%M:%S', record[FM[x]]) book['id'] = record[FM['id']] books.append(book) + for key in CKEYS: + def concat(name, val): + return '%s:#:%s'%(name, unicode(val)) + val = record[CFM[key]['rec_index']] + if val: + datatype = CFM[key]['datatype'] + if datatype in ['comments']: + continue + name = CFM[key]['name'] + if datatype == 'text' and CFM[key]['is_multiple']: + book[key] = concat(name, ', '.join(val.split('|'))) + elif datatype == 'series': + book[key] = concat(name, '%s [%s]'%(val, + fmt_sidx(record[CFM.cc_series_index_column_for(key)]))) + elif datatype == 'datetime': + book[key] = concat(name, + format_date(val, CFM[key]['display'].get('date_format','dd MMM yyyy'))) + elif datatype == 'bool': + if val: + book[key] = concat(name, __builtin__._('Yes')) + else: + book[key] = concat(name, __builtin__._('No')) + else: + book[key] = concat(name, val) + updated = self.db.last_modified() cherrypy.response.headers['Content-Type'] = 'text/html; charset=utf-8' @@ -218,7 +253,7 @@ class MobileServer(object): url_base = "/mobile?search=" + search+";order="+order+";sort="+sort+";num="+str(num) return html.tostring(build_index(books, num, search, sort, order, - start, len(ids), url_base), + start, len(ids), url_base, CKEYS), encoding='utf-8', include_meta_content_type=True, pretty_print=True) diff --git a/src/calibre/library/server/xml.py b/src/calibre/library/server/xml.py index 9db786953e..0d7aabc629 100644 --- a/src/calibre/library/server/xml.py +++ b/src/calibre/library/server/xml.py @@ -15,6 +15,7 @@ from calibre.library.server.utils import strftime from calibre.ebooks.metadata import fmt_sidx from calibre.constants import preferred_encoding from calibre import isbytestring +from calibre.utils.date import format_date E = ElementMaker() @@ -86,6 +87,39 @@ class XMLServer(object): kwargs[x] = serialize(y) if y else '' c = kwargs.pop('comments') + + CFM = self.db.field_metadata + CKEYS = [key for key in sorted(CFM.get_custom_fields(), + cmp=lambda x,y: cmp(CFM[x]['name'].lower(), + CFM[y]['name'].lower()))] + custcols = [] + for key in CKEYS: + def concat(name, val): + return '%s:#:%s'%(name, unicode(val)) + val = record[CFM[key]['rec_index']] + if val: + datatype = CFM[key]['datatype'] + if datatype in ['comments']: + continue + k = str('CF_'+key[1:]) + name = CFM[key]['name'] + custcols.append(k) + if datatype == 'text' and CFM[key]['is_multiple']: + kwargs[k] = concat(name, ', '.join(val.split('|'))) + elif datatype == 'series': + kwargs[k] = concat(name, '%s [%s]'%(val, + fmt_sidx(record[CFM.cc_series_index_column_for(key)]))) + elif datatype == 'datetime': + kwargs[k] = concat(name, + format_date(val, CFM[key]['display'].get('date_format','dd MMM yyyy'))) + elif datatype == 'bool': + if val: + kwargs[k] = concat(name, __builtin__._('Yes')) + else: + kwargs[k] = concat(name, __builtin__._('No')) + else: + kwargs[k] = concat(name, val) + kwargs['custcols'] = ','.join(custcols) books.append(E.book(c, **kwargs)) updated = self.db.last_modified()