Source code for eucrim.procedure.forms

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

from django import forms
from django.urls import reverse
from django.contrib.admin.utils import quote
from django.utils.safestring import mark_safe
from wagtail.admin.forms import WagtailAdminPageForm

from .eur_lex_cellar import get_eur_lex_links


[docs] class ProcedurePageForm(WagtailAdminPageForm): """ Override the standard WagtailAdminPageForm for custom validation that appears as a non-field error see: http://docs.wagtail.io/en/stable/advanced_topics/customisation/page_editing_interface.html#customising-generated-forms # noqa: E501 """ # def __init__(self, data=None, files=None, parent_page=None, *args, **kwargs): # super().__init__(data, files, *args, **kwargs) # # In case the title is generated, the slug will not be automatically filled by the JS # # in the frontend. This code sets the field as "not required" so that the slug is # # automatically generated by the backend in Page.full_clean(). # self.fields['slug'].required = False
[docs] def clean(self): from .models import ProcedurePage cleaned_data = super().clean() # If T-Case, there could not be a referring cour: celex_case_type = cleaned_data.get("celex_case_type") celex_year = cleaned_data.get("celex_year") celex_case = cleaned_data.get("celex_case") referring_court = cleaned_data.get("referring_court") type_of_proceeding = cleaned_data.get("type_of_proceeding") if not all([celex_case_type, celex_year, celex_case]): self.add_error(None, "Invalid: Celex case type, year and case must be set.") alredy_exists = ProcedurePage.objects.filter( celex_case_type=celex_case_type, celex_year=celex_year, celex_case=celex_case, ) if alredy_exists.exists() and alredy_exists.first().id != self.instance.id: alredy_exists_url = reverse( "wagtailadmin_explore", args=(quote(alredy_exists.first().pk),) ) msg = f"Invalid: A case for {celex_case_type} {celex_year}/{celex_case} already exists: {alredy_exists.first().title} <https://eucrim.eu{alredy_exists_url}edit/>" self.add_error(None, mark_safe(msg)) if all( [ celex_case_type == "T", referring_court, ] ): self.add_error( "referring_court", "Invalid: T-case and referring court. Set as C-case or delete the referring court.", ) if all( [ celex_case_type == "C", type_of_proceeding == ProcedurePage.TypeOfProceedingChoiceses.REFERENCE_FOR_PRELIMINARY_RULING, not referring_court, ] ): self.add_error( "referring_court", "Invalid: referring court not set for C-case." ) return cleaned_data
[docs] def save(self, commit=True): page = super().save(commit=False) # Populate our celex link fields and last_update field updates = get_eur_lex_links(page, force=True) if updates: for fieldname, fieldvalue in updates.items(): setattr(page, fieldname, fieldvalue) if commit: page.save() return page
[docs] class ProcedureFilterForm(forms.Form): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label_suffix = "" # Removes ":" as label suffix template_name = "procedure/forms/form.html"
# field_order = [ # "q", # "year", # "categories_foundation", # "categories_institution", # "categories_cooperation", # ]