Skip to content
Mek edited this page Feb 9, 2025 · 4 revisions

The Magic Incantation

Any time you want to import and test open library code that needs access to web.ctx, you'll need to run a magic incantation 🧙.

First, you need to exec into the docker container and launch python: docker compose exec -it web python

Next, use the following incantation to load Open Library and launch a minimal headless app:

import web
import infogami
import os
from openlibrary.config import load_config
path = '/olsystem/etc/openlibrary.yml'  # prod
if not os.path.exists('/olsystem/etc/openlibrary.yml'):
  path = 'config/openlibrary.yml'  # fallback to dev
load_config(path)
infogami._setup()
from infogami import config

Fetching a patron's S3 keys

Most privileges actions/requests Open Library takes on behalf of a patron will require their s3 keys:

from openlibrary import accounts
from openlibrary.accounts.model import OpenLibraryAccount

logged_in_user = accounts.get_current_user()
username = logged_in_user and logged_in_user.key.split('/')[-1]
account = username and OpenLibraryAccount.get(username=username)
s3_keys = web.ctx.site.store.get(account._key).get('s3_keys')

Fetching Things in Bulk

keys = ["/works/OL5285479W", "/works/OL257943W", "/works/OL27448W"]
docs = web.ctx.site.get_many(keys)

Privacy Settings

How to determine, given a set of patron usernames, which have public reading logs / can be followed?

usernames = ['mekBot', 'ScarTissue', 'seabelis', 'brewster', 'jachamp']
user_prefs = web.ctx.site.get_many([f'/people/{username}/preferences' for username in usernames])
followable = dict((user.key.split('/')[2], user.notifications.public_readlog == 'yes') for user in user_prefs)

Currently Logged In Patron?

The following must be called within a controller that has access to web.ctx:

user = accounts.get_current_user()

Welcome to the Open Library Handbook! Here you will learn how to...

Developer Guides


Project Management

  1. Directory of Projects
  2. Important Documents by Year
  3. Responsibilities Matrix

Other Portals


Legacy
Orphaned Editions Planning

Clone this wiki locally