from django import template, forms
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.template.base import Variable, VariableDoesNotExist
from django.db.models import get_model
from category.utils.tree import construct_tree
from category.loading import cache
from django.conf import settings
from category.utils.catroot import get_root
from category.utils.render import build_attrs_string
from django.template.defaultfilters import slugify
from category import widgets
Category = get_model('category', 'category')
register = template.Library()
def do_render_node(root, value, template_name):
subtree = None
if root.children.count():
subtree = "\n".join([do_render_node(child, value, template_name = template_name) for child in root.children])
subtree = mark_safe(subtree)
selected = root.node.pk == value
return render_to_string(template_name, {'object' : root, 'subtree' : subtree, 'open' : selected, 'selected' : selected })
do_render_node.is_safe = True
class RenderNodeNode(template.Node):
def __init__(self, root, **kwargs):
self.root = root
self.attrs = kwargs
def render(self, context):
root = Variable(self.root).resolve(context)
attrs = {
'template_name' : 'category/render-node.html',
'value' : None,
}
for key, value in self.attrs.items():
if self.attrs[key] == True:
attrs[key] = True
else:
attrs[key] = Variable(value).resolve(context)
return do_render_node(root, **attrs)
def do_render_tree(provider, root, template_name, collapse, context = None, attrs = {}):
list = provider.get_categories(root)
map = {}
roots = construct_tree(list, map)
return render_to_string(template_name, {'roots' : roots, 'collapse' : collapse, \
'attrs_str': build_attrs_string(attrs), 'attrs' : attrs}, context)
class RenderTreeNode(template.Node):
def __init__(self, provider, root = None, template_name = None, collapse = True, media = 'media', **kwargs):
self.provider, self.root, self.template_name = provider, root, template_name
self.attrs = kwargs
self.collapse = collapse
self.media_key = media
def render(self, context):
provider = Variable(self.provider).resolve(context)
media = forms.Media(css = widgets.CategorySelectWidget.Media.css, \
js = widgets.CategorySelectWidget.Media.js)
context[self.media_key] = media
attrs = {
'id': 'category-tree-{0}'.format(slugify(provider)),
'class' : 'category-tree',
}
for key, value in self.attrs.items():
if self.attrs[key] == True:
attrs[key] = True
else:
attrs[key] = Variable(value).resolve(context)
root = None
if self.root:
root = Variable(self.root).resolve(context)
if self.template_name is None:
template_name = 'category/render-tree.html'
else:
template_name = Variable(self.template_name).resolve(context)
provider = cache.get_object(provider)
if root:
if isinstance(root, Category):
if root.is_root:
root = root
else:
root = root.root
else:
root = get_root(name = root)
else:
root = provider.get_root_category()
cache.add_object(provider, root.pk)
kw = {
'template_name':template_name,
'context': context,
'collapse': self.collapse,
'attrs' : attrs,
}
return do_render_tree(provider, root, **kw)
def do_get_category_url(category, provider = None):
"""
{{ category|get_category_url:"ask.settings" }}
"""
root = category.root
assert root
try:
if provider is not None:
provider = cache.get_object(provider)
else:
provider = cache.get_object(root.pk)
except KeyError:
if settings.DEBUG:
raise VariableDoesNotExist("there is no provider for %s in cache", (root.pk,))
return ''
return provider.get_url(category)
def do_category_count(category, provider = None):
"""
{{ category|category_count:"ask.settings" }}
"""
root = category.root
assert root
try:
if provider is not None:
provider = cache.get_object(provider)
else:
provider = cache.get_object(root.pk)
except KeyError:
if settings.DEBUG:
raise VariableDoesNotExist("there is no provider for %s in cache", (root.pk,))
return ''
return provider.get_count_for(category)
def do_render_tree_tag(parser, token):
"""
{% render_category_tree provider="provider name" root="root name" collapse %}
"""
bits = token.split_contents()
kwargs = {}
for bit in bits[1:]:
chunks = bit.split('=')
if len(chunks) == 2:
kwargs[chunks[0]] = chunks[1]
elif len(chunks) == 1:
kwargs[chunks[0]] = True
if len(bits) < 2 or not kwargs.get('provider', None):
raise template.TemplateSyntaxError("'%s' takes 2-n arguments" % bits[0])
return RenderTreeNode(**kwargs)
def do_render_node_tag(parser, token):
"""
{% render_node node value template_name %}
"""
bits = token.split_contents()
kwargs = {}
for bit in bits[1:]:
chunks = bit.split('=')
if len(chunks) == 2:
kwargs[str(chunks[0])] = chunks[1]
elif len(chunks) == 1:
kwargs[str(chunks[0])] = True
if len(bits) < 2 or not kwargs.get('root', None):
raise template.TemplateSyntaxError("'%s' takes root must be set arguments" % bits[0])
return RenderNodeNode(**kwargs)
register.filter('get_category_url', do_get_category_url)
register.filter('category_count', do_category_count)
register.filter('render_node', do_render_node)
register.tag('render_node', do_render_node_tag)
register.tag('render_category_tree', do_render_tree_tag)
没有合适的资源?快使用搜索试试~ 我知道了~
PyPI 官网下载 | django-content-category-1.0.3.tar.gz
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 147 浏览量
2022-01-10
16:24:07
上传
评论
收藏 19KB GZ 举报
温馨提示
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共54个文件
py:25个
gif:15个
html:6个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
资源来自pypi官网。 资源全名:django-content-category-1.0.3.tar.gz
资源推荐
资源详情
资源评论
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/HTML.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/GIF.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 54 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/277f6345dca0446498fbbc03843436aa_qq_38161040.jpg!1)
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)