Source code for literature.models

"""Django models: a relational form of the CSL JSON 1.0.2 bibliographic data format."""

from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from partial_dates import PartialDateField

from literature.choices import DateType, IdentifierType, ItemType, NameRole
from literature.utils import doi_url
from literature.validators import validate_identifier


[docs] class Item(models.Model): """CSL JSON top-level item object. Stores all scalar/string/number CSL JSON fields as columns. Name-variables, date-variables, and identifiers are stored in related models (ItemName, ItemDate, ItemIdentifier). Maps to the top-level item object of the CSL JSON schema (https://resource.citationstyles.org/schema/v1.0/input/json/csl-data.json). """ citation_key = models.CharField( max_length=255, db_index=True, verbose_name=_("citation key"), help_text=_("CSL JSON: citation-key (or id fallback). Need not be unique."), ) type = models.CharField( max_length=30, choices=ItemType.choices, verbose_name=_("type"), db_index=True, help_text=_("CSL JSON: type"), ) title = models.CharField( max_length=1000, blank=True, db_index=True, verbose_name=_("title"), help_text=_("CSL JSON: title"), ) title_short = models.CharField( max_length=500, blank=True, verbose_name=_("short title"), help_text=_("CSL JSON: title-short (also imports deprecated shortTitle)"), ) original_title = models.CharField( max_length=500, blank=True, verbose_name=_("original title"), help_text=_("CSL JSON: original-title"), ) container_title = models.CharField( max_length=500, blank=True, db_index=True, verbose_name=_("container title"), help_text=_("CSL JSON: container-title"), ) container_title_short = models.CharField( max_length=255, blank=True, verbose_name=_("container title short"), help_text=_("CSL JSON: container-title-short"), ) collection_title = models.CharField( max_length=500, blank=True, verbose_name=_("collection title"), help_text=_("CSL JSON: collection-title"), ) volume_title = models.CharField( max_length=500, blank=True, verbose_name=_("volume title"), help_text=_("CSL JSON: volume-title"), ) volume_title_short = models.CharField( max_length=255, blank=True, verbose_name=_("volume title short"), help_text=_("CSL JSON: volume-title-short"), ) part_title = models.CharField( max_length=500, blank=True, verbose_name=_("part title"), help_text=_("CSL JSON: part-title"), ) reviewed_title = models.CharField( max_length=500, blank=True, verbose_name=_("reviewed title"), help_text=_("CSL JSON: reviewed-title"), ) reviewed_genre = models.CharField( max_length=100, blank=True, verbose_name=_("reviewed genre"), help_text=_("CSL JSON: reviewed-genre"), ) abstract = models.TextField( blank=True, verbose_name=_("abstract"), help_text=_("CSL JSON: abstract"), ) note = models.TextField( blank=True, verbose_name=_("note"), help_text=_("CSL JSON: note"), ) annote = models.TextField( blank=True, verbose_name=_("annotation"), help_text=_("CSL JSON: annote"), ) publisher = models.CharField( max_length=255, blank=True, db_index=True, verbose_name=_("publisher"), help_text=_("CSL JSON: publisher"), ) publisher_place = models.CharField( max_length=255, blank=True, verbose_name=_("publisher place"), help_text=_("CSL JSON: publisher-place"), ) original_publisher = models.CharField( max_length=255, blank=True, verbose_name=_("original publisher"), help_text=_("CSL JSON: original-publisher"), ) original_publisher_place = models.CharField( max_length=255, blank=True, verbose_name=_("original publisher place"), help_text=_("CSL JSON: original-publisher-place"), ) event_title = models.CharField( max_length=500, blank=True, verbose_name=_("event title"), help_text=_("CSL JSON: event-title (also imports deprecated event)"), ) event_place = models.CharField( max_length=255, blank=True, verbose_name=_("event place"), help_text=_("CSL JSON: event-place"), ) volume = models.CharField( max_length=50, blank=True, verbose_name=_("volume"), help_text=_("CSL JSON: volume (string-or-number stored as string)"), ) issue = models.CharField( max_length=50, blank=True, verbose_name=_("issue"), help_text=_("CSL JSON: issue (string-or-number stored as string)"), ) page = models.CharField( max_length=100, blank=True, verbose_name=_("page"), help_text=_("CSL JSON: page (e.g. '171-175')"), ) page_first = models.CharField( max_length=50, blank=True, verbose_name=_("page first"), help_text=_("CSL JSON: page-first (string-or-number stored as string)"), ) number = models.CharField( max_length=50, blank=True, verbose_name=_("number"), help_text=_("CSL JSON: number"), ) number_of_pages = models.CharField( max_length=50, blank=True, verbose_name=_("number of pages"), help_text=_("CSL JSON: number-of-pages (string-or-number stored as string)"), ) number_of_volumes = models.CharField( max_length=50, blank=True, verbose_name=_("number of volumes"), help_text=_("CSL JSON: number-of-volumes (string-or-number stored as string)"), ) edition = models.CharField( max_length=50, blank=True, verbose_name=_("edition"), help_text=_("CSL JSON: edition (string-or-number, e.g. '2nd', 3)"), ) version = models.CharField( max_length=50, blank=True, verbose_name=_("version"), help_text=_("CSL JSON: version"), ) chapter_number = models.CharField( max_length=50, blank=True, verbose_name=_("chapter number"), help_text=_("CSL JSON: chapter-number (string-or-number stored as string)"), ) collection_number = models.CharField( max_length=50, blank=True, verbose_name=_("collection number"), help_text=_("CSL JSON: collection-number (string-or-number stored as string)"), ) section = models.CharField( max_length=100, blank=True, verbose_name=_("section"), help_text=_("CSL JSON: section"), ) part = models.CharField( max_length=50, blank=True, verbose_name=_("part"), help_text=_("CSL JSON: part (string-or-number stored as string)"), ) supplement = models.CharField( max_length=100, blank=True, verbose_name=_("supplement"), help_text=_("CSL JSON: supplement (string-or-number stored as string)"), ) printing = models.CharField( max_length=50, blank=True, verbose_name=_("printing"), help_text=_("CSL JSON: printing (string-or-number stored as string)"), ) status = models.CharField( max_length=50, blank=True, verbose_name=_("status"), help_text=_("CSL JSON: status"), ) medium = models.CharField( max_length=100, blank=True, verbose_name=_("medium"), help_text=_("CSL JSON: medium"), ) genre = models.CharField( max_length=100, blank=True, verbose_name=_("genre"), help_text=_("CSL JSON: genre"), ) language = models.CharField( max_length=10, blank=True, verbose_name=_("language"), help_text=_("CSL JSON: language (BCP 47 tag, e.g. 'en')"), ) archive = models.CharField( max_length=255, blank=True, verbose_name=_("archive"), help_text=_("CSL JSON: archive"), ) archive_collection = models.CharField( max_length=255, blank=True, verbose_name=_("archive collection"), help_text=_("CSL JSON: archive_collection (note: underscored in CSL JSON)"), ) archive_location = models.CharField( max_length=255, blank=True, verbose_name=_("archive location"), help_text=_("CSL JSON: archive_location (note: underscored in CSL JSON)"), ) archive_place = models.CharField( max_length=255, blank=True, verbose_name=_("archive place"), help_text=_("CSL JSON: archive-place"), ) authority = models.CharField( max_length=255, blank=True, verbose_name=_("authority"), help_text=_("CSL JSON: authority"), ) jurisdiction = models.CharField( max_length=255, blank=True, verbose_name=_("jurisdiction"), help_text=_("CSL JSON: jurisdiction"), ) call_number = models.CharField( max_length=100, blank=True, verbose_name=_("call number"), help_text=_("CSL JSON: call-number"), ) dimensions = models.CharField( max_length=100, blank=True, verbose_name=_("dimensions"), help_text=_("CSL JSON: dimensions"), ) division = models.CharField( max_length=100, blank=True, verbose_name=_("division"), help_text=_("CSL JSON: division"), ) scale = models.CharField( max_length=50, blank=True, verbose_name=_("scale"), help_text=_("CSL JSON: scale"), ) source = models.CharField( max_length=255, blank=True, verbose_name=_("source"), help_text=_("CSL JSON: source"), ) references = models.TextField( blank=True, verbose_name=_("references"), help_text=_("CSL JSON: references"), ) journal_abbreviation = models.CharField( max_length=100, blank=True, verbose_name=_("journal abbreviation"), help_text=_("CSL JSON: journalAbbreviation (camelCase in CSL JSON)"), ) keyword = models.TextField( blank=True, verbose_name=_("keyword"), help_text=_("CSL JSON: keyword (single comma-separated string)"), ) categories = models.JSONField( null=True, blank=True, verbose_name=_("categories"), help_text=_("CSL JSON: categories (string array)"), ) custom = models.JSONField( null=True, blank=True, verbose_name=_("custom"), help_text=_("CSL JSON: custom (arbitrary key-value pairs)"), ) created = models.DateTimeField(auto_now_add=True, verbose_name=_("created")) modified = models.DateTimeField(auto_now=True, verbose_name=_("modified")) class Meta: verbose_name = _("item") verbose_name_plural = _("items") ordering = ["-created"] def __str__(self) -> str: """Return truncated title (≤80 chars), falling back to citation_key.""" if self.title: return self.title[:80] + "…" if len(self.title) > 80 else self.title return self.citation_key
[docs] def get_absolute_url(self) -> str: """Return the reference's page in the opt-in front end, or raise ``NoReverseMatch`` without it.""" return reverse("literature:item-detail", kwargs={"pk": self.pk})
@property def doi(self) -> str | None: """Return the DOI among the item's identifiers, or ``None`` if it has none. Reads ``item_identifiers.all()``, so a queryset that prefetches ``item_identifiers`` answers it without a query per item. Returns: The DOI value, or ``None``. """ for identifier in self.item_identifiers.all(): if identifier.type == IdentifierType.DOI: return identifier.value return None @property def doi_url(self) -> str | None: """Return the ``https://doi.org/`` link for the item's DOI, or ``None``.""" return doi_url(self.doi) @property def year(self) -> int | None: """Return the year of the issued date, or ``None`` when no year is stored. An issued date held only as text, such as "in press", has no year. Reads ``item_dates.all()``, so a queryset that prefetches ``item_dates`` answers it without a query per item. Returns: The year, or ``None``. """ for item_date in self.item_dates.all(): if item_date.date_type == DateType.ISSUED and item_date.begin: year: int = item_date.begin.year return year return None
[docs] def names_in_role(self, role: str) -> list["Name"]: """Return the names credited in ``role``, in their stored order. Reads ``item_names.all()``, so a queryset that prefetches ``item_names__name`` answers it without a query per item. Args: role: A ``NameRole`` value. Returns: The names, in order. """ return [ item_name.name for item_name in self.item_names.all() if item_name.role == role ]
@property def authors(self) -> list["Name"]: """Return the item's authors, in their stored order.""" return self.names_in_role(NameRole.AUTHOR) @property def editors(self) -> list["Name"]: """Return the item's editors, in their stored order.""" return self.names_in_role(NameRole.EDITOR)
[docs] class Name(models.Model): """CSL JSON name-variable definition. Stores all constituent parts of a personal or institutional name as defined in the CSL JSON 1.0.2 name-variable schema. Names are shared across items; the ItemName through-model records the role and ordered position. Maps to the CSL JSON name-variable object. """ family = models.CharField( max_length=255, blank=True, verbose_name=_("family name"), help_text=_("CSL JSON: family"), ) given = models.CharField( max_length=255, blank=True, verbose_name=_("given name"), help_text=_("CSL JSON: given"), ) dropping_particle = models.CharField( max_length=50, blank=True, verbose_name=_("dropping particle"), help_text=_("CSL JSON: dropping-particle (e.g. 'von', 'van')"), ) non_dropping_particle = models.CharField( max_length=50, blank=True, verbose_name=_("non-dropping particle"), help_text=_("CSL JSON: non-dropping-particle (e.g. 'van der', 'de')"), ) suffix = models.CharField( max_length=50, blank=True, verbose_name=_("suffix"), help_text=_("CSL JSON: suffix (e.g. 'Jr.', 'III')"), ) literal = models.CharField( max_length=500, blank=True, verbose_name=_("literal"), help_text=_("CSL JSON: literal (institutional names or unparsed strings)"), ) comma_suffix = models.BooleanField( default=False, verbose_name=_("comma suffix"), help_text=_("CSL JSON: comma-suffix"), ) static_ordering = models.BooleanField( default=False, verbose_name=_("static ordering"), help_text=_("CSL JSON: static-ordering (e.g. for East Asian names)"), ) parse_names = models.BooleanField( default=False, verbose_name=_("parse names"), help_text=_("CSL JSON: parse-names (signal to CSL processor)"), ) created = models.DateTimeField(auto_now_add=True, verbose_name=_("created")) modified = models.DateTimeField(auto_now=True, verbose_name=_("modified")) class Meta: verbose_name = _("name") verbose_name_plural = _("names") indexes = [ models.Index(fields=["family", "given"], name="name_family_given_idx"), ] def __str__(self) -> str: """Return 'family, given' or literal when family/given are empty.""" if self.family or self.given: parts = filter(None, [self.family, self.given]) return ", ".join(parts) return self.literal or f"Name #{self.pk}"
[docs] class ItemName(models.Model): """Ordered through-model linking Name to Item with a contributor role. Provides explicit position ordering of contributors within each ``(item, role)`` scope via the ``order`` field: the author list, the editor list, and so on are each numbered independently from zero, so reordering one role never disturbs another. The position is assigned in :meth:`save` on first insert. See ADR-0005. Maps to the name-variable array entries of a CSL JSON item, such as its author and editor arrays. """ item = models.ForeignKey( Item, on_delete=models.CASCADE, related_name="item_names", verbose_name=_("item"), help_text=_("The bibliographic item this name is associated with."), ) name = models.ForeignKey( Name, on_delete=models.CASCADE, related_name="item_names", verbose_name=_("name"), help_text=_("The name record for this contributor."), ) role = models.CharField( max_length=30, choices=NameRole.choices, verbose_name=_("role"), help_text=_("CSL JSON name-variable field (e.g. author, editor, translator)."), ) order = models.PositiveIntegerField( editable=False, verbose_name=_("order"), help_text=_("Position of this contributor within the (item, role) group."), ) class Meta: verbose_name = _("item name") verbose_name_plural = _("item names") ordering = ["item", "role", "order"] constraints = [ models.UniqueConstraint( fields=["item", "role", "name"], name="unique_name_per_role_per_item", ) ] indexes = [ models.Index( fields=["item", "role", "order"], name="itemname_item_role_order_idx" ), models.Index(fields=["name", "role"], name="itemname_name_role_idx"), ]
[docs] def save(self, *args, **kwargs): """Append a new row to the end of its ``(item, role)`` sequence.""" if self._state.adding and self.order is None: last = ( ItemName.objects.filter(item=self.item, role=self.role) .aggregate(models.Max("order")) .get("order__max") ) self.order = 0 if last is None else last + 1 super().save(*args, **kwargs)
def __str__(self) -> str: """Return '<name> as <role> on <item>'.""" return f"{self.name} as {self.role} on {self.item}"
[docs] class ItemDate(models.Model): """CSL JSON date-variable instance for a specific date slot on an item. Stores partial dates using django-partial-dates' PartialDateField, supporting year-only, year-month, and full-date precision. Date ranges are represented by setting both begin and end. Maps to a CSL JSON date-variable object keyed by slot name, such as ``issued``, ``accessed`` or ``event-date``. """ item = models.ForeignKey( Item, on_delete=models.CASCADE, related_name="item_dates", verbose_name=_("item"), help_text=_("The bibliographic item this date belongs to."), ) date_type = models.CharField( max_length=20, choices=DateType.choices, verbose_name=_("date type"), help_text=_("CSL JSON date-variable slot (e.g. issued, accessed, event-date)."), ) begin = PartialDateField( null=True, blank=True, verbose_name=_("begin date"), help_text=_( "CSL JSON: date-parts[0] — start or single date with partial-date precision." ), ) end = PartialDateField( null=True, blank=True, verbose_name=_("end date"), help_text=_( "CSL JSON: date-parts[1] — end date for ranges. Must not be set without begin." ), ) season = models.CharField( max_length=20, blank=True, verbose_name=_("season"), help_text=_( "CSL JSON: season ('1'=Spring, '2'=Summer, '3'=Autumn, '4'=Winter or custom)" ), ) circa = models.BooleanField( default=False, verbose_name=_("circa"), help_text=_("CSL JSON: circa — approximate date flag."), ) literal = models.CharField( max_length=255, blank=True, verbose_name=_("literal"), help_text=_( "CSL JSON: literal — free-text date when structured representation is impossible." ), ) raw = models.CharField( max_length=255, blank=True, verbose_name=_("raw"), help_text=_("CSL JSON: raw — unparsed date string from source."), ) raw_date_parts = models.JSONField( null=True, blank=True, verbose_name=_("raw date parts"), help_text=_( "CSL JSON: date-parts — stores original array when normalization to PartialDate fails." ), ) class Meta: verbose_name = _("item date") verbose_name_plural = _("item dates") constraints = [ models.UniqueConstraint( fields=["item", "date_type"], name="unique_date_type_per_item", ) ] indexes = [ models.Index( fields=["item", "date_type"], name="itemdate_item_date_type_idx" ), models.Index(fields=["begin"], name="itemdate_begin_idx"), models.Index(fields=["end"], name="itemdate_end_idx"), ] def __str__(self) -> str: """Return '<date_type>: <begin>' or just the date_type.""" if self.begin: return f"{self.date_type}: {self.begin}" if self.literal: return f"{self.date_type}: {self.literal}" return self.date_type
[docs] def clean(self) -> None: """Require a ``begin`` for any ``end``, and an ``end`` no earlier than it (FS-012).""" # begin and end may still be a raw string here (a blank form field, or a # caller that skipped full_clean()), so normalize before comparing. to_partial_date = PartialDateField().to_python begin = to_partial_date(self.begin) end = to_partial_date(self.end) if end is not None: if begin is None: raise ValidationError( _("A date range's end must not be set without a begin.") ) if begin.start > end.start: raise ValidationError( _("A date range's end must not fall before its begin.") ) super().clean()
[docs] def save(self, *args, **kwargs): """Validate the span rule before writing, so a direct ``save()`` cannot skip it.""" self.clean() return super().save(*args, **kwargs)
[docs] class ItemIdentifier(models.Model): """Typed identifier record for a bibliographic item. Stores DOI, ISBN, ISSN, PMID, PMCID, URL and any other identifier type as a (type, value) pair linked to an Item. The type field intentionally does NOT use choices= validation — this allows unknown identifier type strings to be stored without rejection. The IdentifierType enum provides known values for lookup and documentation only. The (item, type) uniqueness constraint means each item stores at most one identifier per type, so two ISBNs (ISBN-10 and ISBN-13) are not supported. Maps to the DOI, ISBN, ISSN, PMID, PMCID and URL top-level CSL JSON fields; unknown types are placed in the custom object on export. """ item = models.ForeignKey( Item, on_delete=models.CASCADE, related_name="item_identifiers", verbose_name=_("item"), help_text=_("The bibliographic item this identifier belongs to."), ) type = models.CharField( max_length=50, verbose_name=_("identifier type"), help_text=_( "Identifier type string (e.g. DOI, ISBN, ISSN, PMID, PMCID, URL, arXiv)." ), ) value = models.CharField( max_length=500, verbose_name=_("value"), help_text=_("The identifier value string."), ) class Meta: verbose_name = _("item identifier") verbose_name_plural = _("item identifiers") constraints = [ models.UniqueConstraint( fields=["item", "type"], name="unique_identifier_type_per_item", ) ] indexes = [ models.Index(fields=["item", "type"], name="itemidentifier_item_type_idx"), models.Index( fields=["type", "value"], name="itemidentifier_type_value_idx" ), models.Index(fields=["value"], name="itemidentifier_value_idx"), ] def __str__(self) -> str: """Return '<type>: <value>'.""" return f"{self.type}: {self.value}"
[docs] def clean(self) -> None: """Validate the identifier's value format for known identifier types.""" validate_identifier(self.type, self.value) super().clean()
[docs] def save(self, *args, **kwargs): """Validate the identifier format before writing, so a direct ``save()`` cannot skip it.""" validate_identifier(self.type, self.value) return super().save(*args, **kwargs)