2010-09-17 17:14:10 +02:00
|
|
|
from django.db import models
|
2011-03-05 21:47:58 +01:00
|
|
|
import web.library.fileformat
|
2010-09-17 17:14:10 +02:00
|
|
|
|
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-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)
|
2011-03-05 20:33:11 +01:00
|
|
|
picture = models.ImageField(upload_to='pictures', null=True, blank=True)
|
|
|
|
thumbnail = models.ImageField(upload_to='thumbnails', null=True, blank=True)
|
2011-03-05 18:35:37 +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 = ''
|
|
|
|
if commit:
|
2011-03-06 13:54:50 +01:00
|
|
|
# generate commit string by sending self.to_dict to fileformat function
|
2011-03-05 19:10:10 +01:00
|
|
|
print fileformat.write_action(self.to_dict())
|
2011-03-05 18:00:21 +01:00
|
|
|
|
|
|
|
else:
|
2011-03-06 20:31:02 +01:00
|
|
|
if self.getid():
|
|
|
|
scratch = 'book %s %s\n' % (self.isbn, self.getid())
|
|
|
|
else:
|
|
|
|
scratch = 'book %s\n' % self.isbn
|
|
|
|
scratch += 'Title: %s\n' % self.title
|
|
|
|
if self.subtitle:
|
|
|
|
scratch += 'Subtitle: %s\n' % self.subtitle
|
|
|
|
scratch += 'ISBN: %s\n' % self.isbn
|
|
|
|
scratch += 'Persons:\n'
|
|
|
|
for bp in self.persons.all():
|
|
|
|
scratch += ' %s %s %s (%s)\n' % (bp.person.id,
|
|
|
|
bp.person.first_name, bp.person.last_name,
|
|
|
|
bp.relation)
|
|
|
|
if len(self.persons.all()) == 0:
|
|
|
|
scratch += ' (no persons associated with this book)\n'
|
|
|
|
if self.series: scratch += 'Part of series: %s %s\n' % (self.series.id, self.series.title)
|
|
|
|
scratch += 'Category: %s\n' % self.category.id
|
|
|
|
if self.publisher: scratch += 'Publisher: %s\n' % self.publisher
|
|
|
|
if self.published_year: scratch += 'Published year: %s\n' % self.published_year
|
|
|
|
if self.edition: scratch += 'Edition: %s\n' % self.edition
|
|
|
|
if self.num_pages: scratch += 'Number of pages: %s\n' % self.num_pages
|
|
|
|
if self.description:
|
|
|
|
scratch += 'Description:\n%s\n' % '\n'.join(map(lambda line: ' '+line,
|
|
|
|
self.description.split('\n')))
|
2011-03-05 18:00:21 +01:00
|
|
|
|
|
|
|
return scratch
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
dict = {}
|
|
|
|
dict['action'] = 'edit-book'
|
2011-03-06 16:44:05 +01:00
|
|
|
|
|
|
|
dict['isbn'] = self.isbn
|
|
|
|
dict['title'] = self.title
|
|
|
|
dict['category'] = self.category.id
|
|
|
|
dict['subtitle'] = self.subtitle
|
|
|
|
dict['publisher'] = self.publisher
|
|
|
|
dict['published_year'] = self.published_year
|
|
|
|
dict['edition'] = self.edition
|
|
|
|
dict['num_pages'] = self.num_pages
|
|
|
|
if self.series:
|
|
|
|
dict['series'] = self.series.id
|
|
|
|
else:
|
|
|
|
dict['series'] = ''
|
|
|
|
dict['description'] = self.description
|
2011-03-05 18:00:21 +01:00
|
|
|
|
|
|
|
people = self.persons.all()
|
|
|
|
dict['persons'] = {}
|
2011-03-05 19:40:17 +01:00
|
|
|
for bookperson in people:
|
|
|
|
person = bookperson.person
|
|
|
|
if bookperson.relation.name in dict['persons']:
|
|
|
|
dict['persons'][bookperson.relation.name].append(person.id)
|
2011-03-05 18:00:21 +01:00
|
|
|
else:
|
2011-03-05 19:40:17 +01:00
|
|
|
dict['persons'][bookperson.relation.name] = [person.id]
|
2011-03-05 18:00:21 +01:00
|
|
|
|
2011-03-05 19:07:24 +01:00
|
|
|
dict['references'] = {}
|
|
|
|
for reference in self.references.all():
|
2011-03-05 19:40:17 +01:00
|
|
|
if reference.reference_type.name in dict['references']:
|
2011-03-05 19:07:24 +01:00
|
|
|
dict['references'][reference.reference_type.name].append(reference.text)
|
|
|
|
else:
|
|
|
|
dict['references'][reference.reference_type.name] = [reference.text]
|
2011-03-05 18:00:21 +01:00
|
|
|
|
|
|
|
#TODO: add thumbnail and image fields
|
|
|
|
|
|
|
|
return dict
|
|
|
|
|
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
def getid(self):
|
2011-03-06 20:31:02 +01:00
|
|
|
try:
|
|
|
|
if self.id:
|
|
|
|
return self.id.id
|
|
|
|
except Id.DoesNotExist:
|
|
|
|
pass
|
2011-03-04 14:37:00 +01:00
|
|
|
return None
|
2010-09-27 17:00:10 +02:00
|
|
|
|
|
|
|
def setid(self,newid):
|
2011-03-06 20:31:02 +01:00
|
|
|
try:
|
|
|
|
if self.id:
|
|
|
|
self.id.delete()
|
|
|
|
except Id.DoesNotExist:
|
|
|
|
pass
|
2011-03-04 14:37:00 +01:00
|
|
|
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
|
|
|
|
2011-03-06 18:41:44 +01:00
|
|
|
def get_persons(self, relation):
|
|
|
|
return map(lambda bp: bp.person,
|
|
|
|
filter(lambda bp: bp.relation.name == relation,
|
|
|
|
self.persons.all()))
|
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
# def print(self):
|
|
|
|
# print '%13s %5s %30s %'
|
|
|
|
|
|
|
|
# class Meta:
|
2011-03-06 18:51:16 +01:00
|
|
|
# order_with_respect_to = 'published_year'
|
2010-09-27 17:00:10 +02:00
|
|
|
# unique_together=(("isbn","id"),)
|
|
|
|
|
2011-03-05 18:35:37 +01:00
|
|
|
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
|
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
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)
|
|
|
|
|
2011-03-05 19:07:24 +01:00
|
|
|
def to_string(self, commit=False):
|
|
|
|
if commit:
|
|
|
|
#generate commit-string
|
2011-03-06 13:54:50 +01:00
|
|
|
print fileformat.write_action(self.to_dict())
|
2011-03-05 19:07:24 +01:00
|
|
|
else:
|
2011-03-06 20:31:02 +01:00
|
|
|
scratch = 'person %s\n' % self.id
|
|
|
|
scratch += 'Name: %s %s\n' % (self.first_name, self.last_name)
|
|
|
|
scratch += 'Books:\n'
|
|
|
|
for bp in self.books.all():
|
|
|
|
scratch += ' %s %s (%s)\n' % (bp.book.isbn, bp.book.title, bp.relation.name)
|
|
|
|
if len(self.books.all()) == 0:
|
|
|
|
scratch += ' (no books associated with this person)\n'
|
2011-03-05 19:07:24 +01:00
|
|
|
|
|
|
|
return scratch
|
|
|
|
|
2011-03-05 19:54:58 +01:00
|
|
|
def to_dict(self):
|
|
|
|
dict = {}
|
|
|
|
dict['action'] = 'edit-person'
|
|
|
|
dict['id'] = self.id
|
|
|
|
dict['first_name'] = self.first_name
|
|
|
|
dict['last_name'] = self.last_name
|
|
|
|
|
|
|
|
return dict
|
2011-03-05 19:07:24 +01:00
|
|
|
|
|
|
|
|
2010-09-27 17:00:10 +02:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.first_name + u' ' + self.last_name
|
2010-09-17 17:14:10 +02:00
|
|
|
|
2011-03-06 18:51:16 +01:00
|
|
|
# class Meta:
|
|
|
|
# order_with_respect_to = 'last_name'
|
|
|
|
|
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):
|
2011-03-06 18:14:25 +01:00
|
|
|
category = models.ForeignKey(Category, related_name='placement')
|
2010-09-17 19:24:29 +02:00
|
|
|
shelf = models.CharField(max_length=10)
|
2011-03-04 16:35:33 +01:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.category.name+" at "+self.shelf
|
2011-03-05 23:20:08 +01:00
|
|
|
|
|
|
|
def as_list(self):
|
|
|
|
shelf = self.shelf
|
|
|
|
return [self.category, shelf[0], int(shelf[1]), int(shelf[2])]
|
|
|
|
|
|
|
|
def get_shelf(self):
|
|
|
|
return self.shelf[0]
|
|
|
|
|
|
|
|
def get_row(self):
|
|
|
|
return int(self.shelf[2])
|
|
|
|
|
|
|
|
def get_column(self):
|
|
|
|
return int(self.shelf[1])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def shelf_as_list(shelfname):
|
|
|
|
placements = Placement.objects.filter(shelf__startswith=shelfname)
|
|
|
|
rows = max(map(lambda x: x.get_row(), placements))
|
|
|
|
columns = max(map(lambda x: x.get_column(), placements))
|
|
|
|
shelf_list = [ [[] for i in xrange(rows+1)] for j in xrange(columns+1)]
|
|
|
|
for placement in placements:
|
|
|
|
shelf_list[placement.get_column()][placement.get_row()].append(placement.category)
|
|
|
|
return shelf_list
|
|
|
|
|
2011-03-06 12:31:51 +01:00
|
|
|
@staticmethod
|
|
|
|
def shelf_as_list_row_then_col(shelfname):
|
|
|
|
placements = Placement.objects.filter(shelf__startswith=shelfname)
|
|
|
|
rows = max(map(lambda x: x.get_row(), placements))
|
|
|
|
columns = max(map(lambda x: x.get_column(), placements))
|
|
|
|
shelf_list = [ [[] for i in xrange(columns+1)] for j in xrange(rows+1)]
|
|
|
|
for placement in placements:
|
|
|
|
shelf_list[placement.get_row()][placement.get_column()].append(placement.category)
|
|
|
|
return shelf_list
|
|
|
|
|
2011-03-05 23:20:08 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_all_shelves():
|
|
|
|
shelves = set()
|
|
|
|
for item in Placement.objects.all():
|
|
|
|
shelves.add(item.get_shelf())
|
|
|
|
return list(shelves)
|
|
|
|
|
|
|
|
|