Fix #6549 (calibre content server fails when listing content of user categories)

Also fix a problem where publishers were not working in user-defined categories
This commit is contained in:
Charles Haley 2010-08-19 14:12:44 +01:00
parent a2e959c7c9
commit 8493206301
2 changed files with 29 additions and 10 deletions

View File

@ -21,7 +21,7 @@ class Item:
return 'name=%s, label=%s, index=%s, exists='%(self.name, self.label, self.index, self.exists) return 'name=%s, label=%s, index=%s, exists='%(self.name, self.label, self.index, self.exists)
class TagCategories(QDialog, Ui_TagCategories): class TagCategories(QDialog, Ui_TagCategories):
category_labels_orig = ['', 'authors', 'series', 'publishers', 'tags'] category_labels_orig = ['', 'authors', 'series', 'publisher', 'tags']
def __init__(self, window, db, on_category=None): def __init__(self, window, db, on_category=None):
QDialog.__init__(self, window) QDialog.__init__(self, window)

View File

@ -99,17 +99,19 @@ def html_to_lxml(raw):
raw = etree.tostring(root, encoding=None) raw = etree.tostring(root, encoding=None)
return etree.fromstring(raw) return etree.fromstring(raw)
def CATALOG_ENTRY(item, base_href, version, updated, ignore_count=False): def CATALOG_ENTRY(item, item_kind, base_href, version, updated,
ignore_count=False, add_kind=False):
id_ = 'calibre:category:'+item.name id_ = 'calibre:category:'+item.name
iid = 'N' + item.name iid = 'N' + item.name
if item.id is not None: if item.id is not None:
iid = 'I' + str(item.id) iid = 'I' + str(item.id)
iid += ':'+item_kind
link = NAVLINK(href = base_href + '/' + hexlify(iid)) link = NAVLINK(href = base_href + '/' + hexlify(iid))
count = _('%d books')%item.count count = (_('%d books') if item.count > 1 else _('%d book'))%item.count
if ignore_count: if ignore_count:
count = '' count = ''
return E.entry( return E.entry(
TITLE(item.name), TITLE(item.name + ('' if not add_kind else ' (%s)'%item_kind)),
ID(id_), ID(id_),
UPDATED(updated), UPDATED(updated),
E.content(count, type='text'), E.content(count, type='text'),
@ -265,15 +267,30 @@ class AcquisitionFeed(NavFeed):
class CategoryFeed(NavFeed): class CategoryFeed(NavFeed):
def __init__(self, items, which, id_, updated, version, offsets, page_url, up_url): def __init__(self, items, which, id_, updated, version, offsets, page_url, up_url, db):
NavFeed.__init__(self, id_, updated, version, offsets, page_url, up_url) NavFeed.__init__(self, id_, updated, version, offsets, page_url, up_url)
base_href = self.base_href + '/category/' + hexlify(which) base_href = self.base_href + '/category/' + hexlify(which)
ignore_count = False ignore_count = False
if which == 'search': if which == 'search':
ignore_count = True ignore_count = True
uc = None
if which.endswith(':'):
# We have a user category. Translate back to original categories
uc = {}
try:
ucs = db.prefs['user_categories']
ucs = ucs.get(which[:-1])
for name, category, index in ucs:
uc[name] = category
except:
import traceback
traceback.print_exc()
uc = None
for item in items: for item in items:
self.root.append(CATALOG_ENTRY(item, base_href, version, updated, if uc: which = uc.get(item.name, which)
ignore_count=ignore_count)) self.root.append(CATALOG_ENTRY(item, which, base_href, version,
updated, ignore_count=ignore_count,
add_kind=uc is not None))
class CategoryGroupFeed(NavFeed): class CategoryGroupFeed(NavFeed):
@ -418,7 +435,7 @@ class OPDSServer(object):
cherrypy.response.headers['Content-Type'] = 'application/atom+xml' cherrypy.response.headers['Content-Type'] = 'application/atom+xml'
return str(CategoryFeed(items, category, id_, updated, version, offsets, return str(CategoryFeed(items, category, id_, updated, version, offsets,
page_url, up_url)) page_url, up_url, self.db))
def opds_navcatalog(self, which=None, version=0, offset=0): def opds_navcatalog(self, which=None, version=0, offset=0):
@ -461,7 +478,7 @@ class OPDSServer(object):
offsets = OPDSOffsets(offset, max_items, len(items)) offsets = OPDSOffsets(offset, max_items, len(items))
items = list(items)[offsets.offset:offsets.offset+max_items] items = list(items)[offsets.offset:offsets.offset+max_items]
ans = CategoryFeed(items, which, id_, updated, version, offsets, ans = CategoryFeed(items, which, id_, updated, version, offsets,
page_url, up_url) page_url, up_url, self.db)
else: else:
class Group: class Group:
def __init__(self, text, count): def __init__(self, text, count):
@ -507,7 +524,9 @@ class OPDSServer(object):
which = which[1:] which = which[1:]
if type_ == 'I': if type_ == 'I':
try: try:
which = int(which) p = which.index(':')
category = which[p+1:]
which = int(which[:p])
except: except:
raise cherrypy.HTTPError(404, 'Tag %r not found'%which) raise cherrypy.HTTPError(404, 'Tag %r not found'%which)