-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Coding Recipes
Mek edited this page Feb 9, 2025
·
4 revisions
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
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')
keys = ["/works/OL5285479W", "/works/OL257943W", "/works/OL27448W"]
docs = web.ctx.site.get_many(keys)
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)
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...
- Get Set Up
- Understand the Codebase
- Contribute to the Front-end
- Contribute to the Back-end
- Manage your developer environment
- Lookup Common Recipes
- Participate in the Community
Developer Guides
- BookWorm / Affiliate Server
- Developing the My Books & Reading Log
- Developing the Books page
- Understanding the "Read" Button
Other Portals
- Design
- Librarianship
- Communications
- Staff (internal)
Legacy
Orphaned Editions Planning