Source code for eucrim.treaty.models

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

from django.db import models
from django.shortcuts import render

from django_countries.fields import CountryField
from django_countries import Countries
from modelcluster.fields import ParentalKey

from wagtail.models import Orderable, Page
from wagtail.fields import RichTextField
from wagtail.admin.panels import FieldPanel, InlinePanel
from wagtail.contrib.routable_page.models import RoutablePageMixin, route

from eucrim.core.models.mixins import HideShowInMenusField


[docs] class TreatyCountries(Countries): override = { "CZ": "Czech Republic", # "MK": "FYR Macedonia", "EU": "European Union", }
[docs] class RatificationManager(models.Manager): """Custom manager for Ratifications"""
[docs] def get_queryset(self): return super().get_queryset().order_by("-date_signed")
[docs] class Ratification(models.Model): SIGNATURE = "S" RATIFICATION = "R" ACCESSION = "A" ENTRY_INTO_FORCE = "E" LEGAL_ACT_CHOICES = ( (SIGNATURE, "Signature"), (RATIFICATION, "Ratification"), (ACCESSION, "Accession"), (ENTRY_INTO_FORCE, "Entry into Force"), ) country = CountryField(countries=TreatyCountries) date_signed = models.DateField() legal_act = models.CharField( max_length=1, choices=LEGAL_ACT_CHOICES, ) panels = [ FieldPanel("country"), FieldPanel("date_signed"), FieldPanel("legal_act"), ] objects = RatificationManager()
[docs] class Meta: abstract = True ordering = ["-date_signed"]
[docs] class TreatyPageRatifications(Orderable, Ratification): page = ParentalKey("treaty.TreatyPage", related_name="ratifications")
[docs] class TreatyPage(HideShowInMenusField, Page): parent_page_types = ["treaty.TreatyIndexPage"] subpage_types = [] show_in_menus = False toggle_threshold = 6 # Toggle ratifications for this treaty after X ratifications reference = models.CharField( max_length=255, blank=False, help_text='Reference number, e.g."029"' ) # website url should be predictable # website = models.URLField(blank=True, help_text='Public treaty website, e.g. "http://www.coe.int/en/web/conventions/recent-changes-for-treaties/-/conventions/treaty/029"') # noqa:E501
[docs] def get_admin_display_title(self): return "{}: {}".format(self.reference, super().get_admin_display_title())
@property def get_ratifications_count(self): if self.ratifications: return self.ratifications.count() content_panels = Page.content_panels + [ FieldPanel("reference"), InlinePanel("ratifications", label="Ratifications"), ] class Meta: verbose_name = "Treaty" verbose_name_plural = "Treaties"
[docs] class TreatyIndexPage(HideShowInMenusField, RoutablePageMixin, Page): parent_page_types = ["core.HomePage", "core.StandardPage"] subpage_types = ["treaty.TreatyPage"] max_count = 1 show_in_menus = True intro = RichTextField(blank=True) legend = RichTextField( blank=True, help_text="Legend for legal act statuses.", ) qs = ( TreatyPage.objects.live() .prefetch_related("ratifications") .order_by("-reference") )
[docs] def get_context(self, request, *args, **kwargs): context = super().get_context(request) context["treaties"] = self.qs return context
[docs] @route(r"^$") def default(self, request): """ View function for the default treaty index page """ context = self.get_context(request) return render(request, self.template, context)
[docs] @route(r"^plain/$") def plain(self, request): """ View function for the plain treaty index table """ self.template = "treaty/treaty_index_page_plain.html" context = self.get_context(request) context["page_style"] = "plain" return render(request, self.template, context)
content_panels = Page.content_panels + [ FieldPanel("intro", classname="full"), FieldPanel("legend", classname="full"), ] class Meta: verbose_name = "Treaty" verbose_name_plural = "Treaties"