Files
website-v2/core/models.py
2026-02-20 12:41:54 -08:00

131 lines
4.4 KiB
Python

import re
from django.db import models
from django.utils.translation import gettext_lazy as _
from django_extensions.db.models import TimeStampedModel
from libraries.path_matcher.utils import determine_latest_url
from versions.models import Version
from .managers import RenderedContentManager
class LatestPathMatchIndicator(models.IntegerChoices):
UNDETERMINED = 0, _("Undetermined")
DIRECT_MATCH = 1, _("Direct match exists")
CUSTOM_MATCH = 2, _("Determined by matcher")
class RenderedContent(TimeStampedModel):
"""Stores a copy of rendered content. Generally, this content is retrieved
from the S3 buckets and, if necessary, converted to HTML.
This model is intended to be used as a cache. If the content is not found,
it will be retrieved from S3 and stored in this model. If the content is
found, it will be returned from this model.
TimeStampedModel adds `created` and `modified` fields:
https://django-extensions.readthedocs.io/en/latest/model_extensions.html
"""
cache_key = models.CharField(
max_length=255,
unique=True,
help_text=_("The cache key for the content."),
db_index=True,
)
content_type = models.CharField(
max_length=255,
help_text=_("The content type/MIME type."),
null=True,
blank=True,
)
content_original = models.TextField(
help_text=_("The original content."), null=True, blank=True
)
content_html = models.TextField(
help_text=_("The rendered HTML content."), null=True, blank=True
)
last_updated_at = models.DateTimeField(
help_text=_("The last time the content was updated in S3."),
null=True,
blank=True,
)
latest_path_matched_indicator = models.IntegerField(
choices=LatestPathMatchIndicator,
default=LatestPathMatchIndicator.UNDETERMINED,
null=False,
blank=False,
help_text=_("Indicates how the latest path should be determined."),
)
latest_docs_path = models.CharField(blank=True, default="")
latest_path_match_class = models.CharField(max_length=128, blank=True, default="")
objects = RenderedContentManager()
class Meta:
verbose_name = _("rendered content")
verbose_name_plural = _("rendered contents")
def __str__(self):
return self.cache_key
@property
def latest_path(self) -> str | None:
indicator = self.latest_path_matched_indicator
if indicator == LatestPathMatchIndicator.DIRECT_MATCH:
return re.sub(
r"static_content_[\d_]+/(?P<content_path>[^/]\S+)",
"doc/libs/latest/\g<content_path>",
self.cache_key,
)
elif indicator == LatestPathMatchIndicator.CUSTOM_MATCH:
return self.latest_docs_path
elif indicator == LatestPathMatchIndicator.UNDETERMINED:
return determine_latest_url(
self.cache_key.replace("static_content_", ""),
Version.objects.most_recent(),
)
def save(self, *args, **kwargs):
if isinstance(self.content_original, bytes):
self.content_original = self.content_original.decode("utf-8")
if isinstance(self.content_html, bytes):
self.content_html = self.content_html.decode("utf-8")
if isinstance(self.content_type, bytes):
self.content_type = self.content_type.decode("utf-8")
super().save(*args, **kwargs)
class SiteSettings(models.Model):
wordcloud_ignore = models.TextField(
default="",
help_text="A comma-separated list of words to ignore in the release report wordcloud.", # noqa E501
)
rendered_content_replacement_start = models.DateTimeField(
null=True,
blank=True,
editable=False,
help_text="Set via RenderedContent admin action.",
)
class Meta:
constraints = [
# check constraint to only allow id=1 to exist
models.CheckConstraint(
name="%(app_label)s_%(class)s_single_instance",
condition=models.Q(id=1),
),
]
verbose_name_plural = "Site Settings"
@classmethod
def load(cls):
obj, _ = cls.objects.get_or_create(pk=1)
return obj
@property
def wordcloud_ignore_set(self):
return set(x.strip().lower() for x in self.wordcloud_ignore.split(","))