Skip to content

Commit

Permalink
add doctests, move json load to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
teovin committed May 22, 2024
1 parent 2797a90 commit 19904af
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
8 changes: 8 additions & 0 deletions web/config/settings/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
from typing import Any, TypedDict
import json

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
ALLOWED_HOSTS: list[str] = []
Expand Down Expand Up @@ -299,3 +300,10 @@ class LoggerConfig(TypedDict, total=False):
SENTRY_SEND_DEFAULT_PII = False

COVER_IMAGES = False

# Load ali materials
try:
with open(f"{STATIC_ROOT}/data/ali_materials.json", "r") as file:
ALI_MATERIALS = json.load(file)
except FileNotFoundError as e:
print("Error opening file.", e)
5 changes: 5 additions & 0 deletions web/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ def legal_doc(legal_document_factory):
return legal_document_factory()


@pytest.fixture
def content_node(content_node_factory):
return content_node_factory()


@pytest.fixture
def capapi_mock(requests_mock):
"""
Expand Down
26 changes: 21 additions & 5 deletions web/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
import time
import json
from datetime import datetime
from enum import Enum
from os.path import commonprefix
Expand Down Expand Up @@ -1658,19 +1657,36 @@ def export_content(self, request):
return self.resource.content

def get_ali_license_text(self):
with open(f"{settings.STATIC_ROOT}/data/ali_materials.json", "r") as file:
licensed_materials = json.load(file)
"""Returns custom license text if title contains all items of the match_words list
>>> node = getfixture('content_node')
>>> # title including both items in the form of single word strings
>>> node.title = 'Restatement (Third) of Property (Servitudes) Notes and Questions'
>>> assert node.get_ali_license_text() == 'Restatement of the Law, Property, copyright @ 1977-2023 by the American Law Institute. Reproduced with permission, not as part of a Creative Commons license.'
>>> # title including both items in the form of one single word string and one multi-word string
>>> node.title = 'Excerpts from Restatement (First) and (Second) of Conflict of Laws'
>>> assert node.get_ali_license_text() == 'Restatement of the Law, Conflict of Laws, copyright @ 1971-2023 by the American Law Institute. Reproduced with permission, not as part of a Creative Commons license.'
>>> # title including both items in the form of two multi-word strings in lower case
>>> node.title = 'section from model penal code sexual assault and related offenses publication'
>>> assert node.get_ali_license_text() == 'Model Penal Code, Sexual Assault and Related Offenses, copyright @ 2021-2022 by the American Law Institute. Reproduced with permission, not as part of a Creative Commons license.'
>>> # title that only includes one of the words
>>> node.title = 'section from the principles of law publications'
>>> assert node.get_ali_license_text() == ''
>>> # title that doesn't include any of the words
>>> node.title = 'Text that does not include any of the match words'
>>> assert node.get_ali_license_text() == ''
"""

title = self.title.lower()
license_txt = ""

for item in licensed_materials:
for item in settings.ALI_MATERIALS:
if all(word in title for word in item["match_words"]):
license_txt = (
f"{item['title']}, copyright @ {item['years']} by the American Law Institute. "
f"Reproduced with permission, not as part of a Creative Commons license."
)

break
return license_txt

@property
Expand Down
2 changes: 1 addition & 1 deletion web/main/templates/includes/casebook_copyright_notice.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<span class="ali-license">
{% if section.ali_licensed %}
{{ ali_license }}
{{ section.get_ali_license_text }}
{% else %}
Any excerpts from the <i>Restatements of the Law</i>, <i>Principles of the Law</i>, and the <i>Model Penal Code</i> are copyright by The American Law Institute. Excerpts are reproduced with permission, not as part of a Creative Commons license.
{% endif %}
Expand Down
2 changes: 0 additions & 2 deletions web/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,6 @@ def get(self, request, casebook, section):
{
"casebook": casebook,
"section": section,
"ali_license": section.get_ali_license_text(),
"tabs": section.tabs_for_user(request.user),
"casebook_color_class": casebook.casebook_color_indicator,
"edit_mode": casebook.directly_editable_by(request.user),
Expand Down Expand Up @@ -2379,7 +2378,6 @@ def get(self, request, casebook: Casebook, resource: Resource):
{
"casebook": casebook,
"section": section,
"ali_license": section.get_ali_license_text(),
"body_json": body_json,
"contents": section,
"include_vuejs": section.annotatable,
Expand Down
14 changes: 7 additions & 7 deletions web/static/data/ali_materials.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,55 +139,55 @@
"title": "Principle of the Law, Aggregate Litigation",
"years": "2010-2023",
"match_words": [
"restatement",
"principle",
"aggregate litigation"
]
},
{
"title": "Principle of the Law, Corporate Governance",
"years": "1994-2023",
"match_words": [
"restatement",
"principle",
"corporate governance"
]
},
{
"title": "Principle of the Law, Data Privacy",
"years": "2020",
"match_words": [
"restatement",
"principle",
"data privacy"
]
},
{
"title": "Principle of the Law, Election Administration: Non-Precinct Voting and Resolution of Ballot-Counting Disputes",
"years": "2019-2023",
"match_words": [
"restatement",
"principle",
"election administration: non-precinct voting and resolution of ballot-counting disputes"
]
},
{
"title": "Principle of the Law, Family Dissolution",
"years": "2002-2023",
"match_words": [
"restatement",
"principle",
"family dissolution"
]
},
{
"title": "Principle of the Law, Intellectual Property: Principles Governing Jurisdiction, Choice of Law, and Judgments in Transnational Disputes",
"years": "2008",
"match_words": [
"restatement",
"principle",
"intellectual property: principles governing jurisdiction, choice of law, and judgments in transnational disputes"
]
},
{
"title": "Principle of the Law, Software Contracts",
"years": "2010",
"match_words": [
"restatement",
"principle",
"software contracts"
]
},
Expand Down
7 changes: 4 additions & 3 deletions web/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ def populate_ali_licensed(ctx):
"""
from main.models import ContentNode

for node in ContentNode.objects.all():
if node.get_ali_license_text():
node.ali_licensed = True
for node in tqdm(ContentNode.objects.iterator()):
is_licensed = bool(node.get_ali_license_text())
if node.ali_licensed is not is_licensed:
node.ali_licensed = is_licensed
node.save(update_fields=["ali_licensed"])

0 comments on commit 19904af

Please sign in to comment.