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

App: improve title handling #479

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion legal_tools/management/commands/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import socket
from argparse import SUPPRESS, ArgumentParser
from copy import copy
from multiprocessing import Pool
from pathlib import Path
from pprint import pprint
Expand All @@ -28,7 +29,9 @@
save_bytes_to_file,
save_redirect,
save_url_as_static_file,
update_title,
)
from legal_tools.views import render_redirect

ALL_TRANSLATION_BRANCHES = "###all###"
LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -114,7 +117,14 @@ def save_legal_code(output_dir, legal_code, opt_apache_only):
for symlink in symlinks:
wrap_relative_symlink(output_dir, relpath, symlink)
for redirect_data in redirects_data:
save_redirect(output_dir, redirect_data)
redirect_content = render_redirect(
title=redirect_data["title"],
destination=redirect_data["destination"],
language_code=redirect_data["language_code"],
)
save_redirect(
output_dir, redirect_data["redirect_file"], redirect_content
)
return legal_code.get_redirect_pairs()


Expand Down Expand Up @@ -231,6 +241,18 @@ def add_arguments(self, parser: ArgumentParser):
dest="apache_only",
)

def check_titles(self):
LOG.info("Checking legal code titles")
log_level = copy(LOG.level)
LOG.setLevel(LOG_LEVELS[0])
results = update_title(options={"dryrun": True})
LOG.setLevel(log_level)
if results["records_requiring_update"] > 0:
raise CommandError(
"Legal code titles require an update. See the `update_title`"
" command."
)

def purge_output_dir(self):
if self.options["apache_only"] or self.options["rdf_only"]:
return
Expand Down Expand Up @@ -639,6 +661,7 @@ def distill_metadata_yaml(self):
)

def distill_and_copy(self):
self.check_titles()
self.purge_output_dir()
self.call_collectstatic()
self.write_robots_txt()
Expand Down
44 changes: 44 additions & 0 deletions legal_tools/management/commands/update_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Standard library
import logging
from argparse import ArgumentParser

# Third-party
from django.core.management import BaseCommand

# First-party/Local
from legal_tools.utils import init_utils_logger, update_title

LOG = logging.getLogger(__name__)
LOG_LEVELS = {
0: logging.ERROR,
1: logging.WARNING,
2: logging.INFO,
3: logging.DEBUG,
}


class Command(BaseCommand):
"""
Update the title property of all legal tools by normalizing legalcy titles
and normalizing translated titles for current legal tools (Licenses 4.0 and
CC0 1.0).
"""

def add_arguments(self, parser: ArgumentParser):
# Python defaults to lowercase starting character for the first
# character of help text, but Djano appears to use uppercase and so
# shall we
parser.description = self.__doc__
parser._optionals.title = "Django optional arguments"
parser.add_argument(
"-n",
"--dryrun",
action="store_true",
help="dry run: do not make any changes",
)

def handle(self, **options):
self.options = options
LOG.setLevel(LOG_LEVELS[int(options["verbosity"])])
init_utils_logger(LOG)
update_title(options)
103 changes: 82 additions & 21 deletions legal_tools/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,12 @@ def test_relative_symlink(self):

def test_save_redirect(self):
output_dir = "/OUTPUT_DIR"
redirect_data = {
"destination": "DESTINATION",
"language_code": "LANGUAGE_CODE",
"redirect_file": ("FILE_PATH"),
"title": "TITLE",
}

with mock.patch(
"legal_tools.utils.render_redirect",
return_value="STRING",
) as mock_render:
with mock.patch(
"legal_tools.utils.save_bytes_to_file"
) as mock_save:
utils.save_redirect(output_dir, redirect_data)

mock_render.assert_called_with(
title="TITLE",
destination="DESTINATION",
language_code="LANGUAGE_CODE",
)
redirect_file = "FILE_PATH"
redirect_content = "STRING"

with mock.patch("legal_tools.utils.save_bytes_to_file") as mock_save:
utils.save_redirect(output_dir, redirect_file, redirect_content)

mock_save.assert_called_with("STRING", "/OUTPUT_DIR/FILE_PATH")


Expand Down Expand Up @@ -567,3 +552,79 @@ def validate_udpate_source():
# Subsequent run to test with wrong data and verify behavior of
# repeated runs
validate_udpate_source()


class TitleTest(TestCase):
def setup(self):
for version in ("1.0", "4.0"):
ToolFactory(category="licenses", unit="by", version=version)
for tool in Tool.objects.all():
LegalCodeFactory(tool=tool, language_code="fr")
title_en = utils.get_tool_title_en(
tool.unit,
tool.version,
tool.category,
tool.jurisdiction_code,
)
LegalCodeFactory(tool=tool, title=title_en, language_code="en")
LegalCodeFactory(tool=tool, title=title_en, language_code="nl")
if tool.version == "1.0":
LegalCodeFactory(
tool=tool,
title="Namensnennung 1.0 Generic",
language_code="de",
)
elif tool.version == "4.0":
LegalCodeFactory(
tool=tool,
title="Namensnennung 4.0 International",
language_code="de",
)

def test_get_tool_title(self):
self.setup()
unit = "by"
category = "licenses"
jurisdiction = ""
titles = {}

with self.assertNumQueries(6):
for version in ("1.0", "4.0"):
for language_code in ("de", "en", "fr", "nl"):
title = utils.get_tool_title(
unit=unit,
version=version,
category=category,
jurisdiction=jurisdiction,
language_code=language_code,
)
titles[f"{version}{language_code}"] = title

self.assertEqual("Namensnennung 1.0 Generic", titles["1.0de"])
self.assertEqual("Attribution 1.0 Generic", titles["1.0en"])
self.assertEqual("Attribution 1.0 Générique", titles["1.0fr"])
self.assertEqual("Naamsvermelding 1.0 Unported", titles["1.0nl"])
self.assertEqual("Namensnennung 4.0 International", titles["4.0de"])
self.assertEqual("Attribution 4.0 International", titles["4.0en"])
self.assertEqual("Attribution 4.0 International", titles["4.0fr"])
self.assertEqual("Naamsvermelding 4.0 Internationaal", titles["4.0nl"])

def test_update_titles_dryrun(self):
self.setup()

with self.assertNumQueries(9):
results = utils.update_title({"dryrun": True})

self.assertEqual(
{"records_updated": 0, "records_requiring_update": 4}, results
)

def test_update_titles_with_updates(self):
self.setup()

with self.assertNumQueries(13):
results = utils.update_title({"dryrun": False})

self.assertEqual(
{"records_updated": 4, "records_requiring_update": 0}, results
)
Loading