Skip to content

Commit

Permalink
Merge branch 'master' into refactor/remove-asyncComputedV2
Browse files Browse the repository at this point in the history
  • Loading branch information
RayBB committed Oct 17, 2023
2 parents 96b60e3 + fd69891 commit c114ecb
Show file tree
Hide file tree
Showing 33 changed files with 232 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codegen_api_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
codegen_api_docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-python@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cron_watcher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
cron_watcher:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: 3.x
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy_storybook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
deploy_storybook:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: '14'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/javascript_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
javascript_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: '16' # # Should match what's in our Dockerfile
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
python_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-python@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: pip install --user ruff==0.0.286
- run: ruff --format=github .
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-toml
- id: check-yaml
Expand Down Expand Up @@ -66,7 +66,7 @@ repos:
- id: validate-pyproject

- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.50.0
rev: v8.51.0
hooks:
- id: eslint
types: [file]
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This welcome section is intended for new contributors.
* [How we use git](https://github.com/internetarchive/openlibrary/wiki/Git-Cheat-Sheet)
* [Finding good first issues](https://github.com/internetarchive/openlibrary/issues?q=is%3Aopen+is%3Aissue+label%3A%22Needs%3A+Help%22+label%3A%22Good+First+Issue%22)
* [Testing your code](https://github.com/internetarchive/openlibrary/wiki/Testing)
* [Enabling debugging](https://github.com/internetarchive/openlibrary/wiki/Debugging)
* [Enabling debugging & profiling](https://github.com/internetarchive/openlibrary/wiki/Debugging-and-Performance-Profiling)
5. **Learning the Code:**
* [Technical Tour & System Overview (1h)](https://archive.org/details/openlibrary-tour-2020/technical_overview.mp4)
* [Walkthrough videos](https://archive.org/details/openlibrary-tour-2020)
Expand All @@ -25,7 +25,7 @@ This welcome section is intended for new contributors.
7. **Questions?**
* [Wiki](https://github.com/internetarchive/openlibrary/wiki)
* [Request a slack invite](https://openlibrary.org/volunteer)
* [Weekly Community calls](https://docs.google.com/document/d/1LEbzsLZ1F9_YIQOoZzO7GoZnG1z-rudhZ9HNtsameTc/edit)
* [Weekly Community calls](https://github.com/internetarchive/openlibrary/wiki/Community-Call)
* [Open Library FAQs](https://openlibrary.org/help/faq)
## Quick Tour

Expand Down
3 changes: 1 addition & 2 deletions conf/openlibrary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ features:
support: admin
undo: enabled
upstream: enabled
# Uncomment below line to enable the My Books Dropper
# my_books_dropper: enabled
my_books_dropper: enabled

upstream_to_www_migration: true
default_template_root: /upstream
Expand Down
4 changes: 4 additions & 0 deletions openlibrary/catalog/add_book/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ def normalize_import_record(rec: dict) -> None:
if not isinstance(rec['source_records'], list):
rec['source_records'] = [rec['source_records']]

publication_year = get_publication_year(rec.get('publish_date'))
if publication_year and published_in_future_year(publication_year):
del rec['publish_date']

# Split subtitle if required and not already present
if ':' in rec.get('title', '') and not rec.get('subtitle'):
title, subtitle = split_subtitle(rec.get('title'))
Expand Down
24 changes: 24 additions & 0 deletions openlibrary/catalog/add_book/tests/test_add_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from copy import deepcopy
from datetime import datetime
from infogami.infobase.client import Nothing

from infogami.infobase.core import Text
Expand All @@ -18,6 +19,7 @@
isbns_from_record,
load,
load_data,
normalize_import_record,
should_overwrite_promise_item,
split_subtitle,
RequiredField,
Expand Down Expand Up @@ -1451,3 +1453,25 @@ def test_passing_edition_to_load_data_overwrites_edition_with_rec_data(
'ia:newlyscannedpromiseitem',
]
assert edition.works[0]['key'] == '/works/OL1W'


class TestNormalizeImportRecord:
@pytest.mark.parametrize(
'year, expected',
[
("2000-11-11", True),
(str(datetime.now().year), True),
(str(datetime.now().year + 1), False),
("9999-01-01", False),
],
)
def test_future_publication_dates_are_deleted(self, year, expected):
"""It should be impossible to import books publish_date in a future year."""
rec = {
'title': 'test book',
'source_records': ['ia:blob'],
'publish_date': year,
}
normalize_import_record(rec=rec)
result = 'publish_date' in rec
assert result == expected
7 changes: 5 additions & 2 deletions openlibrary/coverstore/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,15 @@ def render_list_preview_image(lst_key):
author_text = "A list on Open Library"
if owner := lst.get_owner():
author_text = f"A list by {owner.displayname}"
w, h = draw.textsize(author_text, font=font_author)

left, top, right, bottom = font_author.getbbox(author_text)
w, h = right - left, bottom - top
draw.text(((W - w) / 2, current_h), author_text, font=font_author, fill=(0, 0, 0))
current_h += h + 5

for line in para:
w, h = draw.textsize(line, font=font_title)
left, top, right, bottom = font_title.getbbox(line)
w, h = right - left, bottom - top
draw.text(((W - w) / 2, current_h), line, font=font_title, fill=(0, 0, 0))
current_h += h

Expand Down
35 changes: 29 additions & 6 deletions openlibrary/i18n/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# i18n Contributor's Guide

Want to get started contributing a language towards Open Library internationalization (i18n)?
I18n pages allow for the translation of content to various languages, enabling users to access localized versions of a webpage based on their locale preferences. For instance, when a user accesses https://openlibrary.org/subjects , they are redirected to https://openlibrary.org/subjects.en or https://openlibrary.org/subjects.es, depending on their selected language. To get started:

**Step 1:** Please kindly reach out to us via the volunteer page [https://openlibrary.org/volunteer#translator](https://openlibrary.org/volunteer#translator)

**Step 2:** Watch this overview
1. Please kindly reach out to us via the volunteer page [https://openlibrary.org/volunteer#translator](https://openlibrary.org/volunteer#translator)
2. Watch this overview:

[![archive org_details_openlibrary-tour-2020_openlibrary-i18n mp4](https://user-images.githubusercontent.com/978325/122978288-33343700-d34b-11eb-858c-774151af4e87.png)](https://archive.org/embed/openlibrary-tour-2020/openlibrary-i18n.mp4?start=8)

Expand All @@ -14,8 +13,7 @@ The messages file format used by the `gettext` toolset is described [here](http:

In case you want to get started here are the following steps:

**Step 3:**
To contribute a translation,
## Contributing translations

Option 1. [Locate the right target language within the project](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/i18n) (e.g. `es` for Spanish) and then click on the `po` file (the raw file where translation strings are contributed), e.g. [this one for Spanish](https://github.com/internetarchive/openlibrary/tree/master/openlibrary/i18n/es). Click on the pencil (edit) option which will bring you to an [editable page like this](https://github.com/internetarchive/openlibrary/edit/master/openlibrary/i18n/es/messages.po) where you can add or edit translations. When you're satisfied with your translations, scroll down to the bottom of the page where it shows **Commit Changes**, leave a description of your changes and make sure to select the radio button of Create a **new branch**. You can call "translations-es" or dash whatever language you're working with. Then, click `Propose Changes` button and you're done! We can follow up if there are any validation issues which may need to be addressed.

Expand Down Expand Up @@ -110,3 +108,28 @@ Languages with deprecated translations:
@cdrini has a script to help automate the creation if i18n versions of openlibrary.org infogami pages (e.g. https://openlibrary.org/about v. https://openlibrary.org/about.es):

https://gist.github.com/cdrini/615d75653e1e47115930fa394e83ab17

# Internationalization (i18n) Page Conversion Guide
Internationalization (i18n) pages are specialized pages within the Infogami platform that enable users to contribute translations. This guide outlines the process of converting a standard page into an i18n page, using the example of converting https://openlibrary.org/librarians to its English (`en`) i18n version.

Follow these steps to convert a standard page into an i18n page. In this example, we'll use https://openlibrary.org/librarians as the source page to be converted into its English (`en`) i18n version.

## Prerequisites

1. Administrator permissions on the Infogami platform.

## Steps

1. **Create the English (`en`) Version of the Page**
- Access the page: https://openlibrary.org/librarians.en.yml?m=edit
- Copy the content from the unsuffixed page: https://openlibrary.org/librarians.yml?m=edit
- Update the `key` field to include the `.en` suffix.
2. **Edit the Un-Suffixed Page**
- Access the unsuffixed page: https://openlibrary.org/librarians.yml?m=edit
- Remove the existing `body` content.
- Change the `type` field to `/type/i18n_page`.
3. **Test**
- Access https://openlibrary.org/librarians to ensure it displays content in English (or your desired locale).
- Access https://openlibrary.org/librarians?lang=es to verify that it displays content in Spanish (or the corresponding locale).

This concludes the process of converting a standard page into an i18n page, making it accessible in multiple languages. Ensure that you have made the necessary updates to the `key` and `type` fields as specified above.
5 changes: 3 additions & 2 deletions openlibrary/macros/ListCarousel.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$def with (list_key, title=None, sort='new', key='', limit=20, has_fulltext_only=True, url=None)
$def with (list_key, title=None, sort='new', key='', limit=20, has_fulltext_only=True, url=None, layout='carousel')

$# Takes following parameters
$# * list_key (str) -- A list key e.g. /people/mekBot/lists/OL104041L
Expand All @@ -8,5 +8,6 @@
$# * limit (int) -- initial number of books to pull
$# * has_fulltext_only (bool) -- only include readable titles
$# * url (str) -- whether to make title a link to url
$# * layout (str) -- layout type, default 'carousel', currently also supports 'grid'

$:macros.QueryCarousel(query=list_key, title=title, sort=sort, key=key, limit=limit, has_fulltext_only=has_fulltext_only, url=url)
$:macros.QueryCarousel(query=list_key, title=title, sort=sort, key=key, limit=limit, has_fulltext_only=has_fulltext_only, url=url, layout=layout)
5 changes: 3 additions & 2 deletions openlibrary/macros/QueryCarousel.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$def with(query, title=None, sort='new', key='', limit=20, search=False, has_fulltext_only=True, url=None)
$def with(query, title=None, sort='new', key='', limit=20, search=False, has_fulltext_only=True, url=None, layout='carousel')

$# Takes following parameters
$# * query (str) -- Any arbitrary Open Library search query, e.g. subject:"Textbooks"
Expand All @@ -7,6 +7,7 @@
$# * key (str) -- unique name of the carousel in analytics
$# * limit (int) -- initial number of books to pull
$# * search (bool) -- whether to include search within collection
$# * layout (str) -- layout type, default 'carousel', currently also supports 'grid'

$# Set default for key
$ key = key or str(abs(hash(query)))
Expand All @@ -33,4 +34,4 @@
books = [storage(b) for b in (results.get('docs', []))]
load_more = {"url": "/search.json?" + urlencode(params), "limit": limit }

$:render_template("books/custom_carousel", books=books, title=title, url=url, key=key, load_more=load_more)
$:render_template("books/custom_carousel", books=books, title=title, url=url, key=key, load_more=load_more, layout=layout)
8 changes: 1 addition & 7 deletions openlibrary/macros/TypeChanger.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
$# and any generic page in the admin UI; e.g. creating a page.
$if ctx.user and ctx.user.is_admin():
<div class="formElement pagetype collapse adminOnly" id="pageType">
<script>
function changeTemplate() {
var t = document.edit['type.key'].value;
document.location.href += '&t=' + t;
}
</script>
<div class="label">
<label for="type.key">$_("Page Type")</label>
<span class="small gray">&nbsp;<a href="javascript:;"
Expand All @@ -18,7 +12,7 @@
<p class="smaller">$_("Every piece of Open Library is defined by the type of content it displays, usually by content definition (e.g. Authors, Editions, Works) but sometimes by content use (e.g. macro, template, rawtext). Changing this for an existing page will alter its presentation and the data fields available for editing its content.") <span class="red">$_("Please use caution changing Page Type!")</span></p><p class="smaller">$_("(Simplest solution: If you aren't sure whether this should be changed, don't change it.)")</p>
</div>
<div class="input">
$:thinginput(type, name="type.key", id="type.key", expected_type="/type/type", kind="regular", onchange="changeTemplate();")
$:thinginput(type, name="type.key", id="type.key", expected_type="/type/type", kind="regular")
</div>
</div>
$else:
Expand Down
7 changes: 7 additions & 0 deletions openlibrary/plugins/openlibrary/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ jQuery(function () {
});
}

// conditionally load for type changing input
const typeChanger = document.getElementById('type.key')
if (typeChanger) {
import(/* webpackChunkName: "type-changer" */ './type_changer.js')
.then(module => module.initTypeChanger(typeChanger));
}

// conditionally load real time signup functionality based on class in the page
if (document.getElementsByClassName('olform create validate').length) {
import(/* webpackChunkName: "realtime-account-validation" */'./realtime_account_validation.js')
Expand Down
19 changes: 19 additions & 0 deletions openlibrary/plugins/openlibrary/js/type_changer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Functionality for TypeChanger.html
*/

export function initTypeChanger(elem) {
// /about?m=edit - where this code is run

function changeTemplate() {
// Change the template of the page based on the selected value
const searchParams = new URLSearchParams(window.location.search);
const t = elem.value;
searchParams.set('t', t);

// Update the URL and navigate to the new page
window.location.search = searchParams.toString();
}

elem.addEventListener('change', changeTemplate);
}
5 changes: 4 additions & 1 deletion openlibrary/plugins/openlibrary/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,10 @@ def GET(self, key):
lst = get_list_seeds(key)
if not lst:
raise web.notfound()
return delegate.RawText(formats.dump(lst, self.encoding))

return delegate.RawText(
formats.dump(lst, self.encoding), content_type=self.content_type
)

def POST(self, key):
site = web.ctx.site
Expand Down
6 changes: 4 additions & 2 deletions openlibrary/templates/admin/imports.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
$ component_times['TotalTime'] = time()

<div id="contentHead">
$:render_template("admin/menu")
$if ctx.user and ctx.user.is_admin():
$:render_template("admin/menu")
<h1>Imports</h1>
<div><a href="/admin/imports/add">Add new books to import queue</a></div>
$if ctx.user and ctx.user.is_admin():
<div><a href="/admin/imports/add">Add new books to import queue</a></div>
</div>

<div id="contentBody">
Expand Down
23 changes: 13 additions & 10 deletions openlibrary/templates/books/custom_carousel.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
$def with(books=[], title="", url="", key="", min_books=1, load_more=None, test=False, compact_mode=False, secondary_action=False)
$def with(books=[], title="", url="", key="", min_books=1, load_more=None, test=False, compact_mode=False, secondary_action=False, layout='carousel')

$ ctx.setdefault("links", [])
$# Apparently fonts always need crossorigin for some reason
$ slick_font = '<link rel="preload" href="/static/css/fonts/slick.woff" as="font" type="font/woff" crossorigin>'
$if slick_font not in ctx.links:
$ ctx.links.append(slick_font)
$if layout == 'carousel':
$ ctx.setdefault("links", [])
$# Apparently fonts always need crossorigin for some reason
$ slick_font = '<link rel="preload" href="/static/css/fonts/slick.woff" as="font" type="font/woff" crossorigin>'
$if slick_font not in ctx.links:
$ ctx.links.append(slick_font)

$def render_carousel_cover(book, lazy):
$def render_carousel_cover(book, lazy, layout):
$ fallback_cover = 'https://openlibrary.org/images/icons/avatar_book.png'
$ cover_host = '//covers.openlibrary.org'
$ url = book.get('key') or book.url
Expand Down Expand Up @@ -46,7 +47,7 @@
<img class="bookcover" loading="lazy"
title="$('%s%s'%(title,byline))"
alt="$('%s%s'%(title,byline))"
$if lazy:
$if lazy and layout == 'carousel':
data-lazy="$(cover_url)"
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="/>
$else:
Expand Down Expand Up @@ -85,10 +86,12 @@ <h2 class="home-h2"><a name="$key" href="$url">$title</a></h2>
$else:
$ config = ['.carousel-' + key, 6, 5, 4, 3, 2, 1, load_more_config ]
$ compact = "carousel--compact" if compact_mode else ""
<div class="carousel $compact carousel-$key carousel--progressively-enhanced"
$ grid = "carousel--grid" if layout == 'grid' else ""
$ loadjs = "carousel--progressively-enhanced" if layout == 'carousel' else ""
<div class="carousel $grid $compact carousel-$key $loadjs"
data-config="$json_encode(config)">
$for index, book in enumerate(books or []):
$:render_carousel_cover(book, index > 5)
$:render_carousel_cover(book, index > 5, layout)
</div>
</div>
</div>
Loading

0 comments on commit c114ecb

Please sign in to comment.