From 9655f193639bba5919b7f6fb9889a0a2d377991b Mon Sep 17 00:00:00 2001 From: oysteini Date: Tue, 8 Mar 2011 13:53:19 +0000 Subject: [PATCH] =?UTF-8?q?Lagt=20til=20st=C3=B8tte=20for=20=C3=A5=20vise?= =?UTF-8?q?=20kategorier=20med=20'show'-kommandoen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/web/library/models.py | 20 +++++++++++++++++++- python/worblehat.py | 16 ++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/python/web/library/models.py b/python/web/library/models.py index c2c5c5f..329cf00 100644 --- a/python/web/library/models.py +++ b/python/web/library/models.py @@ -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) diff --git a/python/worblehat.py b/python/worblehat.py index bc0c6ee..ec30796 100755 --- a/python/worblehat.py +++ b/python/worblehat.py @@ -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': [],