diff --git a/cli/worblehat.py b/cli/worblehat.py index e3d3f42..fd45de0 100644 --- a/cli/worblehat.py +++ b/cli/worblehat.py @@ -43,6 +43,10 @@ q_books_for_person = \ 'FROM bookperson ' \ 'INNER JOIN book ON bookperson.book=book.isbn ' \ 'WHERE bookperson.person=%(id)s' +q_books_for_category = \ + 'SELECT isbn, title ' \ + 'FROM book ' \ + 'WHERE category=%(id)s' def connect_to_db(): connection = pgdb.connect(database='oysteini_pbb2', @@ -85,6 +89,11 @@ def get_books_for_person(connection, person_id): c.execute(q_books_for_person, {'id': person_id}) return fetchall_dict(c) +def get_books_for_category(connection, cat_id): + c = connection.cursor() + c.execute(q_books_for_category, {'id': cat_id}) + return fetchall_dict(c) + def show_book(book): s = '' if book['id']: @@ -128,8 +137,15 @@ def show_person(person): s += ' (no books by this person)\n' return s -def show_category(category): - return '' +def show_category(cat): + s = 'category %s\n' % cat['id'] + s += 'Name: %s\n' % cat['name'] + s += 'Books:\n' + for book in cat['books']: + s += ' %-13s %s\n' % (book['isbn'], book['title']) + if len(cat['books']) == 0: + s += ' (no books)\n' + return s def show(connection, ids, commit_format=False, tmp_file=False): objects = map(lambda id: get_by_id(connection, id), ids) @@ -139,9 +155,12 @@ 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': + elif typ == 'person': books = get_books_for_person(connection, objects[i]['id']) objects[i]['books'] = books + elif typ == 'category': + books = get_books_for_category(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: