2 from oaipmh import server, common
3 from catalogue.models import Book, Tag
4 from api.models import Deleted
5 from librarian.dcparser import BookInfo
6 from django.contrib.contenttypes.models import ContentType
9 class Catalogue(common.ResumptionOAIPMH):
11 super(Catalogue, self).__init__()
13 def metadata(self, book):
14 bi = BookInfo.from_file(book.xml_file)
16 for field in bi.FIELDS:
17 dc_field = field.uri.split('}')[1]
18 value = getattr(bi, dc_field.name)
19 if isinstance(value,list):
20 value = ';'.join(map(unicode, value))
22 value = unicode(value)
23 meta["dc:"+dc_field] = value
26 def record_for_book(self, book):
27 header = common.Header(book.slug, book.changed_at, [], False)
28 meta = common.Metadata(self.metadata(book))
30 return header, meta, about
32 def getRecord(self, record, **kw):
34 Returns (header, metadata, about) for given record.
38 book = Book.objects.get(slug=slug)
39 return self.record_for_book(book)
40 except Book.DoesNotExist, e:
41 book_type = ContentType.objects.get_for_model(Book)
42 deleted_book = Deleted.objects.filter(content_type=book_type,
44 header = common.Header(deleted_book.slug,
45 deleted_book.deleted_at,
47 meta = common.Metadata({})
48 return header, meta, None # None for about.
52 def listRecords(self, **kw):
54 can get a resumptionToken kw.
57 return [self.record_for_book(book) for book in Book.objects.all()]