Lagt til støtte for å vise kategorier med 'show'-kommandoen.
This commit is contained in:
parent
1ee76ee0b6
commit
9655f19363
|
@ -12,6 +12,24 @@ class Category(models.Model):
|
|||
id = models.CharField(max_length=255, primary_key=True)
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
def to_string(self):
|
||||
str = 'category %s\nName: %s\n' % (self.id, self.name)
|
||||
str += 'Placement: %s\n' % self.placements_str()
|
||||
str += 'Books:\n'
|
||||
for book in self.books.all():
|
||||
str += ' %-13s %-10s %s\n' % (book.isbn, book.getid() or '', book.title)
|
||||
if len(self.books.all()) == 0:
|
||||
str += ' (no books in this category)\n'
|
||||
return str
|
||||
|
||||
def to_dict(self):
|
||||
return { 'action': 'edit-category',
|
||||
'id': self.id, 'name': self.name,
|
||||
'placement': self.placements_str() }
|
||||
|
||||
def placements_str(self):
|
||||
return ', '.join(map(lambda pl: pl.shelf, self.placement.all()))
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
|
@ -33,7 +51,7 @@ class Book(models.Model):
|
|||
# id = models.CharField(max_length=255)
|
||||
title = models.CharField(max_length=511)
|
||||
subtitle = models.CharField(max_length=511, null=True, blank=True)
|
||||
category = models.ForeignKey(Category, null=True, blank=True)
|
||||
category = models.ForeignKey(Category, null=True, blank=True, related_name='books')
|
||||
publisher = models.CharField(max_length=255, null=True, blank=True)
|
||||
published_year = models.IntegerField(null=True, blank=True)
|
||||
edition = models.IntegerField(null=True, blank=True)
|
||||
|
|
|
@ -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': [],
|
||||
|
|
Reference in New Issue