def category(parent_id=ROOT_CAT_ID): ''' This will render the main page with the selected category defined by parent_id, and with the content that it contains ''' contents = get_images_for_category(parent_id) if request.is_xhr: categories = [c.jsond() for c in CategoryModel.all().filter('parent_id', parent_id).filter('visible', True)] categories = sorted(categories, key=lambda c: c.order) return jsonify(categories=categories, contents=contents) else: category = CategoryModel.get_by_id(parent_id) path = None if category: path = category.get_path_ids_to_root() + [category.key().id()] categories = [] for cat in CategoryModel.all().filter('parent_id', ROOT_CAT_ID).filter('visible', True): if path is not None and cat.key().id() in path: init_sub_tree_along_path(cat, path) categories += [cat] pass pass categories = sorted(categories, key=lambda c: c.order) contents = sorted(contents, key=lambda c: c['order']) return render_template('category/index.html', categories=categories, contents=contents, current_category=category) pass
def category(parent_id=ROOT_CAT_ID): ''' This will render the main page with the selected category defined by parent_id, and with the content that it contains ''' contents = get_images_for_category(parent_id) if request.is_xhr: categories = [ c.jsond() for c in CategoryModel.all().filter( 'parent_id', parent_id).filter('visible', True) ] categories = sorted(categories, key=lambda c: c.order) return jsonify(categories=categories, contents=contents) else: category = CategoryModel.get_by_id(parent_id) path = None if category: path = category.get_path_ids_to_root() + [category.key().id()] categories = [] for cat in CategoryModel.all().filter('parent_id', ROOT_CAT_ID).filter( 'visible', True): if path is not None and cat.key().id() in path: init_sub_tree_along_path(cat, path) categories += [cat] pass pass categories = sorted(categories, key=lambda c: c.order) contents = sorted(contents, key=lambda c: c['order']) return render_template('category/index.html', categories=categories, contents=contents, current_category=category) pass
def init_sub_tree_along_path(category, path, visible_only=True): ''' Initializes the subcategories for the category recursively, if the category is in the path ''' category.subcategories = [] query_set = CategoryModel.all().filter('parent_id', category.key().id()) if visible_only: query_set.filter('visible', True) for cat in query_set: if cat.key().id() in path: init_sub_tree_along_path(cat, path) pass category.subcategories += [cat] pass