Skip to content

Commit

Permalink
add a check-metadata command
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Jul 15, 2024
1 parent 1b67b84 commit ff89fd9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions content/pages/a_page.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title Page1
slug page1
template_engine jinja2
description
---
{

Expand Down
41 changes: 41 additions & 0 deletions jssg/management/commands/check-metadata.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion jssg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff89fd9

Please sign in to comment.