diff --git a/cli/worblehat.py b/cli/worblehat.py index 305eaff..3283441 100755 --- a/cli/worblehat.py +++ b/cli/worblehat.py @@ -83,6 +83,9 @@ q_edit_category = \ 'UPDATE category ' \ 'SET name=%(name)s ' \ 'WHERE id=%(id)s' +q_add_bookreference = \ + 'INSERT INTO bookreference (book, reftype, value) ' \ + 'VALUES (%(isbn)s, %(reftype)s, %(value))' def connect_to_db(): connection = pgdb.connect(database='oysteini_pbb2', @@ -308,6 +311,23 @@ def do_action(connection, action): {'isbn': action['isbn'], 'person_id': person, 'relation': relation}) + if action['references']: + c.execute('SELECT referencetype.id FROM referencetype') + refs = c.fetchone() + for (reftype, reflist) in action['references'].items(): + for ref in reflist: + if ref in refs: + c.execute(q_add_reference, + {'isbn': action['isbn'], + 'reftype': reftype, + 'value': ref}) + else: + raise WorblehatException('%s is not in the defined references, please use a more general one' % reftype) + +class WorblehatException(Exception): + def __init__(self, msg): + Exception.__init__(self, msg) + def commit_actions(connection, actions): for action in actions: