在 Django 中实现通用受保护预览

发布于 2024-10-20 21:24:17 字数 275 浏览 0 评论 0原文

我正在尝试向 Django 管理添加某种通用预览功能。与 Django 内置的现场预览功能相反,此预览应该仅对具有特定权限的登录用户可见。

我的所有内容模型都具有相同的基类,它添加了已发布和未发布等状态。显然,未发布的内容不会出现在网站上,但编辑者仍然应该能够预览未发布的网站。

我在即将发布的 Django 1.3 版本中读到了有关基于类的视图,它可能非常适合以通用方式实现它。对于 Django 1.2,我似乎无法在不触及任何单个视图并添加特定权限检查的情况下提出解决方案。以前有人做过类似的事情吗?

I'm trying to add some kind of generic preview functionality to the Django admin. Opposed to Django's builtin preview-on-site functionality this preview should only be visible to logged in users with specific permissions.

All my content models have the same base class which adds a status like published and unpublished. Obviously unpublished content doesn't appear on the website, but editors should still be able to preview an unpublished site.

I read about class based views in the upcoming Django 1.3 release which might be well suited to implement it in a generic way. With Django 1.2 i can't seem to come up with a solution without touching any single view and adding specific permission checks. Has anyone done something like that before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-10-27 21:24:17

我相信 Django 管理员已经为任何提供 get_absolute_url() 方法的模型的管理页面提供了“现场显示”选项。使用装饰器,应该可以跨模型以通用的方式做到这一点

class MyArticleModel(Article): #extends your abstract Article model
    title = .....
    slug = ......
    body = ......

    @models.permalink
    def get_absolute_url(self): # this puts a 'view on site' link in the model admin page
        return ('views.article_view', [self.slug])

#------ custom article decorator -------------------
from django.http import Http404
from django.shortcuts import get_object_or_404

def article(view, model, key='slug'):
    """ Decorator that takes a model class and returns an instance 
        based on whether the model is viewable by the current user. """
    def worker_function(request, **kwargs):
        selector = {key:kwargs[key]}
        instance = get_object_or_404(model, **selector)
        del kwargs[key] #remove id/slug from view params
        if instance.published or request.user.is_staff() or instance.author is request.user:
            return view(request, article=instance, **kwargs)
        else:
            raise Http404
    return worker_function

#------- urls -----------------

url(r'^article/(?(slug)[\w\-]{10-30})

希望这是信息丰富的(并且希望是正确的;)

, article_view, name='article-view'), url(r'^article/print/(?(id)\d+)

希望这是信息丰富的(并且希望是正确的;)

, article(view=generic.direct_to_template, model=MyArticleModel, key='id'), name='article-print-view' ) #------ views ---------------- from django.shortcuts import render_to_response @article(MyArticleModel) def article(request, article): #do processing! return render_to_response('article_template.html', {'article':instance}, xontext_instance=RequestContext(request) )

希望这是信息丰富的(并且希望是正确的;)

I believe the Django Admin already provides a "show on site" option to the admin pages of any models which provides a get_absolute_url() method. Using decorators, it should be possible to do this in a generic way across models

class MyArticleModel(Article): #extends your abstract Article model
    title = .....
    slug = ......
    body = ......

    @models.permalink
    def get_absolute_url(self): # this puts a 'view on site' link in the model admin page
        return ('views.article_view', [self.slug])

#------ custom article decorator -------------------
from django.http import Http404
from django.shortcuts import get_object_or_404

def article(view, model, key='slug'):
    """ Decorator that takes a model class and returns an instance 
        based on whether the model is viewable by the current user. """
    def worker_function(request, **kwargs):
        selector = {key:kwargs[key]}
        instance = get_object_or_404(model, **selector)
        del kwargs[key] #remove id/slug from view params
        if instance.published or request.user.is_staff() or instance.author is request.user:
            return view(request, article=instance, **kwargs)
        else:
            raise Http404
    return worker_function

#------- urls -----------------

url(r'^article/(?(slug)[\w\-]{10-30})

Hope this is informative (and hopefully correct ;)

, article_view, name='article-view'), url(r'^article/print/(?(id)\d+)

Hope this is informative (and hopefully correct ;)

, article(view=generic.direct_to_template, model=MyArticleModel, key='id'), name='article-print-view' ) #------ views ---------------- from django.shortcuts import render_to_response @article(MyArticleModel) def article(request, article): #do processing! return render_to_response('article_template.html', {'article':instance}, xontext_instance=RequestContext(request) )

Hope this is informative (and hopefully correct ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文