Quote:
Originally Posted by ownedbycats
Yes, that's where I figured to put it.  Should've bee a little more clear. What I'm a bit confused on is whether it could be plugged into a template (presumably Action Chains' single-field edit) to convert an identifier from isbn10 to 13.
|
Sure. It is a template function like any other. You just make a GPM template to call it.
Code:
program: whatever_you_called_it()
You could instead make it a stored template instead of a template function.
Example:
This template is slightly different from the function above, correcting a bug and making it return the existing isbn if it isn't 10 long.
Code:
def evaluate(book, context):
isbn = book.identifiers.get('isbn', '')
if len(isbn) != 10:
# This isn't an isbn 10. Return it as it is.
return isbn
prefix = '978' + isbn[:-1]
check = check_digit_13(prefix)
return prefix + check
def check_digit_13(isbn):
sum = 0
for i in range(len(isbn)):
c = int(isbn[i])
if i % 2: w = 3
else: w = 1
sum += w * c
r = 10 - (sum % 10)
if r == 10: return '0'
else: return str(r)
Call it with
Code:
program: isbn10_to_13()