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: add tool meta information (canonical url, rdf/xml, plaintext) #392

Merged
merged 12 commits into from
Nov 22, 2023
Merged
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ repository.
## Not the live site

This project is not intended to serve the legal tools directly. Instead, a
command line tool can be used to save all the rendered HTML pages as files.
Then those files are used as part of the real creativecommons.org site, just
served as static files.
command line tool can be used to save all the rendered HTML and RDF/XML pages
as files. Then those files are used as part of the real CreativeCommons.org
site (served as static files).


## Software Versions
Expand Down
73 changes: 72 additions & 1 deletion cc_legal_tools/static/cc-legal-tools/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ div.masthead > nav.ancilliary-menu span.locale.icon-attach:before {


/* header title */
/* TODO: resolve with vocabulary-theme */
.cc-legal-tools main > header {
padding: 3em 0;
}
.cc-legal-tools main > header > span.tool-icons > span.cc-icon > svg {
display: inline;
height: 4em;
width: 4em;
}
.cc-legal-tools main > header > h1 {
margin: 0.2em 0;
}
.cc-legal-tools main > header > h2 {
margin: 0;
}


/* right nav */
Expand Down Expand Up @@ -99,14 +109,75 @@ main > aside > nav ul > li { /* TODO: resolve with vocabulary-theme */
padding: 0.5em;
margin: 0;
}

.cc-legal-tools table tr > th {
}
.cc-legal-tools table > tbody > tr:nth-child(3n) {
background-color: var(--vocabulary-neutral-color-lighter-gray);
}


/*.tool-meta */
.cc-legal-tools .tool-meta {
display: flex;
flex-wrap: wrap;
gap: 1em;
}
.cc-legal-tools .tool-meta div.meta-box {
list-style: none;
background-color: var(--vocabulary-brand-color-soft-turquoise);
border-radius: 5px;
padding: 0.8em;
padding-top: 1em;
padding-right: 1.4em;
}
.cc-legal-tools .tool-meta div.meta-box a {
--underline-background-color: var(--vocabulary-brand-color-soft-turquoise);
font-family: 'Source Sans Pro';
}
.cc-legal-tools .tool-meta h2 {
font-size: 1em;
margin: 0;
font-family: 'Source Sans Pro';
margin-left: 0.6em;
display: inline;
}
.cc-legal-tools .tool-meta h2:after {
content: ':';
}
.cc-legal-tools article.canonical-url {
margin-bottom: 0.5em;
}
.cc-legal-tools article.canonical-url h2 {
margin-bottom: 0.6em;
}
.cc-legal-tools article.canonical-url a {
font-family: monospace;
}
.cc-legal-tools .tool-meta .formats ul {
list-style: none;
display: inline-flex;
margin: 0;
font-size: 1em;
}
.cc-legal-tools .tool-meta .formats ul li {
padding: 0.3em;
}
.cc-legal-tools .tool-meta .formats ul li:first-child {
padding-left: 0;
}
.cc-legal-tools .alt-view {
align-self: center;
margin-left: auto;
}
.cc-legal-tools .alt-view a {
font-family: 'Source Sans Pro';
font-size: 1.5em;
font-style: normal;
font-weight: 700;
line-height: 150%;
}


/* Notices */
.cc-legal-tools div.notice-bottom, .cc-legal-tools div.notice-top {
padding: 1em;
Expand Down
4 changes: 0 additions & 4 deletions cc_legal_tools/static/cc-legal-tools/deed.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.cc-legal-tools main > header > h1 {
margin: 0.5em 0;
}

.cc-legal-tools div.content div#deed-body {
border-top: 10px solid var(--vocabulary-brand-color-turquoise);
border-bottom: 5px solid var(--vocabulary-neutral-color-dark-gray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Theme Name: CC Vocabulary Theme
Author: the Creative Commons team; possumbilities, Timid Robot
Author URI: https://opensource.creativecommons.org/
Description: Theme based on the Vocabulary Design System
Version: 1.3
Version: 1.3.1
Requires at least: 5.0
Tested up to: 6.2.2
Requires PHP: 7.0
Expand Down
19 changes: 19 additions & 0 deletions legal_tools/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,3 +1399,22 @@ def test_view_legal_tool_rdf_images_mixin(self):
" </rdf:Description>\n",
content,
)


class ViewLegacyPlaintext(ToolsTestsMixin, TestCase):
def test_view_legacy_plaintext_file_exists(self):
tool = Tool.objects.get(unit="by", version="4.0")
url = build_path(base_url=tool.base_url, document="legalcode")
url = f"{url}.txt"
response = self.client.get(url)
content = response.content.decode()
self.assertEqual(f"{response.status_code} {url}", f"200 {url}")
self.assertEqual(response.headers["Content-Type"], "text/plain")
self.assertIn("Attribution 4.0 International", content)

def test_view_legacy_plaintext_file_does_not_exist(self):
tool = Tool.objects.get(unit="by", version="2.0")
url = build_path(base_url=tool.base_url, document="legalcode")
url = f"{url}.txt"
response = self.client.get(url)
self.assertEqual(f"{response.status_code} {url}", f"404 {url}")
23 changes: 15 additions & 8 deletions legal_tools/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
view_deed,
view_dev_index,
view_image_rdf,
view_legacy_plaintext,
view_legal_code,
view_legal_tool_rdf,
view_list,
Expand Down Expand Up @@ -246,6 +247,20 @@ def to_url(self, value):
name="view_deed_unported",
),
# LEGALCODE PAGES #########################################################
# Legalcode: plain text
path(
"<category:category>/<unit:unit>/<version:version>/legalcode.txt",
view_legacy_plaintext,
name="view_legacy_plaintext",
),
# NOTE: programmatic plaintext functionality disabled
# # Plaintext Legalcode: no Jurisdiction (int/unported), no language_code
# path(
# "<category:category>/<unit:unit>/<version:version>/legalcode.txt",
# view_legal_code,
# kwargs=dict(jurisdiction="", is_plain_text=True),
# name="view_legal_code_unported",
# ),
# Legalcode: with Jurisdiction (ported), with language_code
path(
"<category:category>/<unit:unit>/<version:version>"
Expand Down Expand Up @@ -275,14 +290,6 @@ def to_url(self, value):
kwargs=dict(jurisdiction=""),
name="view_legal_code_unported",
),
# NOTE: plaintext functionality disabled
# # Plaintext Legalcode: no Jurisdiction (int/unported), no language_code
# path(
# "<category:category>/<unit:unit>/<version:version>/legalcode.txt",
# view_legal_code,
# kwargs=dict(jurisdiction="", is_plain_text=True),
# name="view_legal_code_unported",
# ),
# CCREL DOCUMENTS #########################################################
# Legal tool RDF/XML: no Jurisdiction (international/unported)
path(
Expand Down
69 changes: 65 additions & 4 deletions legal_tools/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Standard library
import os.path
import os
import re
from operator import itemgetter
from typing import Iterable
Expand Down Expand Up @@ -37,6 +37,21 @@
)

NUM_COMMITS = 3
PLAIN_TEXT_TOOL_IDENTIFIERS = [
"CC BY 3.0",
"CC BY-NC 3.0",
"CC BY-NC-ND 3.0",
"CC BY-NC-SA 3.0",
"CC BY-ND 3.0",
"CC BY-SA 3.0",
"CC BY 4.0",
"CC BY-NC 4.0",
"CC BY-NC-ND 4.0",
"CC BY-NC-SA 4.0",
"CC BY-ND 4.0",
"CC BY-SA 4.0",
"CC0 1.0",
]

# For removing the deed.foo section of a deed url
REMOVE_DEED_URL_RE = re.compile(r"^(.*?/)(?:deed)?(?:\..*)?$")
Expand Down Expand Up @@ -414,11 +429,14 @@ def view_list(request, category, language_code=None):
request_path=request.path,
selected_language_code=language_code,
)
canonical_url_html = os.path.join(
settings.CANONICAL_SITE, request.path.lstrip(os.sep)
)
html_response = render(
request,
template_name=f"list-{category}.html",
context={
"canonical_url": f"{settings.CANONICAL_SITE}{request.path}",
"canonical_url_html": canonical_url_html,
"category": category,
"category_title": category_title,
"category_list": category_list,
Expand Down Expand Up @@ -505,13 +523,18 @@ def view_deed(
else:
body_template = "includes/deed_body_unimplemented.html"

canonical_url_html = os.path.join(
settings.CANONICAL_SITE, request.path.lstrip(os.sep)
)
canonical_url_cc = os.path.join(os.path.dirname(canonical_url_html), "")
html_response = render(
request,
template_name="deed.html",
context={
"additional_classes": "",
"body_template": body_template,
"canonical_url": f"{settings.CANONICAL_SITE}{request.path}",
"canonical_url_cc": canonical_url_cc,
"canonical_url_html": canonical_url_html,
"category": category,
"category_title": category_title,
"identifier": tool.identifier(),
Expand Down Expand Up @@ -541,6 +564,7 @@ def view_legal_code(
language_code=None,
is_plain_text=False,
):
plain_text_url = None
request.path, language_code = normalize_path_and_lang(
request.path, jurisdiction, language_code
)
Expand Down Expand Up @@ -607,10 +631,20 @@ def view_legal_code(
language_default,
)

if tool.identifier() in PLAIN_TEXT_TOOL_IDENTIFIERS:
plain_text_url = "legalcode.txt"

canonical_url_html = os.path.join(
settings.CANONICAL_SITE, request.path.lstrip(os.sep)
)
canonical_url_cc = os.path.join(
os.path.dirname(canonical_url_html), ""
)
kwargs = dict(
template_name="legalcode.html",
context={
"canonical_url": f"{settings.CANONICAL_SITE}{request.path}",
"canonical_url_cc": canonical_url_cc,
"canonical_url_html": canonical_url_html,
"category": category,
"category_title": category_title,
"deed_rel_path": deed_rel_path,
Expand All @@ -619,6 +653,7 @@ def view_legal_code(
"legal_code": legal_code,
"list_licenses": list_licenses,
"list_publicdomain": list_publicdomain,
"plain_text_url": plain_text_url,
"replaced_path": replaced_path,
"replaced_title": replaced_title,
"tool": tool,
Expand Down Expand Up @@ -797,3 +832,29 @@ def view_image_rdf(request):
serialized_rdf_content, content_type="application/rdf+xml"
)
return response


def view_legacy_plaintext(
request,
unit,
version,
category=None,
):
"""
Display plain text file, if it exists (this view is only used in
development).
"""
published_docs_path = os.path.abspath(
os.path.realpath(os.path.join("..", "cc-legal-tools-data", "docs"))
)
plain_text_path = os.path.join(
published_docs_path, request.path.lstrip(os.sep)
)
if os.path.isfile(plain_text_path):
with open(plain_text_path, "rt") as file_obj:
content = file_obj.read()
response = HttpResponse(content, content_type="text/plain")
else:
raise Http404("plain text file does not exist")

return response
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{% block title %}{% endblock %} | Creative Commons</title>
<link href="{{ canonical_url }}" rel="canonical"/>
<link href="{{ canonical_url_html }}" rel="canonical"/>
{% block head_meta %}
{% endblock %}
<link rel="icon" href="/wp-content/themes/vocabulary-theme/vocabulary/favicon/favicon.ico" sizes="any">
Expand Down
19 changes: 16 additions & 3 deletions templates/deed.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@
{% include 'includes/notice_newer_license.html' %}
{% endif %}

<div class="tool-meta">

<div class="meta-box">
<article class="canonical-url">
<h2>{% trans "Canonical URL" %}</h2>
<a href="{{ canonical_url_cc}}">{{ canonical_url_cc }}</a>
</article>
</div>

{% if not tool.deed_only %}
<p class="document-link">
<a href="{{ legal_code_rel_path }}">{% trans "See the legal code" %}</a>
</p>
<div class="alt-view">
<a href="{{ legal_code_rel_path }}">
{% trans "See the legal code" %}
</a>
</div>
{% endif %}

</div>

{% include body_template %}

{% if category == "licenses" and tool.version == "4.0" %}
Expand Down
Loading