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

Implementert visning av person.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-10-08 13:27:03 +00:00
parent 7eb9967524
commit bf3d8f78a6
1 changed files with 21 additions and 1 deletions

View File

@ -27,6 +27,11 @@ q_persons_for_book = \
'FROM person ' \ 'FROM person ' \
'INNER JOIN bookperson ON person.id=bookperson.person ' \ 'INNER JOIN bookperson ON person.id=bookperson.person ' \
'WHERE bookperson.book=%(isbn)s' '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(): def connect_to_db():
connection = pgdb.connect(database='oysteini_pbb2', 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}) c.execute(q_persons_for_book, {'isbn': isbn})
return fetchall_dict(c) 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): def show_book(book):
s = '' s = ''
if book['id']: if book['id']:
@ -98,7 +108,14 @@ def show_book(book):
return s return s
def show_person(person): 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): def show_category(category):
return '' return ''
@ -111,6 +128,9 @@ 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':
books = get_books_for_person(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: