pyceo/ceo/library.py

123 lines
3.1 KiB
Python
Raw Normal View History

2009-01-10 19:41:09 -05:00
from sqlobject import *
from sqlobject.sqlbuilder import *
2008-06-02 23:43:13 -04:00
from ceo import conf
from ceo import members
from ceo import terms
2009-01-10 19:41:09 -05:00
import time
from datetime import datetime, timedelta
2008-06-02 23:43:13 -04:00
2009-01-10 19:42:10 -05:00
CONFIG_FILE = "/etc/csc/library.cf"
2008-06-02 23:43:13 -04:00
cfg = {}
def configure():
2009-01-14 18:57:31 -05:00
"""
Load configuration
2009-01-10 19:41:09 -05:00
"""
cfg_fields = [ "library_connect_string" ]
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
temp_cfg = conf.read(CONFIG_FILE)
conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg)
cfg.update(temp_cfg)
2009-01-10 19:41:09 -05:00
sqlhub.processConnection = connectionForURI(cfg["library_connect_string"])
class Book(SQLObject):
2009-01-14 18:57:31 -05:00
"""
A book. This does all the stuff we could
ever want to do with a book.
"""
2009-01-10 19:41:09 -05:00
isbn = StringCol()
title = StringCol()
year = StringCol()
publisher = StringCol()
authors = SQLRelatedJoin("Author")
signouts = SQLMultipleJoin("Signout")
def sign_out(self, u):
2009-01-14 18:57:31 -05:00
"""
Call this with a username to sign out
a book.
"""
2009-01-15 19:18:04 -05:00
if members.registered(u, terms.current()):
s = Signout(username=u, book=self,
outdate=datetime.today(), indate=None)
2009-01-10 19:41:09 -05:00
def sign_in(self, u):
2009-01-14 18:57:31 -05:00
"""
Call this to check a book back in to
the library. Username is used to
disambiguate in case more than one
copy of this book has been signed out.
"""
2009-01-10 19:41:09 -05:00
s = self.signouts.filter(AND(Signout.q.indate==None, Signout.q.username==u))
if s.count() > 0:
2009-01-15 23:28:13 -05:00
list(s.orderBy(Signout.q.outdate).limit(1))[0].sign_in()
2009-01-10 19:41:09 -05:00
return True
else:
raise Exception("PEBKAC: Book not signed out!")
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
def __str__(self):
2009-01-14 18:57:31 -05:00
"""
Magic drugs to make books display
nicely.
"""
2009-01-10 19:41:09 -05:00
book = "%s [%s]" % (self.title, self.year)
book += "\nBy: "
for a in self.authors:
book += a.name
book += ", "
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
if self.authors.count() < 1:
book += "(unknown)"
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
book = book.strip(", ")
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
signouts = self.signouts.filter(Signout.q.indate==None)
if signouts.count() > 0:
book += "\nSigned Out: "
for s in signouts:
2009-01-28 01:07:58 -05:00
book += s.username + " (" + str(s.due_date) + "), "
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
book = book.strip(", ")
return book
2008-06-02 11:32:33 -04:00
2009-01-10 19:41:09 -05:00
class Author(SQLObject):
2009-01-14 18:57:31 -05:00
"""
An author can author many books, and a book
can have many authors. This lets us map
both ways.
"""
2009-01-10 19:41:09 -05:00
name = StringCol()
books = RelatedJoin("Book")
class Signout(SQLObject):
2009-01-14 18:57:31 -05:00
"""
An instance of a signout associates usernames,
books, signout dates, and return dates to mark
that a book has been signed out by a particular
user.
"""
2009-01-10 19:41:09 -05:00
username = StringCol()
book = ForeignKey("Book")
outdate = DateCol()
indate = DateCol()
def sign_in(self):
2009-01-14 18:57:31 -05:00
"""
Terminate the signout (return the book).
"""
2009-01-10 19:41:09 -05:00
self.indate = datetime.today()
def _get_due_date(self):
"""
Compute the due date of the book based on the sign-out
date.
"""
return self.outdate + timedelta(weeks=2)
if __name__ == "__main__":
print "This functionality isn't implemented yet."