70e78c5fbc485ea07e3162572334402ac7e53423
[wolnelektury.git] / apps / oai / handlers.py
1 from oaipmh import server, common, metadata, error
2 from catalogue.models import Book, Tag
3 from api.models import Deleted
4 from api.handlers import WL_BASE
5 from librarian.dcparser import BookInfo
6 from librarian import WLURI
7 from django.contrib.contenttypes.models import ContentType
8 from django.contrib.auth.models import User
9 from datetime import datetime
10 from lxml import etree
11 from lxml.etree import ElementTree
12 from django.db.models import Q
13
14
15 wl_dc_reader = metadata.MetadataReader(
16     fields={
17     'title':       ('textList', 'rdf:RDF/rdf:Description/dc:title/text()'),
18     'creator':     ('textList', 'rdf:RDF/rdf:Description/dc:creator/text()'),
19     'subject':     ('textList', 'rdf:RDF/rdf:Description/dc:subject.period/text() | rdf:RDF/rdf:Description/dc:subject.type/text() | rdf:RDF/rdf:Description/dc:subject.genre/text()'),
20     'description': ('textList', 'rdf:RDF/rdf:Description/dc:description/text()'),
21     'publisher':   ('textList', 'rdf:RDF/rdf:Description/dc:publisher/text()'),
22     'contributor': ('textList', 'rdf:RDF/rdf:Description/dc:contributor.editor/text() | rdf:RDF/rdf:Description/dc:contributor.translator/text() | rdf:RDF/rdf:Description/dc:contributor.technical_editor/text()'),
23     'date':        ('textList', 'rdf:RDF/rdf:Description/dc:date/text()'),
24     'type':        ('textList', 'rdf:RDF/rdf:Description/dc:type/text()'),
25     'format':      ('textList', 'rdf:RDF/rdf:Description/dc:format/text()'),
26     'identifier':  ('textList', 'rdf:RDF/rdf:Description/dc:identifier.url/text()'),
27     'source':      ('textList', 'rdf:RDF/rdf:Description/dc:source/text()'),
28     'language':    ('textList', 'rdf:RDF/rdf:Description/dc:language/text()'),
29     #    'relation':    ('textList', 'rdf:RDF/rdf:Description/dc:relation/text()'),
30     #    'coverage':    ('textList', 'rdf:RDF/rdf:Description/dc:coverage/text()'),
31     'rights':      ('textList', 'rdf:RDF/rdf:Description/dc:rights/text()')
32     },
33     namespaces={
34     'dc': 'http://purl.org/dc/elements/1.1/',
35     'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
36     )
37
38
39 class Catalogue(common.ResumptionOAIPMH):
40     TAG_CATEGORIES = ['author', 'epoch', 'kind', 'genre']
41     
42     def __init__(self):
43         super(Catalogue, self).__init__()
44
45         # earliest change
46         year_zero = datetime(1990, 1, 1, 0, 0, 0)
47
48         try:
49             earliest_change = \
50                 Book.objects.order_by('changed_at')[0].changed_at
51         except: earliest_change = year_zero
52
53         try:
54             earliest_delete = \
55                 Deleted.objects.ordery_by('deleted_at')[0].deleted_at
56         except: earliest_delete = year_zero
57
58         self.earliest_datestamp = earliest_change <= earliest_delete and \
59             earliest_change or earliest_delete
60
61         # admins
62         self.admin_emails = [u.email for u in User.objects.filter(is_superuser=True).exclude(email__exact=u'')]
63
64     def metadata(self, book):
65         xml = etree.parse(book.xml_file)
66         md = wl_dc_reader(xml)
67         return md.getMap()
68
69     def record_for_book(self, book, headers_only=False):
70         meta = None
71         identifier = str(WLURI.from_slug(book.slug))
72         if isinstance(book, Book):
73             #            setSpec = map(self.tag_to_setspec, book.tags.filter(category__in=self.TAG_CATEGORIES))
74             header = common.Header(identifier, book.changed_at, [], False)
75             if not headers_only:
76                 meta = common.Metadata(self.metadata(book))
77             about = None
78         elif isinstance(book, Deleted):
79             header = common.Header(identifier, book.deleted_at, [], True)
80             if not headers_only:
81                 meta = common.Metadata({})
82             about = None
83         if headers_only:
84             return header
85         return header, meta, about
86
87     def identify(self, **kw):
88         ident = common.Identify(
89             'Wolne Lektury',  # generate
90             '%s/oaipmh' % WL_BASE,  # generate
91             '2.0',  # version
92             self.admin_emails,  # adminEmails
93             self.earliest_datestamp,  # earliest datestamp of any change
94             'persistent',  # deletedRecord
95             'YYYY-MM-DDThh:mm:ssZ',  # granularity
96             ['identity'],  # compression
97             []  # descriptions
98             )
99         return ident
100
101     def books(self, tag, from_, until):
102         if tag:
103             # we do not support sets, since they are problematic for deleted books.
104             raise errror.NoSetHierarchyError("Wolne Lektury does not support sets.")
105             # books = Book.tagged.with_all([tag])
106         else:
107             books = Book.objects.all()
108         deleted = Deleted.objects.filter(slug__isnull=False)
109
110         books = books.order_by('changed_at')
111         deleted = deleted.order_by('deleted_at')
112         if from_:
113             books = books.filter(changed_at__gte=from_)
114             deleted = deleted.filter(deleted_at__gte=from_)
115         if until:
116             books = books.filter(changed_at__lte=until)
117             deleted = deleted.filter(deleted_at__lte=until)
118         return list(books) + list(deleted)
119
120     @staticmethod
121     def tag_to_setspec(tag):
122         return "%s:%s" % (tag.category, tag.slug)
123
124     @staticmethod
125     def setspec_to_tag(s):
126         if not s: return None
127         cs = s.split(':')
128         if len(cs) == 2:
129             if not cs[0] in Catalogue.TAG_CATEGORIES:
130                 raise error.NoSetHierarchyError("No category part in set")
131             tag = Tag.objects.get(slug=cs[1], category=cs[0])
132             return tag
133         raise error.NoSetHierarchyError("Setspec should have two components: category:slug")
134
135     def getRecord(self, **kw):
136         """
137 Returns (header, metadata, about) for given record.
138         """
139         slug = WLURI(kw['identifier']).slug
140         try:
141             book = Book.objects.get(slug=slug)
142             return self.record_for_book(book)
143         except Book.DoesNotExist:
144             book_type = ContentType.objects.get_for_model(Book)
145             try:
146                 deleted_book = Deleted.objects.get(content_type=book_type,
147                                                   slug=slug)
148             except:
149                 raise error.IdDoesNotExistError("No item for this identifier")
150             return self.record_for_book(deleted_book)
151
152     def listIdentifiers(self, **kw):
153         records = [self.record_for_book(book, headers_only=True) for
154                    book in self.books(None,
155                            kw.get('from_', None),
156                            kw.get('until', None))]
157         return records, None
158
159     def listRecords(self, **kw):
160         """
161 can get a resumptionToken kw.
162 returns result, token
163         """
164         records = [self.record_for_book(book) for
165                    book in self.books(None,
166                            kw.get('from_', None),
167                            kw.get('until', None))]
168
169         return records, None
170
171     def listMetadataFormats(self, **kw):
172         return [('oai_dc',
173                  'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
174                  server.NS_OAIDC)]
175
176     def listSets(self, **kw):
177         raise error.NoSetHierarchyError("Wolne Lektury does not support sets.")
178         # tags = []
179         # for category in Catalogue.TAG_CATEGORIES:
180         #     for tag in Tag.objects.filter(category=category):
181         #         tags.append(("%s:%s" % (tag.category, tag.slug),
182         #                      tag.name,
183         #                      tag.description))
184         # return tags, None
185
186