From 6145f9ec502998d8f51092eae72dfbb1c0b930b2 Mon Sep 17 00:00:00 2001 From: Sandrava Date: Thu, 25 Jan 2024 13:29:07 +0100 Subject: [PATCH 1/4] 1688: temporarily unpublish or publish a document --- liiweb/views/legislation.py | 3 ++- peachjam/admin.py | 15 +++++++++++++++ .../migrations/0117_coredocument_published.py | 18 ++++++++++++++++++ peachjam/models/core_document_model.py | 5 +++++ peachjam/views/courts.py | 2 +- peachjam/views/document_nature.py | 4 +++- peachjam/views/documents.py | 4 ++++ peachjam/views/gazette.py | 8 ++++++-- peachjam/views/generic_document.py | 4 +++- peachjam/views/generic_views.py | 7 +++++-- peachjam/views/judgment.py | 4 +++- peachjam/views/legal_instrument.py | 4 +++- peachjam/views/legislation.py | 2 +- 13 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 peachjam/migrations/0117_coredocument_published.py diff --git a/liiweb/views/legislation.py b/liiweb/views/legislation.py index 867daa546..dee1a0c89 100644 --- a/liiweb/views/legislation.py +++ b/liiweb/views/legislation.py @@ -17,7 +17,8 @@ class LegislationListView(TemplateView): def get_queryset(self): qs = ( - self.model.objects.distinct("work_frbr_uri") + self.model.objects.exclude(published=False) + .distinct("work_frbr_uri") .order_by("work_frbr_uri", "-date", "language__pk") .preferred_language(get_language(self.request)) ) diff --git a/peachjam/admin.py b/peachjam/admin.py index c01171204..23e01810b 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -371,6 +371,8 @@ class DocumentAdmin(BaseAdmin): "reindex_for_search", "apply_labels", "ensure_source_file_pdf", + "publish", + "unpublish", ] fieldsets = [ @@ -384,6 +386,7 @@ class DocumentAdmin(BaseAdmin): "title", "date", "language", + "published", ] }, ), @@ -587,6 +590,18 @@ def has_change_permission(self, request, obj=None): return True return super().has_change_permission(request, obj=obj) + def publish(self, request, queryset): + queryset.update(published=True) + self.message_user(request, _("Documents published.")) + + publish.short_description = gettext_lazy("Publish selected documents") + + def unpublish(self, request, queryset): + queryset.update(published=False) + self.message_user(request, _("Documents unpublished.")) + + unpublish.short_description = gettext_lazy("Unpublish selected documents") + class TaxonomyForm(MoveNodeForm): def save(self, commit=True): diff --git a/peachjam/migrations/0117_coredocument_published.py b/peachjam/migrations/0117_coredocument_published.py new file mode 100644 index 000000000..7cd759432 --- /dev/null +++ b/peachjam/migrations/0117_coredocument_published.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.16 on 2024-01-24 12:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("peachjam", "0116_copy_and_delete_old_order_outcome_field_data"), + ] + + operations = [ + migrations.AddField( + model_name="coredocument", + name="published", + field=models.BooleanField(default=True, verbose_name="published"), + ), + ] diff --git a/peachjam/models/core_document_model.py b/peachjam/models/core_document_model.py index 126e45de7..fa29aac7b 100644 --- a/peachjam/models/core_document_model.py +++ b/peachjam/models/core_document_model.py @@ -427,6 +427,11 @@ class CoreDocument(PolymorphicModel): help_text=_("Allow this document to be indexed by search engine robots."), ) + published = models.BooleanField( + _("published"), + default=True, + ) + # options for the FRBR URI doctypes frbr_uri_doctypes = FRBR_URI_DOCTYPES labels = models.ManyToManyField(Label, verbose_name=_("labels"), blank=True) diff --git a/peachjam/views/courts.py b/peachjam/views/courts.py index 546dafb99..bb17347d6 100644 --- a/peachjam/views/courts.py +++ b/peachjam/views/courts.py @@ -18,7 +18,7 @@ class CourtDetailView(FilteredDocumentListView): model = Judgment template_name = "peachjam/court_detail.html" navbar_link = "judgments" - queryset = Judgment.objects.prefetch_related("labels") + queryset = Judgment.objects.exclude(published=False).prefetch_related("labels") def get_base_queryset(self): qs = super().get_base_queryset().filter(court=self.court) diff --git a/peachjam/views/document_nature.py b/peachjam/views/document_nature.py index d201a6bbd..91ab57654 100644 --- a/peachjam/views/document_nature.py +++ b/peachjam/views/document_nature.py @@ -7,7 +7,9 @@ class DocumentNatureListView(FilteredDocumentListView): template_name = "peachjam/document_nature_list.html" model = GenericDocument - queryset = GenericDocument.objects.prefetch_related("authors", "nature", "work") + queryset = GenericDocument.objects.exclude(published=False).prefetch_related( + "authors", "nature", "work" + ) def get(self, *args, **kwargs): self.document_nature = get_object_or_404( diff --git a/peachjam/views/documents.py b/peachjam/views/documents.py index 64a3a4560..5a906d874 100644 --- a/peachjam/views/documents.py +++ b/peachjam/views/documents.py @@ -45,6 +45,9 @@ def dispatch(self, request, *args, **kwargs): return redirect(url) raise Http404() + if not obj.published: + raise Http404() + if not exact or portion: url = obj.get_absolute_url() # this translates from /akn/.../~sec_2 to /akn/.../#sec_2 @@ -53,6 +56,7 @@ def dispatch(self, request, *args, **kwargs): return redirect(url) view_class = registry.views.get(obj.doc_type) + if view_class: view = view_class() view.setup(request, *args, **kwargs) diff --git a/peachjam/views/gazette.py b/peachjam/views/gazette.py index 4a7998f0c..9dae5ec7e 100644 --- a/peachjam/views/gazette.py +++ b/peachjam/views/gazette.py @@ -34,7 +34,7 @@ def group_years(years, locality={}): class GazetteListView(TemplateView): - queryset = Gazette.objects.prefetch_related("source_file") + queryset = Gazette.objects.exclude(published=False).prefetch_related("source_file") template_name = "peachjam/gazette_list.html" navbar_link = "gazettes" @@ -85,7 +85,11 @@ def get_year_stats(self, queryset): class GazetteYearView(DocumentListView): model = Gazette - queryset = Gazette.objects.prefetch_related("source_file").order_by("-date") + queryset = ( + Gazette.objects.exclude(published=False) + .prefetch_related("source_file") + .order_by("-date") + ) template_name = "peachjam/gazette_year.html" paginate_by = 0 navbar_link = "gazettes" diff --git a/peachjam/views/generic_document.py b/peachjam/views/generic_document.py index fd41d26c4..0cbd1a547 100644 --- a/peachjam/views/generic_document.py +++ b/peachjam/views/generic_document.py @@ -10,7 +10,9 @@ class DocumentListView(FilteredDocumentListView): template_name = "peachjam/generic_document_list.html" model = GenericDocument navbar_link = "doc" - queryset = GenericDocument.objects.prefetch_related("authors", "nature", "work") + queryset = GenericDocument.objects.exclude(published=False).prefetch_related( + "authors", "nature", "work" + ) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) diff --git a/peachjam/views/generic_views.py b/peachjam/views/generic_views.py index 1bdd954b3..2a8822137 100644 --- a/peachjam/views/generic_views.py +++ b/peachjam/views/generic_views.py @@ -30,7 +30,9 @@ class DocumentListView(ListView): context_object_name = "documents" paginate_by = 50 model = CoreDocument - queryset = CoreDocument.objects.prefetch_related("nature", "work") + queryset = CoreDocument.objects.exclude(published=False).prefetch_related( + "nature", "work" + ) def get_base_queryset(self): return self.queryset if self.queryset is not None else self.model.objects @@ -130,9 +132,10 @@ class BaseDocumentDetailView(DetailView): slug_field = "expression_frbr_uri" slug_url_kwarg = "frbr_uri" context_object_name = "document" + queryset = CoreDocument.objects.exclude(published=False) def get_object(self, *args, **kwargs): - return self.model.objects.get( + return self.queryset.get( expression_frbr_uri=add_slash(self.kwargs.get("frbr_uri")) ) diff --git a/peachjam/views/judgment.py b/peachjam/views/judgment.py index 135afb24f..25ae77249 100644 --- a/peachjam/views/judgment.py +++ b/peachjam/views/judgment.py @@ -14,7 +14,9 @@ def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["court_classes"] = CourtClass.objects.prefetch_related("courts") - context["recent_judgments"] = Judgment.objects.order_by("-date")[:30] + context["recent_judgments"] = Judgment.objects.exclude( + published=False + ).order_by("-date")[:30] context["doc_type"] = "Judgment" return context diff --git a/peachjam/views/legal_instrument.py b/peachjam/views/legal_instrument.py index daa8fa06b..c1db152de 100644 --- a/peachjam/views/legal_instrument.py +++ b/peachjam/views/legal_instrument.py @@ -10,7 +10,9 @@ class LegalInstrumentListView(FilteredDocumentListView): model = LegalInstrument template_name = "peachjam/legal_instrument_list.html" navbar_link = "legal_instruments" - queryset = LegalInstrument.objects.prefetch_related("authors", "nature", "work") + queryset = LegalInstrument.objects.exclude(published=False).prefetch_related( + "authors", "nature", "work" + ) def get_queryset(self): queryset = super(LegalInstrumentListView, self).get_queryset() diff --git a/peachjam/views/legislation.py b/peachjam/views/legislation.py index 043d79a72..5b8fd6846 100644 --- a/peachjam/views/legislation.py +++ b/peachjam/views/legislation.py @@ -17,7 +17,7 @@ class LegislationListView(FilteredDocumentListView): model = Legislation template_name = "peachjam/legislation_list.html" navbar_link = "legislation" - queryset = Legislation.objects.prefetch_related("work") + queryset = Legislation.objects.exclude(published=False).prefetch_related("work") @registry.register_doc_type("legislation") From 2ba19bf0e7d532e3f478484df40097ca8dbb7f0f Mon Sep 17 00:00:00 2001 From: Sandrava Date: Thu, 25 Jan 2024 13:38:21 +0100 Subject: [PATCH 2/4] 1688: reverted changes to generic_views --- peachjam/views/generic_views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/peachjam/views/generic_views.py b/peachjam/views/generic_views.py index 2a8822137..cb83e7b54 100644 --- a/peachjam/views/generic_views.py +++ b/peachjam/views/generic_views.py @@ -132,10 +132,9 @@ class BaseDocumentDetailView(DetailView): slug_field = "expression_frbr_uri" slug_url_kwarg = "frbr_uri" context_object_name = "document" - queryset = CoreDocument.objects.exclude(published=False) def get_object(self, *args, **kwargs): - return self.queryset.get( + return self.model.objects.get( expression_frbr_uri=add_slash(self.kwargs.get("frbr_uri")) ) From 1ba9db5d01f7011a1c3415c2f353428656347044 Mon Sep 17 00:00:00 2001 From: Sandrava Date: Fri, 26 Jan 2024 12:43:50 +0100 Subject: [PATCH 3/4] 1688: review updates --- peachjam/admin.py | 2 +- peachjam/models/core_document_model.py | 2 ++ peachjam/views/courts.py | 2 +- peachjam/views/document_nature.py | 4 +--- peachjam/views/gazette.py | 6 +----- peachjam/views/generic_document.py | 4 +--- peachjam/views/generic_views.py | 10 ++++++---- peachjam/views/legal_instrument.py | 4 +--- peachjam/views/legislation.py | 2 +- 9 files changed, 15 insertions(+), 21 deletions(-) diff --git a/peachjam/admin.py b/peachjam/admin.py index 23e01810b..7f9a3c908 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -386,7 +386,6 @@ class DocumentAdmin(BaseAdmin): "title", "date", "language", - "published", ] }, ), @@ -432,6 +431,7 @@ class DocumentAdmin(BaseAdmin): "toc_json", "content_html_is_akn", "allow_robots", + "published", ], }, ), diff --git a/peachjam/models/core_document_model.py b/peachjam/models/core_document_model.py index fa29aac7b..215ca78c3 100644 --- a/peachjam/models/core_document_model.py +++ b/peachjam/models/core_document_model.py @@ -430,6 +430,8 @@ class CoreDocument(PolymorphicModel): published = models.BooleanField( _("published"), default=True, + db_index=True, + help_text=_("Is this document published and visible on the website?"), ) # options for the FRBR URI doctypes diff --git a/peachjam/views/courts.py b/peachjam/views/courts.py index bb17347d6..546dafb99 100644 --- a/peachjam/views/courts.py +++ b/peachjam/views/courts.py @@ -18,7 +18,7 @@ class CourtDetailView(FilteredDocumentListView): model = Judgment template_name = "peachjam/court_detail.html" navbar_link = "judgments" - queryset = Judgment.objects.exclude(published=False).prefetch_related("labels") + queryset = Judgment.objects.prefetch_related("labels") def get_base_queryset(self): qs = super().get_base_queryset().filter(court=self.court) diff --git a/peachjam/views/document_nature.py b/peachjam/views/document_nature.py index 91ab57654..d201a6bbd 100644 --- a/peachjam/views/document_nature.py +++ b/peachjam/views/document_nature.py @@ -7,9 +7,7 @@ class DocumentNatureListView(FilteredDocumentListView): template_name = "peachjam/document_nature_list.html" model = GenericDocument - queryset = GenericDocument.objects.exclude(published=False).prefetch_related( - "authors", "nature", "work" - ) + queryset = GenericDocument.objects.prefetch_related("authors", "nature", "work") def get(self, *args, **kwargs): self.document_nature = get_object_or_404( diff --git a/peachjam/views/gazette.py b/peachjam/views/gazette.py index 9dae5ec7e..8283f0f3a 100644 --- a/peachjam/views/gazette.py +++ b/peachjam/views/gazette.py @@ -85,11 +85,7 @@ def get_year_stats(self, queryset): class GazetteYearView(DocumentListView): model = Gazette - queryset = ( - Gazette.objects.exclude(published=False) - .prefetch_related("source_file") - .order_by("-date") - ) + queryset = Gazette.objects.prefetch_related("source_file").order_by("-date") template_name = "peachjam/gazette_year.html" paginate_by = 0 navbar_link = "gazettes" diff --git a/peachjam/views/generic_document.py b/peachjam/views/generic_document.py index 0cbd1a547..fd41d26c4 100644 --- a/peachjam/views/generic_document.py +++ b/peachjam/views/generic_document.py @@ -10,9 +10,7 @@ class DocumentListView(FilteredDocumentListView): template_name = "peachjam/generic_document_list.html" model = GenericDocument navbar_link = "doc" - queryset = GenericDocument.objects.exclude(published=False).prefetch_related( - "authors", "nature", "work" - ) + queryset = GenericDocument.objects.prefetch_related("authors", "nature", "work") def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) diff --git a/peachjam/views/generic_views.py b/peachjam/views/generic_views.py index cb83e7b54..933eabf38 100644 --- a/peachjam/views/generic_views.py +++ b/peachjam/views/generic_views.py @@ -30,12 +30,14 @@ class DocumentListView(ListView): context_object_name = "documents" paginate_by = 50 model = CoreDocument - queryset = CoreDocument.objects.exclude(published=False).prefetch_related( - "nature", "work" - ) + queryset = CoreDocument.objects.prefetch_related("nature", "work") def get_base_queryset(self): - return self.queryset if self.queryset is not None else self.model.objects + return ( + self.queryset.exclude(published=False) + if self.queryset is not None + else self.model.objects.exclude(published=False) + ) def get_queryset(self): qs = self.get_base_queryset() diff --git a/peachjam/views/legal_instrument.py b/peachjam/views/legal_instrument.py index c1db152de..daa8fa06b 100644 --- a/peachjam/views/legal_instrument.py +++ b/peachjam/views/legal_instrument.py @@ -10,9 +10,7 @@ class LegalInstrumentListView(FilteredDocumentListView): model = LegalInstrument template_name = "peachjam/legal_instrument_list.html" navbar_link = "legal_instruments" - queryset = LegalInstrument.objects.exclude(published=False).prefetch_related( - "authors", "nature", "work" - ) + queryset = LegalInstrument.objects.prefetch_related("authors", "nature", "work") def get_queryset(self): queryset = super(LegalInstrumentListView, self).get_queryset() diff --git a/peachjam/views/legislation.py b/peachjam/views/legislation.py index 5b8fd6846..043d79a72 100644 --- a/peachjam/views/legislation.py +++ b/peachjam/views/legislation.py @@ -17,7 +17,7 @@ class LegislationListView(FilteredDocumentListView): model = Legislation template_name = "peachjam/legislation_list.html" navbar_link = "legislation" - queryset = Legislation.objects.exclude(published=False).prefetch_related("work") + queryset = Legislation.objects.prefetch_related("work") @registry.register_doc_type("legislation") From 2d34baf953c2751db3bb6eebde61fc4ec97d4316 Mon Sep 17 00:00:00 2001 From: Sandrava Date: Tue, 30 Jan 2024 10:52:07 +0100 Subject: [PATCH 4/4] 1688: review updates --- africanlii/views/home.py | 24 +++++++++++++++--------- liiweb/views/general.py | 8 ++++++-- peachjam/views/home.py | 18 +++++++++++++----- peachjam_search/documents.py | 2 +- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/africanlii/views/home.py b/africanlii/views/home.py index 225a4a2ea..c221352d0 100644 --- a/africanlii/views/home.py +++ b/africanlii/views/home.py @@ -22,15 +22,21 @@ def get_context_data(self, **kwargs): ) context["recent_articles"] = recent_articles - context["recent_soft_law"] = GenericDocument.objects.exclude( - frbr_uri_doctype="doc" - ).order_by("-date")[:5] - context["recent_reports_guides"] = GenericDocument.objects.filter( - frbr_uri_doctype="doc" - ).order_by("-date")[:5] - context["recent_legal_instruments"] = CoreDocument.objects.filter( - frbr_uri_doctype="act" - ).order_by("-date")[:5] + context["recent_soft_law"] = ( + GenericDocument.objects.exclude(published=False) + .exclude(frbr_uri_doctype="doc") + .order_by("-date")[:5] + ) + context["recent_reports_guides"] = ( + GenericDocument.objects.exclude(published=False) + .filter(frbr_uri_doctype="doc") + .order_by("-date")[:5] + ) + context["recent_legal_instruments"] = ( + CoreDocument.objects.exclude(published=False) + .filter(frbr_uri_doctype="act") + .order_by("-date")[:5] + ) context["au_organs"] = AfricanUnionOrgan.objects.prefetch_related("author") context["au_institutions"] = AfricanUnionInstitution.objects.prefetch_related( "author" diff --git a/liiweb/views/general.py b/liiweb/views/general.py index 3120211be..eaaf89e1a 100644 --- a/liiweb/views/general.py +++ b/liiweb/views/general.py @@ -10,8 +10,12 @@ def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["court_classes"] = CourtClass.objects.prefetch_related("courts") - context["recent_judgments"] = Judgment.objects.order_by("-date")[:5] - context["recent_legislation"] = Legislation.objects.order_by("-date")[:10] + context["recent_judgments"] = Judgment.objects.exclude( + published=False + ).order_by("-date")[:5] + context["recent_legislation"] = Legislation.objects.exclude( + published=False + ).order_by("-date")[:10] context["taxonomies"] = Taxonomy.dump_bulk() context["taxonomy_url"] = "taxonomy_detail" context["recent_articles"] = ( diff --git a/peachjam/views/home.py b/peachjam/views/home.py index 004a161b3..cfe3b27ed 100644 --- a/peachjam/views/home.py +++ b/peachjam/views/home.py @@ -16,11 +16,19 @@ class HomePageView(TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - recent_judgments = Judgment.objects.order_by("-date")[:5] - recent_documents = GenericDocument.objects.order_by("-date")[:5] - recent_instruments = LegalInstrument.objects.order_by("-date")[:5] - recent_legislation = Legislation.objects.order_by("-date")[:5] - documents_count = CoreDocument.objects.count() + recent_judgments = Judgment.objects.exclude(published=False).order_by("-date")[ + :5 + ] + recent_documents = GenericDocument.objects.exclude(published=False).order_by( + "-date" + )[:5] + recent_instruments = LegalInstrument.objects.exclude(published=False).order_by( + "-date" + )[:5] + recent_legislation = Legislation.objects.exclude(published=False).order_by( + "-date" + )[:5] + documents_count = CoreDocument.objects.exclude(published=False).count() authors = Author.objects.exclude( Q(genericdocument__isnull=True), diff --git a/peachjam_search/documents.py b/peachjam_search/documents.py index ae4013854..ec349d885 100644 --- a/peachjam_search/documents.py +++ b/peachjam_search/documents.py @@ -116,7 +116,7 @@ class SearchableDocument(Document): ] def should_index_object(self, obj): - if isinstance(obj, ExternalDocument): + if isinstance(obj, ExternalDocument) or not obj.published: return False return True