Source code for eucrim.core.wagtail_hooks

# SPDX-FileCopyrightText: 2024 Thomas Breitner <t.breitner@csl.mpg.de>
#
# SPDX-License-Identifier: EUPL-1.2

from django.conf import settings
from django.utils.html import format_html_join, format_html
from django.utils.safestring import mark_safe
from django.templatetags.static import static

from wagtail import hooks
from wagtail.snippets.models import register_snippet
from wagtail.snippets.views.snippets import SnippetViewSet
from wagtail.admin.ui.tables import UpdatedAtColumn
from wagtail.admin.menu import DismissibleMenuItem

from .models import (
    CategoryFoundation,
    CategoryInstitution,
    CategoryAreasCrime,
    CategoryProceduralCriminalLaw,
    CategoryCooperation,
    CategoryRegion,
)


# Customize the ordering of pages in page choosers by date instead of path (default)
# https://docs.wagtail.io/en/latest/reference/hooks.html#construct-page-chooser-queryset
[docs] @hooks.register("construct_page_chooser_queryset") def show_my_pages_only(pages, request): if "choose-page" in request.path: # Order by date when in a page chooser panel pages.order_by("-first_published_at") # for p in pages: # print( # "Path: {} , Date: {}".format(p.path, p.first_published_at) # ) return pages
[docs] @hooks.register("insert_editor_js") def editor_js(): js_files = [ "core/js/textarea_charcount.js", # 'core/js/toggle_type_fields.js', ] js_includes = format_html_join( "\n", '<script src="{0}{1}"></script>', ((settings.STATIC_URL, filename) for filename in js_files), ) # return js_includes + format_html("""<script></script>""") return js_includes
[docs] @hooks.register("insert_global_admin_css") def global_admin_css(): return format_html('<link rel="stylesheet" href="{}">', static("backend.css"))
[docs] @hooks.register("insert_global_admin_js") def global_admin_js(): js_files = [ "backend.js", ] js_includes = format_html_join( "\n", '<script src="{0}"></script>', ((static(filename),) for filename in js_files), ) return js_includes + mark_safe( """ <script> window.addEventListener('DOMContentLoaded', (event) => { }); </script> """ )
# @hooks.register('insert_global_admin_js') # def global_admin_js(): # return format_html( # '<!-- <script src="{}"></script> -->', static('vendor/select2/select2.min.js') # ) # @hooks.register('insert_global_admin_css') # def global_admin_css(): # return format_html( # # '<!-- <link rel="stylesheet" href="{}"><link rel="stylesheet" href="{}"><link rel="stylesheet" href="{}"> -->', # # static('vendor/select2/select2.min.css'), # # static('vendor/select2-bs-theme/select2-bootstrap4.min.css'), # # '<link rel="stylesheet" href="{}">', static('core/css/wagtail_theme.css'), # )
[docs] @hooks.register("register_help_menu_item") def register_custom_help_item(): return DismissibleMenuItem( "eucrim.eu Help", "/docs/", icon_name="help", order=9999, attrs={"target": "_blank", "rel": "noreferrer"}, name="eucrim-help", )
[docs] class CategoryViewSet(SnippetViewSet): search_fields = ["title", "description"] list_display = ["title", "print_order", "description", UpdatedAtColumn()] ordering = ["print_order", "title"]
[docs] class CategoryFoundationViewSet(CategoryViewSet): model = CategoryFoundation
register_snippet(CategoryFoundationViewSet)
[docs] class CategoryInstitutionViewSet(CategoryViewSet): model = CategoryInstitution
register_snippet(CategoryInstitutionViewSet)
[docs] class CategoryAreasCrimeViewSet(CategoryViewSet): model = CategoryAreasCrime
register_snippet(CategoryAreasCrimeViewSet)
[docs] class CategoryProceduralCriminalLawViewSet(CategoryViewSet): model = CategoryProceduralCriminalLaw
register_snippet(CategoryProceduralCriminalLawViewSet)
[docs] class CategoryCooperationViewSet(CategoryViewSet): model = CategoryCooperation
register_snippet(CategoryCooperationViewSet)
[docs] class CategoryRegionViewSet(CategoryViewSet): model = CategoryRegion
register_snippet(CategoryRegionViewSet)