context_processors.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:grical 作者: wikical 项目源码 文件源码
def global_template_vars(request):
    """
    Adds variables to all templates.

    It uses memcached to minimize hitting the db.
    """
    def get_user():
        if request.user.is_authenticated():
            return ExtendedUser.objects.get( id = request.user.id )
        else:
            return None
    vars_funcs = {
            'SITE_NAME':    lambda: Site.objects.get_current().name,
            'SITE_DOMAIN':  lambda: Site.objects.get_current().domain,
            'USERS_NR':     lambda: User.objects.count(),
            'EVENTS_NR':    lambda: Event.objects.count(),
            'GROUPS_NR':    lambda: Group.objects.count(), }
            # protocol  (computed below)
    vars_dic = cache.get_many( vars_funcs.keys() )
    if not vars_dic:
        vars_dic = {}
        # we get the values
        for key, func in vars_funcs.items():
            vars_dic[ key ] = func()
        # we put the values in the cache
        cache.set_many( vars_dic )
    # we add protocol
    if request.is_secure():
        vars_dic['PROTOCOL'] = "https"
    else:
        vars_dic['PROTOCOL'] = "http"
    vars_dic['VERSION'] = settings.VERSION
    vars_dic['MEDIA_URL'] = settings.MEDIA_URL
    # TODO: think on the trick to get the user out of a signed Django-1.4 cookie
    vars_dic['USER'] = get_user()
    vars_dic['READ_ONLY'] = settings.READ_ONLY
    # return
    return vars_dic
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号