Fikset litt på show-kommandoen.
Nå kan man slå opp bøker etter id også, ikke bare isbn. Dessuten får man en fornuftig feilmelding hvis man prøver å vise noe som ikke eksisterer.
This commit is contained in:
parent
8a578ffdb2
commit
39c3e4ec38
|
@ -13,24 +13,22 @@ import placement
|
||||||
|
|
||||||
def show_book_or_person(ids, commit_format=False, tmp_file=False):
|
def show_book_or_person(ids, commit_format=False, tmp_file=False):
|
||||||
for id in ids:
|
for id in ids:
|
||||||
object = get_book_or_person(id)
|
object = get_book_or_person(id)
|
||||||
# TODO: skriv ut mer informasjon
|
if object:
|
||||||
print object.to_string()
|
print object.to_string()
|
||||||
|
else:
|
||||||
|
print 'No book or person with id %s.' % id
|
||||||
|
|
||||||
def get_book_or_person(id):
|
def get_book_or_person(id):
|
||||||
if id.isdigit():
|
books = Book.objects.filter(Q(isbn=id)|Q(id__id=id)).all()
|
||||||
return get_in_order([Book,Person],id)
|
persons = Person.objects.filter(id=id)
|
||||||
else:
|
if len(books) + len(persons) > 1:
|
||||||
return get_in_order([Person,Book],id)
|
print 'Warning: More than one match for id %d.' % id
|
||||||
|
print 'This should not happen.'
|
||||||
def get_in_order(list, id):
|
if len(books) > 0:
|
||||||
for object in list:
|
return books[0]
|
||||||
try:
|
if len(persons) > 0:
|
||||||
b = object.objects.get(pk=id)
|
return persons[0]
|
||||||
return b
|
|
||||||
except object.DoesNotExist:
|
|
||||||
pass
|
|
||||||
return None
|
|
||||||
|
|
||||||
def remove_duplicates(list):
|
def remove_duplicates(list):
|
||||||
d = {}
|
d = {}
|
||||||
|
|
Reference in New Issue