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

La til to_string og to_dict metoder for bok-klassen, iht #6

This commit is contained in:
Andreas Lindahl Flåten 2011-03-05 17:00:21 +00:00
parent 65d988cec7
commit 20263c3f07
2 changed files with 59 additions and 19 deletions

View File

@ -8,12 +8,13 @@ def search_book_cmd(search_strings, search_description=False):
books = search_book(search_strings, search_description) books = search_book(search_strings, search_description)
format = '%-13s %-10s %-40s %-30s' format = '%-13s %-10s %-40s %-30s'
for book in books: for book in books:
b_id = book.getid() or '' print book.to_string(True)
title = cut_str(book.title, 40, '*') #b_id = book.getid() or ''
authors = map(lambda p: p.first_name+' '+p.last_name, #title = cut_str(book.title, 40, '*')
book.get_authors()) #authors = map(lambda p: p.first_name+' '+p.last_name,
authors_str = cut_str(', '.join(authors), 30, '*') # book.get_authors())
print format % (book.isbn, b_id, title, authors_str) #authors_str = cut_str(', '.join(authors), 30, '*')
#print format % (book.isbn, b_id, title, authors_str)
def search_book(search_strings, search_description=False): def search_book(search_strings, search_description=False):
basic_query = Book.objects.select_related('persons__person','id','alt_titles') basic_query = Book.objects.select_related('persons__person','id','alt_titles')

View File

@ -1,9 +1,11 @@
from django.db import models from django.db import models
book_fields = {'isbn' : 'ISBN', 'id.id' : 'Identifier', 'title' : 'Title', book_fields = [('title' , 'Title'), ('isbn' , 'ISBN'), ('id.id' , 'Identifier'),
'subtitle' : 'Subtitle', 'category' : 'Category', 'publisher':'Publisher', ('subtitle' , 'Subtitle'), ('category' , 'Category'), ('publisher' , 'Publisher'),
'published_year':'Published year', 'edition' : 'Edition', 'num_pages' : 'Number of pages', ('published_year' , 'Published year'), ('edition' , 'Edition'), ('num_pages' , 'Number of pages'),
'series' : 'Series', 'description' : 'Description'} ('series' , 'Series'), ('description' , 'Description')]
class Category(models.Model): class Category(models.Model):
id = models.CharField(max_length=255, primary_key=True) 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) 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) thumbnail = models.ImageField(upload_to='%Y/%m/%d/thumbnails', null=True, blank=True)
references = models.ManyToManyField(Reference, related_name='books',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): def getid(self):
if self.id: if self.id:
return self.id.id return self.id.id