From ff89fd9b537d6de33e19cfff3831186f1336e495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Mon, 15 Jul 2024 12:12:58 +0200 Subject: [PATCH] add a check-metadata command --- content/pages/a_page.md | 1 + jssg/management/commands/check-metadata.py | 41 ++++++++++++++++++++++ jssg/settings.py | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 jssg/management/commands/check-metadata.py diff --git a/content/pages/a_page.md b/content/pages/a_page.md index f37368f..098d382 100644 --- a/content/pages/a_page.md +++ b/content/pages/a_page.md @@ -2,6 +2,7 @@ title Page1 slug page1 template_engine jinja2 +description --- { diff --git a/jssg/management/commands/check-metadata.py b/jssg/management/commands/check-metadata.py new file mode 100644 index 0000000..a490545 --- /dev/null +++ b/jssg/management/commands/check-metadata.py @@ -0,0 +1,41 @@ +from django.core.management.base import BaseCommand +from django.conf import settings +from jssg.models import Page +from pathlib import Path + +class Command(BaseCommand): + help = "Check if metadata in JFME_CONTENT_REQUIRED_METADATA setting are specified in pages." + + def add_arguments(self, parser): + parser.add_argument( + "--verbose", + action = "store_true", + help="Show missing or empty metadata in each page." + ) + parser.add_argument( + "content path", + nargs = "*", + type=str, + default=settings.JFME_PAGES_DIRS, + help="The paths where search the pages. Set to JFME_PAGES_DIRS by default." + ) + + def handle(self, *args, **options) : + for page in Page.load_glob(path = list(map(lambda p : Path(p).absolute(), options["content path"])), all = True) : + missing_metadata = [] + empty_metadata = [] + for required_metadata in settings.JFME_CONTENT_REQUIRED_METADATA : + if required_metadata not in page.metadata : + missing_metadata.append(required_metadata) + elif page.metadata[required_metadata] == "" : + empty_metadata.append(required_metadata) + + self.stdout.write("{:3.0f}% : {}".format( + (len(settings.JFME_CONTENT_REQUIRED_METADATA) - len(missing_metadata) - len(empty_metadata)) * 100 / len(settings.JFME_CONTENT_REQUIRED_METADATA), + page.path.relative_to(page.content_page_dir)) + ) + if options["verbosity"]>1 or options["verbose"] : + for missing in missing_metadata : + self.stdout.write("\t- '{}' is missing".format(missing)) + for empty in empty_metadata : + self.stdout.write("\t- '{}' is empty".format(empty)) \ No newline at end of file diff --git a/jssg/settings.py b/jssg/settings.py index a0ce03b..c57e743 100644 --- a/jssg/settings.py +++ b/jssg/settings.py @@ -52,7 +52,7 @@ JFME_POSTS_DIRS = [path / "posts" for path in JFME_CONTENT_DIRS] JFME_TEMPLATES_DIRS = [path / "templates" for path in JFME_CONTENT_DIRS] JFME_STATIC_DIRS = [path / "static" for path in JFME_CONTENT_DIRS] - +JFME_CONTENT_REQUIRED_METADATA = ["title", "slug", "lang", "description"] # Application definition