Fikset litt på visning av bøker. Får med personer knyttet til boken.
This commit is contained in:
parent
6d50951cf0
commit
7eb9967524
|
@ -22,6 +22,11 @@ q_list_books = \
|
|||
'FROM book ' \
|
||||
'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \
|
||||
'LEFT JOIN person ON bookperson.person=person.id'
|
||||
q_persons_for_book = \
|
||||
'SELECT person.id, lastname, firstname, relation ' \
|
||||
'FROM person ' \
|
||||
'INNER JOIN bookperson ON person.id=bookperson.person ' \
|
||||
'WHERE bookperson.book=%(isbn)s'
|
||||
|
||||
def connect_to_db():
|
||||
connection = pgdb.connect(database='oysteini_pbb2',
|
||||
|
@ -45,6 +50,20 @@ def get_by_id(connection, id):
|
|||
return d
|
||||
return None
|
||||
|
||||
def list_of_dicts_to_dict(lst, key_name, value_name):
|
||||
res = {}
|
||||
for d in lst:
|
||||
if d[key_name] in res:
|
||||
res[d[key_name]].append(d[value_name])
|
||||
else:
|
||||
res[d[key_name]] = [d[value_name]]
|
||||
return res
|
||||
|
||||
def get_persons_for_book(connection, isbn):
|
||||
c = connection.cursor()
|
||||
c.execute(q_persons_for_book, {'isbn': isbn})
|
||||
return fetchall_dict(c)
|
||||
|
||||
def show_book(book):
|
||||
s = ''
|
||||
if book['id']:
|
||||
|
@ -56,7 +75,11 @@ def show_book(book):
|
|||
s += 'Subtitle: %s\n' % book['subtitle']
|
||||
s += 'ISBN: %s\n' % book['isbn']
|
||||
s += 'Persons:\n'
|
||||
# TODO
|
||||
for bp in book['persons_data']:
|
||||
s += ' %s %s %s (%s)\n' % (bp['id'], bp['firstname'], bp['lastname'],
|
||||
bp['relation'])
|
||||
if len(book['persons_data']) == 0:
|
||||
s += ' (no persons associated with this book)\n'
|
||||
if book['series']:
|
||||
s += 'Part of series: %s %s\n' % (book['series'], book['series_title'])
|
||||
s += 'Category: %s\n' % book['category']
|
||||
|
@ -83,16 +106,22 @@ def show_category(category):
|
|||
def show(connection, ids, commit_format=False, tmp_file=False):
|
||||
objects = map(lambda id: get_by_id(connection, id), ids)
|
||||
for i in range(len(ids)):
|
||||
typ = objects[i]['type']
|
||||
if typ == 'book':
|
||||
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 not objects[i]:
|
||||
objects[i] = 'No object with id %s.\n' % ids[i]
|
||||
elif commit_format:
|
||||
objects[i]['action'] = 'edit-%s' % objects[i]['type']
|
||||
objects[i]['action'] = 'edit-%s' % typ
|
||||
else:
|
||||
show_funs = {'book': show_book,
|
||||
'person': show_person,
|
||||
'category': show_category}
|
||||
show_fun = show_funs[objects[i]['type']]
|
||||
objects[i] = show_fun(objects[i])
|
||||
|
||||
if commit_format:
|
||||
output = write_actionlist(objects)
|
||||
else:
|
||||
|
|
Reference in New Issue