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

Fix list add endpoint erroring #8869

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion openlibrary/plugins/openlibrary/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import automaticInit from './automatic';
import bookReaderInit from './bookreader_direct';
import { ungettext, ugettext, sprintf } from './i18n';
import jQueryRepeat from './jquery.repeat';
import { enumerate, htmlquote, websafe, foreach, join, len, range } from './jsdef';
import { enumerate, htmlquote, websafe, foreach, join, len, range, jsdef_get } from './jsdef';
import initAnalytics from './ol.analytics';
import init from './ol.js';
import * as Browser from './Browser';
Expand All @@ -32,6 +32,7 @@ window.cond = cond;
window.enumerate = enumerate;
window.foreach = foreach;
window.htmlquote = htmlquote;
window.jsdef_get = jsdef_get;
window.len = len;
window.range = range;
window.slice = slice;
Expand Down
14 changes: 14 additions & 0 deletions openlibrary/plugins/openlibrary/js/jsdef.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ export function htmlquote(text) {
export function is_jsdef() {
return true;
}


/**
* foo.get(KEY, default) isn't defined in js, so we can't use that construct
* in our jsdef methods. This helper function provides a workaround, and works
* in both environments.
*
* @param {object} obj - the object to get the key from
* @param {string} key - the key to get from the object
* @param {any} def - the default value to return if the key isn't found
*/
export function jsdef_get(obj, key, def=null) {
return (key in obj) ? obj[key] : def;
}
2 changes: 1 addition & 1 deletion openlibrary/plugins/upstream/jsdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def py2js(expr):
>>> py2js("x or not y")
'x || ! y'
"""
d = {"and": "&&", "or": "||", "not": "!"}
d = {"and": "&&", "or": "||", "not": "!", "None": "null"}

def f(tokens):
for t in tokens:
Expand Down
10 changes: 10 additions & 0 deletions openlibrary/plugins/upstream/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,16 @@ def is_jsdef():
return False


@public
def jsdef_get(obj, key, default=None):
"""
foo.get(KEY, default) isn't defined in js, so we can't use that construct
in our jsdef methods. This helper function provides a workaround, and works
in both environments.
"""
return obj.get(key, default)


@public
def get_donation_include() -> str:
ia_host = get_ia_host(allow_dev=True)
Expand Down
5 changes: 3 additions & 2 deletions openlibrary/templates/type/list/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ <h1>$(_("Create a list") if new else _("Edit List"))</h1>

$# Render the ith seed input field
$jsdef render_seed_field(i, seed):
$# Note: Cannot use "in" because this is a jsdef function
$if seed['key'] or seed['key']:
$# seed['key'] errors in python; seed.get('key') errors in js. So need
$# to run different code on the server and client.
$if jsdef_get(seed, 'key') != None:
$ seed = { 'thing': seed, 'notes': '' }

$ key = ''
Expand Down
Loading