From c9cc3c49b32cedabeaa54237b53674ab29bf1332 Mon Sep 17 00:00:00 2001 From: Lacey Williams Henschel Date: Mon, 5 Feb 2024 14:14:18 -0800 Subject: [PATCH] Add exception logic to load the Detail docs links - Add a button to the LibraryVersion admin to reload the docs links - Add some minimal docs on the admin features --- docs/admin.md | 5 ++++ libraries/admin.py | 24 ++++++++++++++++- libraries/tasks.py | 26 +++++++++++++++++++ libraries/tests/test_utils.py | 6 +++++ libraries/utils.py | 5 ++++ .../admin/libraryversion_change_list.html | 11 ++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 docs/admin.md create mode 100644 templates/admin/libraryversion_change_list.html diff --git a/docs/admin.md b/docs/admin.md new file mode 100644 index 00000000..af546e4d --- /dev/null +++ b/docs/admin.md @@ -0,0 +1,5 @@ +# Admin Features + +## LibraryVersions + +- Button **Refresh Documentation Links** will re-run the task that retrieves the links to external documentation for every version of every Boost library. diff --git a/libraries/admin.py b/libraries/admin.py index f7481ecf..f6f3cfdd 100644 --- a/libraries/admin.py +++ b/libraries/admin.py @@ -3,7 +3,10 @@ from django.http import HttpResponseRedirect from django.urls import path from .models import Category, Issue, Library, LibraryVersion, PullRequest -from .tasks import update_libraries +from .tasks import ( + update_libraries, + update_library_version_documentation_urls_all_versions, +) @admin.register(Category) @@ -46,6 +49,25 @@ class LibraryVersionAdmin(admin.ModelAdmin): list_filter = ["library", "version"] ordering = ["library__name", "-version__name"] search_fields = ["library__name", "version__name"] + change_list_template = "admin/libraryversion_change_list.html" + + def get_urls(self): + urls = super().get_urls() + my_urls = [ + path("update_docs_urls/", self.update_docs_urls, name="update_docs_urls"), + ] + return my_urls + urls + + def update_docs_urls(self, request): + """Run the task to refresh the documentation URLS from S3""" + update_library_version_documentation_urls_all_versions.delay() + self.message_user( + request, + """ + Documentation links are being refreshed. + """, + ) + return HttpResponseRedirect("../") @admin.register(Issue) diff --git a/libraries/tasks.py b/libraries/tasks.py index 49f25b38..8b9eec3f 100644 --- a/libraries/tasks.py +++ b/libraries/tasks.py @@ -1,16 +1,28 @@ import structlog from config.celery import app +from django.db.models import Q from core.boostrenderer import get_content_from_s3 from core.htmlhelper import get_library_documentation_urls from libraries.github import LibraryUpdater from libraries.models import LibraryVersion from libraries.utils import get_first_last_day_last_month from versions.models import Version +from .utils import generate_library_docs_url logger = structlog.getLogger(__name__) +LIBRARY_DOCS_EXCEPTIONS = {"detail": generate_library_docs_url} + + +@app.task +def update_library_version_documentation_urls_all_versions(): + """Run the task to update all documentation URLs for all versions""" + for version in Version.objects.all(): + get_and_store_library_version_documentation_urls_for_version.delay(version.pk) + + @app.task def get_and_store_library_version_documentation_urls_for_version(version_pk): """ @@ -64,6 +76,20 @@ def get_and_store_library_version_documentation_urls_for_version(version_pk): ) continue + # See if we can load missing docs URLS another way + library_versions = LibraryVersion.objects.filter(version=version).filter( + Q(documentation_url="") | Q(documentation_url__isnull=True) + ) + for library_version in library_versions: + exception_url_generator = LIBRARY_DOCS_EXCEPTIONS.get( + library_version.library.name.lower() + ) + if exception_url_generator: + library_version.documentation_url = exception_url_generator( + version.boost_url_slug, library_version.library.slug.lower() + ) + library_version.save() + @app.task def update_libraries(update_all=False): diff --git a/libraries/tests/test_utils.py b/libraries/tests/test_utils.py index a374c130..581a98fa 100644 --- a/libraries/tests/test_utils.py +++ b/libraries/tests/test_utils.py @@ -5,6 +5,7 @@ import os from libraries.utils import ( decode_content, generate_fake_email, + generate_library_docs_url, get_first_last_day_last_month, parse_date, write_content_to_tempfile, @@ -28,6 +29,11 @@ def test_generate_fake_email(): assert "@example.com" in result +def test_generate_library_docs_url(): + expected = "/doc/libs/boost_1_84_0/libs/detail/doc/html/index.html" + assert generate_library_docs_url("boost_1_84_0", "detail") == expected + + def test_get_first_last_day_last_month(): first_day, last_day = get_first_last_day_last_month() diff --git a/libraries/utils.py b/libraries/utils.py index 7d47910e..74d0b7b5 100644 --- a/libraries/utils.py +++ b/libraries/utils.py @@ -28,6 +28,11 @@ def generate_fake_email(val: str) -> str: return f"{local_email}@example.com" +def generate_library_docs_url(boost_url_slug, library_slug): + """Generate a documentation url with a specific format""" + return f"/doc/libs/{boost_url_slug}/libs/{library_slug}/doc/html/index.html" + + def get_first_last_day_last_month(): now = datetime.now() first_day_this_month = now.replace(day=1) diff --git a/templates/admin/libraryversion_change_list.html b/templates/admin/libraryversion_change_list.html new file mode 100644 index 00000000..e5379ec3 --- /dev/null +++ b/templates/admin/libraryversion_change_list.html @@ -0,0 +1,11 @@ +{% extends "admin/change_list.html" %} +{% load i18n admin_urls %} + +{% block object-tools %} + +{% endblock %}