Package: django-published
Django PublishedPublished allows you to control the public visibility of model instances.
Useful in situations like below!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.This project is based ondjango-model-gatekeeperbyWGBH.Getting StartedInstallationAdd "published" to yourINSTALLED_APPS:INSTALLED_APPS=[...'published',]Setting Up ModelsThe main use fordjango-publishedis where you have a model with many
instances, but you only want some to be "live" on the site.A good example is a generic "Article" model: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.To start using this, all you need to do is subclass thePublishedModelabstract model,
e.g:frompublished.modelsimportPublishedModelclassArticle(PublishedModel):...The superclass creates two fields:publish_status- this has 3 possible values:NEVER_AVAILABLE= "permanently off" - hard-wired to NEVER be available to
the publicAVAILABLE_AFTER= "use live_as_of" date to determine if the object is
available to the publicAVAILABLE= "always on" - hard-wired to be always available to the
publiclive_as_of- this is the timestamp of when the object should go live, if publish_status
isAVAILABLE_AFTERYou set thepublish_statusandlive_as_ofvalues through the admin.The FrontendGeneric Model ViewsSetting updjango-publishedfor generic models views is easy!Using the Article model as an example, here is the corresponding
view code for  listing and detail views.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'What's happening behind the scenes:In the ListView,django-publishedis filtering the model with the
following rules:If the current user has admin access, always include the model instance.Ifpublish_status = AVAILABLE, include the model instance.Ifpublish_status = NEVER_AVAILABLE, DO NOT the model instance.Ifpublish_status = AVAILABLE_AFTER,andthe current date/time is afterlive_as_of, include the model instance.Return the filtered list of model instances.In the DetailView,django-publishedfollows the same rules but will
throw a 404 error if the model instance is not available.Custom CodeSay there's a section on your homepage that gives a list of the three
most recent articles. If you just create a queryset along the lines of:most_recent_articles = Article.objects.order_by(-date_created)[:3]it will include articles regardless of what their gatekeeping situation
is.So there is a helper function to apply the gatekeeping rules to any
queryset you generate.queryset_filterThis takes a queryset, applies the rules and returns a filtered queryset.frompublished.utilsimportqueryset_filter...recent_articles=Article.objects.order_by('-date_created')recent_articles=queryset_filter(recent_articles,is_auth)...By default,queryset_filterdoes not apply the same exceptions as the view
mixins above. This means that unpublished model instances will benotdisplayed
if the current user has admin access.The optionaluserparameter allows you to enable this special case, as seen below.queryset_filter(queryset,user=self.request.user)available_to_publicNote: This should only be used in templatesIf you need to check if an object is considered "available" in a Django template, you can use theavailable_to_publicmodel attribute, as below.{% for article in article_list %}
    {% if article.available_to_public %}
        I'm published!
    {% endif %}
{% endfor %}The Admin Interfacedjango-publishedhas several helper functions to make adding admin controls easier.
All of them can be found in thedjango-published.adminmodule.Setting UpAll of the below functions require the use of thePublishedAdminabstract class instead
of the defaultModelAdminclass. You can see examples of this in all of the code below.Additionally,add_to_readonly_fieldsalso needs to be added toreadonly_fieldsto provide some of
the fields needed later on.frompublished.adminimportPublishedAdminclassArticleAdmin(PublishedAdmin):readonly_fields=['my_field_1','my_field_2']+add_to_readonly_fields()...Adding to List ViewTo show the status in an admin list view,show_publish_statusneeds to be added tolist_displayThis can be added automatically with theadd_to_list_displaymethod, e.g.:frompublished.adminimportPublishedAdmin,add_to_list_displayclassArticleAdmin(PublishedAdmin):list_display=['pk','title',]+add_to_list_display()Adding to Edit ViewTo add the admin controls to your model, useadd_to_fieldsets. Thecollapseattribute can be used
to make the controls hidden by default.frompublished.adminimportPublishedAdmin,add_to_fieldsetsclassMyModelAdmin(PublishedAdmin):fieldsets=((None,...),add_to_fieldsets(section=True,collapse=False))If you don't want to useadd-to_fieldsets, you can also add the fields manually, with the editablelive_as_of,publish_statusfields and the readonlyshow_publish_statusfield.LicenseThis software is released under the MIT license.Copyright (c) 2019 WGBH Educational Foundation
Copyright (c) 2019-2023 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