diff --git a/cli/worblehat.py b/cli/worblehat.py index 3d2cbf6..9a14b03 100644 --- a/cli/worblehat.py +++ b/cli/worblehat.py @@ -27,6 +27,11 @@ q_persons_for_book = \ 'FROM person ' \ 'INNER JOIN bookperson ON person.id=bookperson.person ' \ 'WHERE bookperson.book=%(isbn)s' +q_books_for_person = \ + 'SELECT isbn, title, relation ' \ + 'FROM bookperson ' \ + 'INNER JOIN book ON bookperson.book=book.isbn ' \ + 'WHERE bookperson.person=%(id)s' def connect_to_db(): connection = pgdb.connect(database='oysteini_pbb2', @@ -64,6 +69,11 @@ def get_persons_for_book(connection, isbn): c.execute(q_persons_for_book, {'isbn': isbn}) return fetchall_dict(c) +def get_books_for_person(connection, person_id): + c = connection.cursor() + c.execute(q_books_for_person, {'id': person_id}) + return fetchall_dict(c) + def show_book(book): s = '' if book['id']: @@ -98,7 +108,14 @@ def show_book(book): return s def show_person(person): - return '' + s = 'person %s\n' % person['id'] + s += 'Name: %s %s\n' % (person['firstname'], person['lastname']) + s += 'Books:\n' + for book in person['books']: + s += ' %-13s %s (%s)\n' % (book['isbn'], book['title'], book['relation']) + if len(person['books']) == 0: + s += ' (no books by this person)\n' + return s def show_category(category): return '' @@ -111,6 +128,9 @@ def show(connection, ids, commit_format=False, tmp_file=False): persons = get_persons_for_book(connection, objects[i]['isbn']) objects[i]['persons'] = list_of_dicts_to_dict(persons, 'relation', 'id') objects[i]['persons_data'] = persons + if typ == 'person': + books = get_books_for_person(connection, objects[i]['id']) + objects[i]['books'] = books if not objects[i]: objects[i] = 'No object with id %s.\n' % ids[i] elif commit_format: