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

Remove unused /ia/sync handler #9707

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions openlibrary/plugins/admin/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,77 +325,6 @@ def GET(self):
return delegate.RawText(json.dumps(data), content_type="application/json")


class sync_ia_ol(delegate.page):
path = '/ia/sync'
encoding = 'json'

def POST(self):
# Authenticate request:
s3_access_key = web.ctx.env.get('HTTP_X_S3_ACCESS', '')
s3_secret_key = web.ctx.env.get('HTTP_X_S3_SECRET', '')

if not self.is_authorized(s3_access_key, s3_secret_key):
raise web.unauthorized()

# Validate input
i = json.loads(web.data())

if not self.validate_input(i):
raise web.badrequest('Missing required fields')

# Find record using OLID (raise 404 if not found)
edition_key = f'/books/{i.get("olid")}'
edition = web.ctx.site.get(edition_key)

if not edition:
raise web.notfound()

# Update record
match i.get('action', ''):
case 'remove':
self.remove_ocaid(edition)
case 'modify':
self.modify_ocaid(edition, i.get('ocaid'))
case '_':
raise web.badrequest('Unknown action')

return delegate.RawText(json.dumps({"status": "ok"}))

def is_authorized(self, access_key, secret_key):
"""Returns True if account is authorized to make changes to records."""
auth = accounts.InternetArchiveAccount.s3auth(access_key, secret_key)

if not auth.get('username', ''):
return False

acct = accounts.OpenLibraryAccount.get(email=auth.get('username'))
user = acct.get_user() if acct else None

return user and user.is_usergroup_member('/usergroup/ia')

def validate_input(self, i):
"""Returns True if the request is valid.
All requests must have an olid and an action. If the action is
'modify', the request must also include 'ocaid'.
"""
action = i.get('action', '')
return 'olid' in i and (
action == 'remove' or (action == 'modify' and 'ocaid' in i)
)

def remove_ocaid(self, edition):
"""Deletes OCAID from given edition"""
data = edition.dict()
del data['ocaid']
web.ctx.site.save(data, 'Remove OCAID: Item no longer available to borrow.')

def modify_ocaid(self, edition, new_ocaid):
"""Adds the given new_ocaid to an edition."""
data = edition.dict()
data['ocaid'] = new_ocaid
web.ctx.site.save(data, 'Update OCAID')


class people_view:
def GET(self, key):
account = accounts.find(username=key) or accounts.find(email=key)
Expand Down
Loading