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
Changes from 1 commit
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
38 changes: 26 additions & 12 deletions jssg/management/commands/check-metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
from jssg.models import Page
from pathlib import Path

class MetadataStatus :

def get_metadata_status_for(self, page) :
self.missing = []
self.empty = []
for required_metadata in settings.JFME_CONTENT_REQUIRED_METADATA :
if required_metadata not in page.metadata :
self.missing.append(required_metadata)
elif page.metadata[required_metadata] == "" :
self.empty.append(required_metadata)
self.complete = self.missing == [] and self.empty == []
Copy link
Member

Choose a reason for hiding this comment

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

I suggest addind parenthesis for readability self.complete = (self.missing == []) and (self.empty == [])

For readability comparison:

self.complete = self.missing == [] and self.empty == []
self.complete = (self.missing == [] and self.empty == [])
self.complete = (self.missing == []) and (self.empty == [])

Note that in the second line, black will remove parenthesis

if len(settings.JFME_CONTENT_REQUIRED_METADATA) > 0 :
self.progression = (len(settings.JFME_CONTENT_REQUIRED_METADATA) - len(self.missing) - len(self.empty)) * 100 / len(settings.JFME_CONTENT_REQUIRED_METADATA)
else :
self.progression = 100

class Command(BaseCommand):
help = "Check if metadata in JFME_CONTENT_REQUIRED_METADATA setting are specified in pages."

Expand All @@ -22,20 +38,18 @@ def add_arguments(self, parser):

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)

metadata_status = MetadataStatus()
metadata_status.get_metadata_status_for(page)
Copy link
Member

Choose a reason for hiding this comment

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

According to the way you implement it, I suggest to rename get_metadata_status_for() to process()

So you use it like:

            metadata_status = MetadataStatus()
            metadata_status.process(page)

Another way would be to implement get_metadata_status_for() as a class method returning a MetadataStatus instance:

            metadata_status = MetadataStatus.get_metadata_status_for(page)

Note: this is not required for merge but it would make the code more readable for future maintainance


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),
metadata_status.progression,
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))
if not metadata_status.complete :
for missing in metadata_status.missing :
self.stdout.write("\t- '%s' is missing" % missing)
for empty in metadata_status.empty :
self.stdout.write("\t- '%s' is empty" % empty)
Loading