Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0
This repository has been archived on 2024-07-04. You can view files and clone it, but cannot push or open issues or pull requests.
worblehat-old/python/web/library/models.py

196 lines
5.7 KiB
Python

from django.db import models
import fileformat
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)
name = models.CharField(max_length=255)
def __unicode__(self):
return self.name
class BookSeries(models.Model):
id = models.CharField(max_length=20, primary_key=True)
title = models.CharField(max_length=511)
def __unicode__(self):
return self.title
class ReferenceType(models.Model):
name = models.CharField(max_length=31)
def __unicode__(self):
return self.name
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, related_name='books', 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',null=True, blank=True)
#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()
print fileformat.write_action(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 bookperson in people:
person = bookperson.person
if bookperson.relation.name in dict['persons']:
dict['persons'][bookperson.relation.name].append(person.id)
else:
dict['persons'][bookperson.relation.name] = [person.id]
dict['references'] = {}
for reference in self.references.all():
if reference.reference_type.name in dict['references']:
dict['references'][reference.reference_type.name].append(reference.text)
else:
dict['references'][reference.reference_type.name] = [reference.text]
#TODO: add thumbnail and image fields
return dict
def getid(self):
if self.id:
return self.id.id
return None
def setid(self,newid):
if self.id:
self.id.delete()
newid = Id(book=self,id=newid)
newid.save()
def get_authors(self):
people = self.persons.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 Reference(models.Model):
book = models.ForeignKey(Book, related_name='references')
reference_type = models.ForeignKey(ReferenceType)
text = models.CharField(max_length=1023)
def __unicode__(self):
return self.text
class Id(models.Model):
id = models.CharField(max_length=511, primary_key=True)
book = models.OneToOneField(Book)
def __unicode__(self):
return self.id
class AlternativeTitle(models.Model):
book = models.ForeignKey(Book, related_name='alt_titles')
alt_title = models.CharField(max_length=511)
def __unicode__(self):
return self.book.title+"="+self.alt_title
class Copy(models.Model):
book = models.ForeignKey(Book, related_name='copies')
number = models.IntegerField()
owner = models.CharField(max_length=9)
def __unicode__(self):
return self.book.title+" "+str(self.number)+" "+self.owner
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 to_string(self, commit=False):
if commit:
#generate commit-string
print ''
else:
scratch = 'Name'.ljust(15, ' ') + ':'
scratch += ' '.join((first_name,last_name))
return scratch
def __unicode__(self):
return self.first_name + u' ' + self.last_name
class Relation(models.Model):
name = models.CharField(max_length=31, primary_key=True)
def __unicode__(self):
return self.name
class BookPerson(models.Model):
book = models.ForeignKey(Book, related_name='persons')
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)
def __unicode__(self):
return self.category.name+" at "+self.shelf