Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

Implementert visning av kategori.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-10-08 14:35:26 +00:00
parent d9f41d3e7e
commit 14d98f3d57
1 changed files with 22 additions and 3 deletions

View File

@ -43,6 +43,10 @@ q_books_for_person = \
'FROM bookperson ' \ 'FROM bookperson ' \
'INNER JOIN book ON bookperson.book=book.isbn ' \ 'INNER JOIN book ON bookperson.book=book.isbn ' \
'WHERE bookperson.person=%(id)s' 'WHERE bookperson.person=%(id)s'
q_books_for_category = \
'SELECT isbn, title ' \
'FROM book ' \
'WHERE category=%(id)s'
def connect_to_db(): def connect_to_db():
connection = pgdb.connect(database='oysteini_pbb2', 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}) c.execute(q_books_for_person, {'id': person_id})
return fetchall_dict(c) 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): def show_book(book):
s = '' s = ''
if book['id']: if book['id']:
@ -128,8 +137,15 @@ def show_person(person):
s += ' (no books by this person)\n' s += ' (no books by this person)\n'
return s return s
def show_category(category): def show_category(cat):
return '' 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): def show(connection, ids, commit_format=False, tmp_file=False):
objects = map(lambda id: get_by_id(connection, id), ids) 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']) persons = get_persons_for_book(connection, objects[i]['isbn'])
objects[i]['persons'] = list_of_dicts_to_dict(persons, 'relation', 'id') objects[i]['persons'] = list_of_dicts_to_dict(persons, 'relation', 'id')
objects[i]['persons_data'] = persons objects[i]['persons_data'] = persons
if typ == 'person': elif typ == 'person':
books = get_books_for_person(connection, objects[i]['id']) books = get_books_for_person(connection, objects[i]['id'])
objects[i]['books'] = books objects[i]['books'] = books
elif typ == 'category':
books = get_books_for_category(connection, objects[i]['id'])
objects[i]['books'] = books
if not objects[i]: if not objects[i]:
objects[i] = 'No object with id %s.\n' % ids[i] objects[i] = 'No object with id %s.\n' % ids[i]
elif commit_format: elif commit_format: