Source code for eucrim.association.models

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

from django.db import models

from wagtail.models import Orderable, Page
from wagtail.fields import RichTextField
from wagtail.admin.panels import FieldPanel, InlinePanel, FieldRowPanel
from wagtail.search import index

from modelcluster.fields import ParentalKey

from django_countries.fields import CountryField
from django_countries import Countries

# from eucrim.core.utils import get_promote_panels_without_show_in_menus
from eucrim.core.models.mixins import HideShowInMenusField


[docs] class AssociationCountries(Countries): only = [ "AT", "BE", "BG", "HR", "CY", ("CZ", "Czech Republic"), "DK", "EE", "FI", "FR", "DE", "GE", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MK", "MN", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "CH", "TR", "GB", ("EU", "European Union"), ]
[docs] class AssociationIndexPage(HideShowInMenusField, Page): parent_page_types = ["core.HomePage"] subpage_types = ["association.AssociationPage"] max_count = 1 show_in_menus = True description = RichTextField(blank=True)
[docs] def get_context(self, request, *args, **kwargs): context = super().get_context(request) # .order_by(): we need to order in the template, as we could not access # country__name context["associations"] = ( AssociationPage.objects.live() .prefetch_related("contact_set") .order_by("title") ) return context
content_panels = Page.content_panels + [ FieldPanel("description"), ] search_fields = Page.search_fields + [ index.SearchField("description"), ]
[docs] class ContactSet(index.Indexed, models.Model): function = models.CharField(max_length=254, blank=True) name = models.CharField(max_length=254) is_contact_point = models.BooleanField(default=False) position = models.CharField( max_length=254, blank=True, help_text='Position at there "native" institution' ) address = models.TextField(blank=True) email = models.EmailField(blank=True) tel = models.CharField(max_length=30, blank=True) panels = [ FieldPanel("function"), FieldPanel("name"), FieldPanel("position"), FieldPanel("address"), FieldRowPanel( [ FieldPanel("email"), FieldPanel("tel"), ] ), FieldPanel("is_contact_point"), ] search_fields = [ index.SearchField("name"), ]
[docs] class Meta: abstract = True
[docs] class AssociationPageContactSets(Orderable, ContactSet): page = ParentalKey("association.AssociationPage", related_name="contact_set")
[docs] class AssociationPage(HideShowInMenusField, Page): parent_page_types = ["association.AssociationIndexPage"] subpage_types = [] show_in_menus = False title_native = models.CharField(max_length=255, blank=True) abbr = models.CharField(max_length=255, blank=True) website = models.URLField(blank=True) country = CountryField(countries=AssociationCountries) note = RichTextField( blank=True, verbose_name="Description", features=["ol", "ul", "bold", "italic", "link", "document-link"], )
[docs] def get_admin_display_title(self): return "{}: {}".format(self.country, super().get_admin_display_title())
content_panels = Page.content_panels + [ FieldPanel("title_native"), FieldPanel("abbr"), FieldPanel("website"), FieldPanel("country"), FieldPanel("note"), InlinePanel("contact_set", label="Contacts"), ] search_fields = Page.search_fields + [ index.SearchField("title_native"), index.SearchField("note"), ] # promote_panels = get_promote_panels_without_show_in_menus(Page.promote_panels) class Meta: verbose_name = "Association" verbose_name_plural = "Associations" ordering = ["title"]
# https://github.com/wagtail/wagtail/issues/161 # AFTER the page class definition. # AssociationPage._meta.get_field('slug').default='blank-slug'