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

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:
Øystein Ingmar Skartsæterhagen 2011-03-06 11:21:44 +00:00
parent 8a578ffdb2
commit 39c3e4ec38
1 changed files with 14 additions and 16 deletions

View File

@ -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 = {}