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 ' \
|
'FROM book ' \
|
||||||
'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \
|
'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \
|
||||||
'LEFT JOIN person ON bookperson.person=person.id'
|
'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():
|
def connect_to_db():
|
||||||
connection = pgdb.connect(database='oysteini_pbb2',
|
connection = pgdb.connect(database='oysteini_pbb2',
|
||||||
|
@ -45,6 +50,20 @@ def get_by_id(connection, id):
|
||||||
return d
|
return d
|
||||||
return None
|
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):
|
def show_book(book):
|
||||||
s = ''
|
s = ''
|
||||||
if book['id']:
|
if book['id']:
|
||||||
|
@ -56,7 +75,11 @@ def show_book(book):
|
||||||
s += 'Subtitle: %s\n' % book['subtitle']
|
s += 'Subtitle: %s\n' % book['subtitle']
|
||||||
s += 'ISBN: %s\n' % book['isbn']
|
s += 'ISBN: %s\n' % book['isbn']
|
||||||
s += 'Persons:\n'
|
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']:
|
if book['series']:
|
||||||
s += 'Part of series: %s %s\n' % (book['series'], book['series_title'])
|
s += 'Part of series: %s %s\n' % (book['series'], book['series_title'])
|
||||||
s += 'Category: %s\n' % book['category']
|
s += 'Category: %s\n' % book['category']
|
||||||
|
@ -83,16 +106,22 @@ def show_category(category):
|
||||||
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)
|
||||||
for i in range(len(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]:
|
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:
|
||||||
objects[i]['action'] = 'edit-%s' % objects[i]['type']
|
objects[i]['action'] = 'edit-%s' % typ
|
||||||
else:
|
else:
|
||||||
show_funs = {'book': show_book,
|
show_funs = {'book': show_book,
|
||||||
'person': show_person,
|
'person': show_person,
|
||||||
'category': show_category}
|
'category': show_category}
|
||||||
show_fun = show_funs[objects[i]['type']]
|
show_fun = show_funs[objects[i]['type']]
|
||||||
objects[i] = show_fun(objects[i])
|
objects[i] = show_fun(objects[i])
|
||||||
|
|
||||||
if commit_format:
|
if commit_format:
|
||||||
output = write_actionlist(objects)
|
output = write_actionlist(objects)
|
||||||
else:
|
else:
|
||||||
|
|
Reference in New Issue