def categories(ctx, rd, library_id): ''' Return the list of top-level categories as a list of dictionaries. Each dictionary is of the form:: { 'name': Display Name, 'url':URL that gives the JSON object corresponding to all entries in this category, 'icon': URL to icon of this category, 'is_category': False for the All Books and Newest categories, True for everything else } ''' db = get_db(ctx, rd, library_id) with db.safe_read_lock: ans = {} categories = ctx.get_categories(rd, db) category_meta = db.field_metadata library_id = db.server_library_id def getter(x): return category_meta[x]['name'] displayed_custom_fields = custom_fields_to_display(db) for category in sorted(categories, key=lambda x: sort_key(getter(x))): if len(categories[category]) == 0: continue if category in ('formats', 'identifiers'): continue meta = category_meta.get(category, None) if meta is None: continue if category_meta.is_ignorable_field(category) and \ category not in displayed_custom_fields: continue display_name = meta['name'] if category.startswith('@'): category = category.partition('.')[0] display_name = category[1:] url = force_unicode(category) icon = category_icon(category, meta) ans[url] = (display_name, icon) ans = [{'url':k, 'name':v[0], 'icon':v[1], 'is_category':True} for k, v in ans.iteritems()] ans.sort(key=lambda x: sort_key(x['name'])) for name, url, icon in [ (_('All books'), 'allbooks', 'book.png'), (_('Newest'), 'newest', 'forward.png'), ]: ans.insert(0, {'name':name, 'url':url, 'icon':icon, 'is_category':False}) for c in ans: c['url'] = ctx.url_for(globals()['category'], encoded_name=encode_name(c['url']), library_id=library_id) c['icon'] = ctx.url_for(get_icon, which=c['icon']) return ans
def categories(ctx, rd, library_id): """ Return the list of top-level categories as a list of dictionaries. Each dictionary is of the form:: { 'name': Display Name, 'url':URL that gives the JSON object corresponding to all entries in this category, 'icon': URL to icon of this category, 'is_category': False for the All Books and Newest categories, True for everything else } """ db = get_db(ctx, library_id) with db.safe_read_lock: ans = {} categories = ctx.get_categories(rd, db) category_meta = db.field_metadata library_id = db.server_library_id def getter(x): return category_meta[x]["name"] displayed_custom_fields = custom_fields_to_display(db) for category in sorted(categories, key=lambda x: sort_key(getter(x))): if len(categories[category]) == 0: continue if category in ("formats", "identifiers"): continue meta = category_meta.get(category, None) if meta is None: continue if category_meta.is_ignorable_field(category) and category not in displayed_custom_fields: continue display_name = meta["name"] if category.startswith("@"): category = category.partition(".")[0] display_name = category[1:] url = force_unicode(category) icon = category_icon(category, meta) ans[url] = (display_name, icon) ans = [{"url": k, "name": v[0], "icon": v[1], "is_category": True} for k, v in ans.iteritems()] ans.sort(key=lambda x: sort_key(x["name"])) for name, url, icon in [(_("All books"), "allbooks", "book.png"), (_("Newest"), "newest", "forward.png")]: ans.insert(0, {"name": name, "url": url, "icon": icon, "is_category": False}) for c in ans: c["url"] = ctx.url_for(globals()["category"], encoded_name=encode_name(c["url"]), library_id=library_id) c["icon"] = ctx.url_for(get_icon, which=c["icon"]) return ans