Lagt til støtte for å vise kategorier med 'show'-kommandoen.

This commit is contained in:
2011-03-08 13:53:19 +00:00
parent 1ee76ee0b6
commit 9655f19363
2 changed files with 29 additions and 7 deletions

View File

@@ -18,8 +18,8 @@ import tempfile
file_encoding = 'utf8'
def show_book_or_person(ids, commit_format=False, tmp_file=False):
objects = map(get_book_or_person, ids)
def show(ids, commit_format=False, tmp_file=False):
objects = map(get_object_by_id, ids)
for i in range(len(ids)):
if not objects[i]:
objects[i] = 'No book or person with id %s.\n' % ids[i]
@@ -38,16 +38,20 @@ def show_book_or_person(ids, commit_format=False, tmp_file=False):
else:
print output.strip()
def get_book_or_person(id):
def get_object_by_id(id):
books = Book.objects.filter(Q(isbn=id)|Q(id__id=id)).all()
persons = Person.objects.filter(id=id)
if len(books) + len(persons) > 1:
categories = Category.objects.filter(id=id)
if len(books) + len(persons) + len(categories) > 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]
if len(categories) > 0:
return categories[0]
return None
def remove_duplicates(list):
d = {}
@@ -198,7 +202,7 @@ def perform_action(a, new_persons):
return p
def edit_book_or_person(ids):
filename = show_book_or_person(ids, commit_format=True, tmp_file=True)
filename = show(ids, commit_format=True, tmp_file=True)
print filename
run_editor(filename)
commit(filename)
@@ -206,7 +210,7 @@ def edit_book_or_person(ids):
commands = { 'show':
{ 'args': [('ids', (1,None))],
'options': ['commit_format', 'tmp_file'],
'fun': show_book_or_person },
'fun': show },
'list':
{ 'args': [('what', (1,1))],
'options': [],