From 20263c3f078a434fb966a5a687adc36dc9991627 Mon Sep 17 00:00:00 2001 From: andrfla Date: Sat, 5 Mar 2011 17:00:21 +0000 Subject: [PATCH] La til to_string og to_dict metoder for bok-klassen, iht #6 --- python/search.py | 13 ++++---- python/web/library/models.py | 65 ++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/python/search.py b/python/search.py index 0dcf56c..49a2cc2 100644 --- a/python/search.py +++ b/python/search.py @@ -8,12 +8,13 @@ def search_book_cmd(search_strings, search_description=False): books = search_book(search_strings, search_description) format = '%-13s %-10s %-40s %-30s' for book in books: - b_id = book.getid() or '' - title = cut_str(book.title, 40, '*') - authors = map(lambda p: p.first_name+' '+p.last_name, - book.get_authors()) - authors_str = cut_str(', '.join(authors), 30, '*') - print format % (book.isbn, b_id, title, authors_str) + print book.to_string(True) + #b_id = book.getid() or '' + #title = cut_str(book.title, 40, '*') + #authors = map(lambda p: p.first_name+' '+p.last_name, + # book.get_authors()) + #authors_str = cut_str(', '.join(authors), 30, '*') + #print format % (book.isbn, b_id, title, authors_str) def search_book(search_strings, search_description=False): basic_query = Book.objects.select_related('persons__person','id','alt_titles') diff --git a/python/web/library/models.py b/python/web/library/models.py index d6e2258..d144012 100644 --- a/python/web/library/models.py +++ b/python/web/library/models.py @@ -1,9 +1,11 @@ from django.db import models -book_fields = {'isbn' : 'ISBN', 'id.id' : 'Identifier', 'title' : 'Title', -'subtitle' : 'Subtitle', 'category' : 'Category', 'publisher':'Publisher', -'published_year':'Published year', 'edition' : 'Edition', 'num_pages' : 'Number of pages', -'series' : 'Series', 'description' : 'Description'} +book_fields = [('title' , 'Title'), ('isbn' , 'ISBN'), ('id.id' , 'Identifier'), +('subtitle' , 'Subtitle'), ('category' , 'Category'), ('publisher' , 'Publisher'), +('published_year' , 'Published year'), ('edition' , 'Edition'), ('num_pages' , 'Number of pages'), +('series' , 'Series'), ('description' , 'Description')] + + class Category(models.Model): id = models.CharField(max_length=255, primary_key=True) @@ -47,16 +49,53 @@ class Book(models.Model): picture = models.ImageField(upload_to='%Y/%m/%d/pictures', null=True, blank=True) thumbnail = models.ImageField(upload_to='%Y/%m/%d/thumbnails', null=True, blank=True) references = models.ManyToManyField(Reference, related_name='books',null=True, blank=True) - - def full_print(self): - for field in book_fields.items(): - try: - print '%-15s: %50s' % (field[1],eval('self.'+field[0])) - except Category.DoesNotExist: - print '%-15s: %50s' % (field[1], 'Does not exist') - except Id.DoesNotExist: - print '%-15s: %50s' % (field[1], '') + #Generate a string from book info + def to_string(self, commit=False): + scratch = '' + authors = ','.join(map(lambda x: str(x),self.get_authors())) + if commit: + # generate commit string by sending self.to_dict to oysteini function + print self.to_dict() + + else: + for field in book_fields: + try: + scratch += '%-15s: %50s' % (field[1],eval('self.'+field[0])) + '\n' + except AttributeError: + scratch += '%-15s: %50s' % (field[1], '') + '\n' + scratch += 'Authors'.ljust(15, ' ') + ':' + scratch += authors.rjust(51, ' ') + + scratch += '\n' + return scratch + + def to_dict(self): + dict = {} + dict['action'] = 'edit-book' + for field in book_fields: + try: + dict[field[0]] = eval('self.'+field[0]) + except AttributeError: + dict[field[0]] = ' ' + + people = self.persons.all() + dict['persons'] = {} + for person in people: + if person.relation.name in dict['persons']: + dict['persons'][person.relation.name].append(person.id) + else: + dict['persons'][person.relation.name] = [person.id] + + dict['references'] = [] + for reference in self.references: + dict['references'].append(reference.text) + + #TODO: add thumbnail and image fields + + return dict + + def getid(self): if self.id: return self.id.id