2010-09-17 17:14:10 +02:00
|
|
|
from django.db import models
|
|
|
|
|
2011-03-05 18:00:21 +01:00
|
|
|
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')]
|
|
|
|
|
|
|
|
|
2010-09-18 17:20:50 +02:00
|
|
|
|
2010-09-17 19:24:29 +02:00
|
|
|
class Category(models.Model):
|
|
|
|
id = models.CharField(max_length=255, primary_key=True)
|
|
|
|
name = models.CharField(max_length=255)
|
2010-09-17 17:14:10 +02:00
|
|
|
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-09-17 17:14:10 +02:00
|
|
|
class BookSeries(models.Model):
|
2010-09-17 19:24:29 +02:00
|
|
|
id = models.CharField(max_length=20, primary_key=True)
|
|
|
|
title = models.CharField(max_length=511)
|
|
|
|
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
2010-09-18 17:20:50 +02:00
|
|
|
class ReferenceType(models.Model):
|
|
|
|
name = models.CharField(max_length=31)
|
|
|
|
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-09-18 17:20:50 +02:00
|
|
|
class Reference(models.Model):
|
|
|
|
reference_type = models.ForeignKey(ReferenceType)
|
|
|
|
text = models.CharField(max_length=1023)
|
|
|
|
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.text
|
|
|
|
|
2010-09-17 19:24:29 +02:00
|
|
|
class Book(models.Model):
|
|
|
|
isbn = models.CharField(max_length=13, primary_key=True)
|
2010-09-27 17:00:10 +02:00
|
|
|
# id = models.CharField(max_length=255)
|
2010-09-17 19:24:29 +02:00
|
|
|
title = models.CharField(max_length=511)
|
|
|
|
subtitle = models.CharField(max_length=511, null=True, blank=True)
|
2010-09-25 18:03:23 +02:00
|
|
|
category = models.ForeignKey(Category, null=True, blank=True)
|
2010-09-17 19:24:29 +02:00
|
|
|
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)
|
|
|
|
num_pages = models.IntegerField(null=True, blank=True)
|
2010-09-28 15:28:48 +02:00
|
|
|
series = models.ForeignKey(BookSeries, related_name='books', null=True, blank=True)
|
2010-09-17 19:24:29 +02:00
|
|
|
description = models.CharField(max_length=1023, 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)
|
2011-03-04 15:22:06 +01:00
|
|
|
references = models.ManyToManyField(Reference, related_name='books',null=True, blank=True)
|
2010-09-27 17:00:10 +02:00
|
|
|
|
2011-03-05 18:00:21 +01:00
|
|
|
#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
|
|
|
|
|
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
def getid(self):
|
2011-03-04 14:37:00 +01:00
|
|
|
if self.id:
|
2010-09-27 17:00:10 +02:00
|
|
|
return self.id.id
|
2011-03-04 14:37:00 +01:00
|
|
|
return None
|
2010-09-27 17:00:10 +02:00
|
|
|
|
|
|
|
def setid(self,newid):
|
2011-03-04 14:37:00 +01:00
|
|
|
if self.id:
|
|
|
|
self.id.delete()
|
|
|
|
newid = Id(book=self,id=newid)
|
|
|
|
newid.save()
|
2010-09-27 19:33:51 +02:00
|
|
|
|
|
|
|
def get_authors(self):
|
2010-09-27 22:44:57 +02:00
|
|
|
people = self.persons.all()
|
2010-09-27 19:33:51 +02:00
|
|
|
authors = []
|
|
|
|
for person in people:
|
|
|
|
if person.relation.name == 'Author':
|
|
|
|
authors.append(person.person)
|
|
|
|
return authors
|
2010-09-27 17:00:10 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
# def print(self):
|
|
|
|
# print '%13s %5s %30s %'
|
|
|
|
|
|
|
|
# class Meta:
|
|
|
|
# unique_together=(("isbn","id"),)
|
|
|
|
|
|
|
|
class Id(models.Model):
|
|
|
|
id = models.CharField(max_length=511, primary_key=True)
|
|
|
|
book = models.OneToOneField(Book)
|
2010-09-17 17:14:10 +02:00
|
|
|
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.id
|
|
|
|
|
2010-09-17 17:14:10 +02:00
|
|
|
class AlternativeTitle(models.Model):
|
2010-09-27 17:00:10 +02:00
|
|
|
book = models.ForeignKey(Book, related_name='alt_titles')
|
2010-09-17 17:14:10 +02:00
|
|
|
alt_title = models.CharField(max_length=511)
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.book.title+"="+self.alt_title
|
2010-09-17 17:14:10 +02:00
|
|
|
|
|
|
|
class Copy(models.Model):
|
2010-09-28 15:28:48 +02:00
|
|
|
book = models.ForeignKey(Book, related_name='copies')
|
2010-09-17 17:14:10 +02:00
|
|
|
number = models.IntegerField()
|
2010-09-17 19:24:29 +02:00
|
|
|
owner = models.CharField(max_length=9)
|
2011-03-04 16:35:33 +01:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.book.title+" "+str(self.number)+" "+self.owner
|
2010-09-17 17:14:10 +02:00
|
|
|
|
|
|
|
class Person(models.Model):
|
2010-09-17 19:24:29 +02:00
|
|
|
id = models.CharField(max_length=255, primary_key=True)
|
2010-09-17 17:14:10 +02:00
|
|
|
first_name = models.CharField(max_length=255)
|
|
|
|
last_name = models.CharField(max_length=255)
|
2010-09-27 17:00:10 +02:00
|
|
|
references = models.ManyToManyField(Reference, related_name='persons', null=True, blank=True)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.first_name + u' ' + self.last_name
|
2010-09-17 17:14:10 +02:00
|
|
|
|
2010-09-17 19:24:29 +02:00
|
|
|
class Relation(models.Model):
|
2010-09-27 22:44:57 +02:00
|
|
|
name = models.CharField(max_length=31, primary_key=True)
|
2010-09-17 19:24:29 +02:00
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-09-17 17:14:10 +02:00
|
|
|
class BookPerson(models.Model):
|
2010-09-27 22:44:57 +02:00
|
|
|
book = models.ForeignKey(Book, related_name='persons')
|
2010-09-27 17:00:10 +02:00
|
|
|
person = models.ForeignKey(Person, related_name='books')
|
2010-09-17 19:24:29 +02:00
|
|
|
relation = models.ForeignKey(Relation)
|
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.person.first_name +u' '+ self.person.last_name+ u' '+ self.relation.name+ u' of '+self.book.title
|
|
|
|
|
2010-09-17 19:24:29 +02:00
|
|
|
class Placement(models.Model):
|
|
|
|
category = models.ForeignKey(Category)
|
|
|
|
shelf = models.CharField(max_length=10)
|
2011-03-04 16:35:33 +01:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.category.name+" at "+self.shelf
|