Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check-metadata command #36

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ For each command, the option `-h` give u some help.
`./manage.py make-widgets` to make a file that groups all jinja2 widgets macros for easier includes. It is automatically called by `runserver` and `distill-local` commands. \
See an example in `EXAMPLE.md`

`./manage.py check-metadata` to check if metadata specified in `JFME_CONTENT_REQUIRED_METADATA` are set in pages.

## Others

JFM-Engine is a friendly fork of [JSSG](https://github.com/jtremesay/jssg/) made in agreement with the JSSG developer because of different goals. \
Expand Down
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
2 changes: 1 addition & 1 deletion content/templates/jinja2/allwidgets.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="navbar__container__right">
<a class="btn btn-sm btn-primary" href="{{ CTA_URL }}"
target="{{ CTA_TARGET }}">{{ CTA_LABEL }}</a>
<a href="{{ url('page', args=[CHANGE_LANG_URL]) }}">
<a href="{{ url_for_slug_path(CHANGE_LANG_URL) }}">
<img class="languageFlag" src="{{ static(CHANGE_LANG_FLAG_URL)}}" alt="{{ CHANGE_LANG_ALT }}"/>
</a>
</div>
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 = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better extensibilité, the alogrithm should be exported into a reusable function.

Example:

for page in Page.load_glob(path = list(map(lambda p : Path(p).absolute(), options["content path"])), all = True) :
    metadata_status = get_metadata_status_for(page)
    self.stdout.write("{:3.0f}% : {}".format(
        metadata_status.complete_percent,
        page.path.relative_to(page.content_page_dir)
    )
    if not metadata_status.is_complete():
        print [...]

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
Loading