from django.db import models book_fields = {'isbn' : 'ISBN', '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'} class Category(models.Model): id = models.CharField(max_length=255, primary_key=True) name = models.CharField(max_length=255) class BookSeries(models.Model): id = models.CharField(max_length=20, primary_key=True) title = models.CharField(max_length=511) class ReferenceType(models.Model): name = models.CharField(max_length=31) class Reference(models.Model): reference_type = models.ForeignKey(ReferenceType) text = models.CharField(max_length=1023) class Book(models.Model): isbn = models.CharField(max_length=13, primary_key=True) # 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) 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) series = models.ForeignKey(BookSeries, null=True, blank=True) 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) references = models.ManyToManyField(Reference, related_name='books') 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') def getid(self): try: return self.id.id except Id.DoesNotExist: return None def setid(self,newid): try: id = self.id id.delete() newid = Id(book=self,id=newid) newid.save() except Id.DoesNotExist: id=Id(id=newid,book=self) id.save() # return Book.get(isbn=self.isbn) def get_authors(self): people = self.person.all() authors = [] for person in people: if person.relation.name == 'Author': authors.append(person.person) return authors 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) class AlternativeTitle(models.Model): book = models.ForeignKey(Book, related_name='alt_titles') alt_title = models.CharField(max_length=511) class Copy(models.Model): book = models.ForeignKey(Book) number = models.IntegerField() owner = models.CharField(max_length=9) class Person(models.Model): id = models.CharField(max_length=255, primary_key=True) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) references = models.ManyToManyField(Reference, related_name='persons', null=True, blank=True) def __unicode__(self): return self.first_name + u' ' + self.last_name class Relation(models.Model): name = models.CharField(max_length=31) def __unicode__(self): return self.name class BookPerson(models.Model): book = models.ForeignKey(Book, related_name='person') person = models.ForeignKey(Person, related_name='books') relation = models.ForeignKey(Relation) def __unicode__(self): return self.person.first_name +u' '+ self.person.last_name+ u' '+ self.relation.name+ u' of '+self.book.title class Placement(models.Model): category = models.ForeignKey(Category) shelf = models.CharField(max_length=10)