Source code for literature.ui.picker

"""Searchable fields for choosing references in a project's own form."""

from django.core.exceptions import ValidationError
from django_tomselect.app_settings import TomSelectConfig, merge_configs
from django_tomselect.forms import (
    TomSelectModelChoiceField,
    TomSelectModelMultipleChoiceField,
)

from literature.choices import NameRole
from literature.models import Item


[docs] def item_label(item: Item) -> str: """Return the text that names a reference wherever the picker shows it. The title (the citation key when there is none), then the issued year in brackets, then the first contributor. Whatever a reference lacks is left out. Reads ``item_dates`` and ``item_names`` through ``.all()``, so items loaded with those prefetched are labelled without a query. Args: item: The reference to label. Returns: The label. """ label = item.title or item.citation_key year = item.year if year is not None: label = f"{label} ({year})" links = sorted(item.item_names.all(), key=lambda link: link.order) authors = [link for link in links if link.role == NameRole.AUTHOR] credited = authors or links if credited: name = credited[0].name.family or credited[0].name.literal if name: label = f"{label}, {name}" return label
[docs] class ReferencePickerMixin: """What the two reference fields share: a default config and a restriction check. The default config points django-tomselect at the package's search route. A config the developer passes is merged over it, so any django-tomselect setting can be added. django-tomselect validates a submission against the search view's whole queryset, so a chosen reference is also checked against the config's constant ``filter_by`` and ``exclude_by`` conditions. A condition that takes its value from another form field is left to django-tomselect. Args: *args: What the django-tomselect field takes. config: A ``TomSelectConfig`` or a dict of its settings, merged over :attr:`default_config`. **kwargs: What the django-tomselect field takes. """ default_config = TomSelectConfig( url="literature:item-autocomplete", value_field="id", label_field="label", minimum_query_length=1, ) def __init__(self, *args, config=None, **kwargs): if config is not None and not isinstance(config, TomSelectConfig): config = TomSelectConfig(**config) super().__init__( *args, config=merge_configs(self.default_config, config), **kwargs )
[docs] def clean(self, value): """Refuse a chosen reference outside the config's constant conditions.""" cleaned = super().clean(value) chosen = [cleaned] if isinstance(cleaned, Item) else cleaned if chosen and not self.within_restriction([item.pk for item in chosen]): raise ValidationError( self.error_messages["invalid_choice"], code="invalid_choice", params={"value": value}, ) return cleaned
[docs] def within_restriction(self, pks): """Return whether every pk meets the constant conditions. Args: pks: The primary keys of the chosen references. Returns: ``True`` if all of them satisfy the config's constant conditions. """ queryset = Item.objects.filter(pk__in=pks) for spec in self.config.get_normalized_filters(): if spec.source_type == "const": queryset = queryset.filter(**{spec.lookup: self.constant_value(spec)}) for spec in self.config.get_normalized_excludes(): if spec.source_type == "const": queryset = queryset.exclude(**{spec.lookup: self.constant_value(spec)}) return queryset.count() == len(set(pks))
[docs] @staticmethod def constant_value(spec): """Return a constant condition's value, split when its lookup takes a list. Args: spec: A normalized ``filter_by`` or ``exclude_by`` condition. Returns: The condition's value. """ if spec.lookup.rsplit("__", 1)[-1] in ("in", "range"): return spec.source.split(",") return spec.source
# django-tomselect's model mixin and Django's choice field type ``queryset`` differently.
[docs] class ReferenceField(ReferencePickerMixin, TomSelectModelChoiceField): # type: ignore[misc] """Choose one reference by searching the catalogue. On a plain form, declare it as an attribute. On a model form, name it in ``Meta.field_classes`` so Django builds it from the model field. """
[docs] class ReferenceMultipleField( # type: ignore[misc] ReferencePickerMixin, TomSelectModelMultipleChoiceField ): """Choose several references by searching the catalogue. Declared the same way as ``ReferenceField``. """