控制模型实例的公共可见性。

django-published的Python项目详细描述


Django已发布PyPI

发布允许您控制模型实例的公共可见性。 在以下情况下很有用!在

You have a model where some number of instances of the model should
be "live". A good example of this would be an Article model, where
you've written some articles that are "live", some that might've
been taken down, some that are still "in progress", and others that
are ready to "go live", but have a "go live" date that's in the
future.

此项目基于django-model-gatekeeperWGBH。在

快速入门

  1. 将“published”添加到您的INSTALLED_APPS
^{pr2}$

把关模型

django published的主要用途是有一个包含许多 实例,但你只希望一些是“活”在网站上。在

一个很好的例子是一个通用的“文章”模型:

  • Some articles are ready-to-go and you want them live to the public;
  • Other articles are still being worked on - you want to be able to preview them, but not take them live JUST yet;
  • Some articles might be pulled (and re-published later)
  • Some articles are ready to be published, but you want them to only go live at a later date.

要开始使用它,只需将 PublishedModel抽象模型, e、 g组:

frompublished.modelsimportPublishedModelclassArticle(PublishedModel):...

超类创建两个字段:

  1. publish_status-这有3个可能的值:

    • NEVER\u AVAILABLE=“永久关闭”-硬连接到永远不可用 公众
    • AVAILABLE_AFTER=“使用live_as_of”日期来确定对象是否 向公众开放
    • AVAILABLE=“始终开启”-硬连线,始终可用于 公众
  2. live_as_of-这是对象在发布状态下应该激活的时间戳 在

您可以通过admin设置publish_statuslive_as_of值。在

通用模型视图

为通用模型视图设置django published很容易!在

以文章模型为例,给出了相应的 查看列表和详细信息视图的代码。在

fromdjango.views.genericimportDetailView,ListViewfrom.modelsimportArticlefrompublished.mixinsimportPublishedListMixin,PublishedDetailMixinclassArticleListView(PublishedListMixin,ListView):model=Articletemplate_name='article/article_list.html'context_object_name='articles'classArticleDetailView(PublishedDetailMixin,DetailView):model=Articletemplate_name='article/article_detail.html'context_object_name='article'

幕后发生了什么:

  1. 在ListView中,django published正在使用 以下规则:

    1. 如果当前用户具有管理员访问权限,请始终包含模型实例。在
    2. 如果publish_status = AVAILABLE,则包括模型实例。在
    3. 如果publish_status = NEVER_AVAILABLE,则不要使用模型实例。在
    4. 如果publish_status = AVAILABLE_AFTER当前日期/时间在之后 live_as_of,包括模型实例。在
    5. 返回已筛选的模型实例列表。在
  2. 在DetailView中,django published遵循相同的规则,但是 如果模型实例不可用,抛出404错误。在

自定义代码

假设你的主页上有一个部分列出了这三个问题 最近的文章。如果您只是沿着以下行创建查询集:

most_recent_articles = Article.objects.order_by(-date_created)[:3]

它将包括文章,无论他们的把关情况如何 是。在

所以有一个helper函数可以将把关规则应用于任何 生成的查询集。在

查询集过滤器

这将获取一个queryset,应用规则并返回一个经过筛选的queryset。在

frompublished.utilsimportqueryset_filter...recent_articles=Article.objects.order_by('-date_created')recent_articles=queryset_filter(recent_articles,is_auth)...

默认情况下,queryset_filter不应用与视图相同的异常 上面是混音。这意味着未发布的模型实例将显示not 如果当前用户具有管理员权限。在

可选的user参数允许您启用这种特殊情况,如下所示。在

queryset_filter(queryset,user=self.request.user)

向公众提供

Note:这只应在模板中使用

如果需要检查对象在Django模板中是否被视为“可用”,可以使用 available_to_public模型属性,如下所示。在

{% for article in article_list %}
    {% if article.available_to_public %}
        I'm published!
    {% endif %}
{% endfor %}

管理界面

django published有几个帮助函数,可以更轻松地添加管理控件。 它们都可以在django-published.admin模块中找到。在

alt test

出版达明

以下所有函数都需要使用PublishedAdmin抽象类 默认的ModelAdmin类。您可以在下面的所有代码中看到这方面的示例。在

只读字段

要使用下面的任何函数,需要向admin实例添加一个字段。 这可以使用add_to_readonly_fields来完成

  1. 一个show_publish_status,它接受live_as_of和{} 字段并从中创建一个友好的字符串

Examp公司le代码:

frompublished.adminimportPublishedAdmin,add_to_readonly_fieldsclassMyModelAdmin(PublishedAdmin):readonly_fields=['my_field_1','my_field_2']+add_to_readonly_fields()

列表显示

要在管理列表视图中显示状态,需要将show_publish_status添加到 list_display

这可以用add_to_list_display方法自动添加,例如:

frompublished.adminimportPublishedAdmin,add_to_list_displayclassMyModelAdmin(PublishedAdmin):list_display=['pk','title',]+add_to_list_display()

字段集

有两种方法可以使用 add_to_fieldsets方法:

作为一个单独的部分

有一个section属性(默认值: True)返回包含gatekeeper字段的整个节元组。 还有一个collapse属性 (默认值:False)使用Django Admin“collapse”类。在

frompublished.adminimportPublishedAdmin,add_to_fieldsetsclassMyModelAdmin(PublishedAdmin):fieldsets=((None,...),add_to_fieldsets(section=True,collapse=False))

包含在

或者您可以将它们作为另一个部分的一部分;在这种情况下,您应该 设置section=False

frompublished.adminimportPublishedAdmin,add_to_fieldsetsclassMyModelAdmin(PublishedAdmin):fieldsets=((None,{'fields':((somesetoffields),add_to_fieldsets(section=False))}),

当然,您可以使用可编辑的live_as_ofpublish_status字段和readonly手动完成所有操作 show_publish_status字段。在

许可证

这个软件是在麻省理工学院许可下发布的。在

Copyright (c) 2019 WGBH Educational Foundation
Copyright (c) 2019-2020 Luke Rogers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java 安卓阻止编译的第三方库的特定网站   java如何使用带回调的send()方法返回元数据?   将双值数组从一个java类传递到另一个java类   java OracleJava7安装程序返回错误代码   smb共享上新创建的Java文件没有用户,也没有组   打印unicode值而不是字符串的Java程序   java将字符串放在括号中的语法意义   java将对象添加到arraylist,除非它已经存在   java正在尝试查找回文数   java如何解决“过时元素引用:导航到上一页时元素未附加到页面文档”   java从无关方法调用超级方法   用于java代码的socket网络设置,以便不同网络/位置上的两台计算机可以使用RMI   用Java字符数组处理C字符数组