diff --git a/python/worblehat.py b/python/worblehat.py index 5bc4694..c362133 100755 --- a/python/worblehat.py +++ b/python/worblehat.py @@ -13,24 +13,22 @@ import placement def show_book_or_person(ids, commit_format=False, tmp_file=False): for id in ids: - object = get_book_or_person(id) - # TODO: skriv ut mer informasjon - print object.to_string() + object = get_book_or_person(id) + if object: + print object.to_string() + else: + print 'No book or person with id %s.' % id def get_book_or_person(id): - if id.isdigit(): - return get_in_order([Book,Person],id) - else: - return get_in_order([Person,Book],id) - -def get_in_order(list, id): - for object in list: - try: - b = object.objects.get(pk=id) - return b - except object.DoesNotExist: - pass - return None + books = Book.objects.filter(Q(isbn=id)|Q(id__id=id)).all() + persons = Person.objects.filter(id=id) + if len(books) + len(persons) > 1: + print 'Warning: More than one match for id %d.' % id + print 'This should not happen.' + if len(books) > 0: + return books[0] + if len(persons) > 0: + return persons[0] def remove_duplicates(list): d = {}