Files
website-v2/core/tasks.py
Gavin Wahl cb25772a13 AsciiDoc use the embedded option (#1344)
Instead of parsing out the HTML body from the full HTML document.

All adoc parsing now goes through `convert_adoc_to_html`. I removed the
`adoc_to_html` task because it was never `.delay`ed, probably because it
doesn't necessarily work: it expects content to be passed in a temp
file, which might not exist on the worker processing the task. The new
function passes the content as a string instead of a temp file.

This removes the `div#header` and `div#content` containers from the
output. It's wrong to have IDs in embeddable html because multiple
asciidoc outputs might be included on the same page, resulting in
duplicate IDs. I couldn't find a place where these containers are
necessary, though, because markdown output doesn't include them,
resulting in a discrepancy in the output for markdown vs asciidoc that
is now fixed. If we need to include the containers, I'd wrap the
asciidoc output in the template where it is used rather than expecting
it to be returned by `convert_adoc_to_html`.

Fixes #1302
2024-10-11 10:34:04 -06:00

88 lines
2.8 KiB
Python

import structlog
from celery import shared_task
from dateutil.parser import parse
from django.core.cache import caches
from core.asciidoc import convert_adoc_to_html
from .boostrenderer import get_content_from_s3
from .models import RenderedContent
logger = structlog.get_logger()
@shared_task
def clear_rendered_content_cache_by_cache_key(cache_key):
"""Deletes a RenderedContent object by its cache key from redis and
database."""
cache = caches["static_content"]
cache.delete(cache_key)
RenderedContent.objects.delete_by_cache_key(cache_key)
@shared_task
def clear_rendered_content_cache_by_content_type(content_type):
"""Deletes all RenderedContent objects for a given content type from redis
and database."""
RenderedContent.objects.clear_cache_by_content_type(content_type)
RenderedContent.objects.delete_by_content_type(content_type)
@shared_task
def clear_static_content_cache():
"""Runs the manager method to clear the static content cache"""
RenderedContent.objects.clear_cache_by_cache_type_and_date(
cache_type="static_content_"
)
@shared_task
def refresh_content_from_s3(s3_key, cache_key):
"""Calls S3 with the s3_key, then saves the result to the
RenderedContent object with the given cache_key."""
content_dict = get_content_from_s3(key=s3_key)
content = content_dict.get("content")
if content_dict and content:
content_type = content_dict.get("content_type")
if content_type == "text/asciidoc":
content = convert_adoc_to_html(content)
last_updated_at_raw = content_dict.get("last_updated_at")
last_updated_at = parse(last_updated_at_raw) if last_updated_at_raw else None
# Clear the cache because we're going to update it.
clear_rendered_content_cache_by_cache_key(cache_key)
# Update the rendered content.
save_rendered_content(
cache_key, content_type, content, last_updated_at=last_updated_at
)
# Cache the refreshed rendered content
cache = caches["static_content"]
cache.set(cache_key, {"content": content, "content_type": content_type})
@shared_task
def save_rendered_content(cache_key, content_type, content_html, last_updated_at=None):
"""Saves a RenderedContent object to database."""
defaults = {
"content_type": content_type,
"content_html": content_html,
}
if last_updated_at:
defaults["last_updated_at"] = last_updated_at
obj, created = RenderedContent.objects.update_or_create(
cache_key=cache_key[:255], defaults=defaults
)
logger.info(
"content_saved_to_rendered_content",
cache_key=cache_key,
content_type=content_type,
status_code=200,
obj_id=obj.id,
obj_created=created,
)
return obj