Merge branch 'master' into funding
[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 from django.conf import settings
14 from django.contrib.sites.models import Site
15 from django.utils import timezone
16
17
18 WL_DC_READER_XPATH = '(.|*)/rdf:RDF/rdf:Description/%s/text()' 
19 wl_dc_reader = metadata.MetadataReader(
20     fields={
21     'title':       ('textList', WL_DC_READER_XPATH % 'dc:title'),
22     'creator':     ('textList', WL_DC_READER_XPATH % 'dc:creator'),
23     'subject':     ('textList', (WL_DC_READER_XPATH + ' | ' + WL_DC_READER_XPATH + ' | ' + WL_DC_READER_XPATH) %
24                     ('dc:subject.period', 'dc:subject.type', 'dc:subject.genre')),
25     'description': ('textList', WL_DC_READER_XPATH % 'dc:description'),
26     'publisher':   ('textList', WL_DC_READER_XPATH % 'dc:publisher'),
27     'contributor': ('textList', (WL_DC_READER_XPATH + ' | ' + WL_DC_READER_XPATH + ' | ' + WL_DC_READER_XPATH) %
28                     ('dc:contributor.editor', 'dc:contributor.translator', 'dc:contributor.technical_editor')),
29     'date':        ('textList', WL_DC_READER_XPATH % 'dc:date'),
30     'type':        ('textList', WL_DC_READER_XPATH % 'dc:type'),
31     'format':      ('textList', WL_DC_READER_XPATH % 'dc:format'),
32     'identifier':  ('textList', WL_DC_READER_XPATH % 'dc:identifier.url'),
33     'source':      ('textList', WL_DC_READER_XPATH % 'dc:source'),
34     'language':    ('textList', WL_DC_READER_XPATH % 'dc:language'),
35     #'isPartOf':     ('textList', 'rdf:RDF/rdf:Description/dc:relation.isPartOf/text()'),
36     'hasPart':     ('textList', WL_DC_READER_XPATH % 'dc:relation.hasPart'),
37     #    'relation':    ('textList', 'rdf:RDF/rdf:Description/dc:relation/text()'),
38     #    'coverage':    ('textList', 'rdf:RDF/rdf:Description/dc:coverage/text()'),
39     'rights':      ('textList', WL_DC_READER_XPATH % 'dc:rights')
40     },
41     namespaces={
42     'dc': 'http://purl.org/dc/elements/1.1/',
43     'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
44     )
45
46
47 NS_DCTERMS = "http://purl.org/dc/terms/"
48
49
50 def nsdcterms(name):
51     return '{%s}%s' % (NS_DCTERMS, name)
52
53
54 class Catalogue(common.ResumptionOAIPMH):
55     TAG_CATEGORIES = ['author', 'epoch', 'kind', 'genre']
56
57     def __init__(self, metadata_registry):
58         super(Catalogue, self).__init__()
59         self.metadata_registry = metadata_registry
60
61         self.oai_id = "oai:" + Site.objects.get_current().domain + ":%s"
62
63         # earliest change
64         year_zero = timezone.make_aware(datetime(1990, 1, 1, 0, 0, 0), timezone.utc)
65
66         try:
67             earliest_change = \
68                 Book.objects.order_by('changed_at')[0].changed_at
69         except: earliest_change = year_zero
70
71         try:
72             earliest_delete = \
73                 Deleted.objects.exclude(slug__exact=u'').ordery_by('deleted_at')[0].deleted_at
74         except: earliest_delete = year_zero
75
76         self.earliest_datestamp = earliest_change <= earliest_delete and \
77             earliest_change or earliest_delete
78
79     def metadata(self, book):
80         try:
81             xml = etree.parse(book.xml_file)
82         finally:
83             book.xml_file.close()
84         md = wl_dc_reader(xml)
85         m = md.getMap()
86         if book.parent:
87             m['isPartOf'] = [str(WLURI.from_slug(book.parent.slug))]
88         return m
89
90     def record_for_book(self, book, headers_only=False):
91         meta = None
92         identifier = self.slug_to_identifier(book.slug)
93         if isinstance(book, Book):
94             #            setSpec = map(self.tag_to_setspec, book.tags.filter(category__in=self.TAG_CATEGORIES))
95             header = common.Header(identifier, book.changed_at, [], False)
96             if not headers_only:
97                 meta = common.Metadata(self.metadata(book))
98             about = None
99         elif isinstance(book, Deleted):
100             header = common.Header(identifier, book.deleted_at, [], True)
101             if not headers_only:
102                 meta = common.Metadata({})
103             about = None
104         if headers_only:
105             return header
106         return header, meta, about
107
108     def identify(self, **kw):
109         ident = common.Identify(
110             'Wolne Lektury',  # generate
111             '%s/oaipmh' % WL_BASE,  # generate
112             '2.0',  # version
113             [m[1] for m in settings.MANAGERS],  # adminEmails
114             self.earliest_datestamp,  # earliest datestamp of any change
115             'persistent',  # deletedRecord
116             'YYYY-MM-DDThh:mm:ssZ',  # granularity
117             ['identity'],  # compression
118             []  # descriptions
119             )
120         return ident
121
122     def books(self, tag, from_, until):
123         if tag:
124             # we do not support sets, since they are problematic for deleted books.
125             raise error.NoSetHierarchyError("Wolne Lektury does not support sets.")
126             # books = Book.tagged.with_all([tag])
127         else:
128             books = Book.objects.all()
129         deleted = Deleted.objects.exclude(slug__exact=u'')
130
131         books = books.order_by('changed_at')
132         deleted = deleted.order_by('deleted_at')
133         if from_:
134             books = books.filter(changed_at__gte=from_)
135             deleted = deleted.filter(deleted_at__gte=from_)
136         if until:
137             books = books.filter(changed_at__lte=until)
138             deleted = deleted.filter(deleted_at__lte=until)
139         return list(books) + list(deleted)
140
141     @staticmethod
142     def tag_to_setspec(tag):
143         return "%s:%s" % (tag.category, tag.slug)
144
145     @staticmethod
146     def setspec_to_tag(s):
147         if not s: return None
148         cs = s.split(':')
149         if len(cs) == 2:
150             if not cs[0] in Catalogue.TAG_CATEGORIES:
151                 raise error.NoSetHierarchyError("No category part in set")
152             tag = Tag.objects.get(slug=cs[1], category=cs[0])
153             return tag
154         raise error.NoSetHierarchyError("Setspec should have two components: category:slug")
155
156     def getRecord(self, **kw):
157         """
158 Returns (header, metadata, about) for given record.
159         """
160         slug = self.identifier_to_slug(kw['identifier'])
161         try:
162             book = Book.objects.get(slug=slug)
163             return self.record_for_book(book)
164         except Book.DoesNotExist:
165             book_type = ContentType.objects.get_for_model(Book)
166             try:
167                 deleted_book = Deleted.objects.get(content_type=book_type,
168                                                   slug=slug)
169             except:
170                 raise error.IdDoesNotExistError("No item for this identifier")
171             return self.record_for_book(deleted_book)
172
173     def validate_kw(self, kw):
174         if 'resumptionToken' in kw:
175             raise error.BadResumptionTokenError("No resumption token support at this point")
176         if 'metadataPrefix' in kw and not self.metadata_registry.hasWriter(kw['metadataPrefix']):
177             raise error.CannotDisseminateFormatError("This format is not supported")
178
179     def identifier_to_slug(self, ident):
180         return ident.split(':')[-1]
181
182     def slug_to_identifier(self, slug):
183         return self.oai_id % slug
184
185     def listIdentifiers(self, **kw):
186         self.validate_kw(kw)
187         records = [self.record_for_book(book, headers_only=True) for
188                    book in self.books(None,
189                            kw.get('from_', None),
190                            kw.get('until', None))]
191         return records, None
192
193     def listRecords(self, **kw):
194         """
195 can get a resumptionToken kw.
196 returns result, token
197         """
198         self.validate_kw(kw)
199         records = [self.record_for_book(book) for
200                    book in self.books(None,
201                            kw.get('from_', None),
202                            kw.get('until', None))]
203
204         return records, None
205
206     def listMetadataFormats(self, **kw):
207         formats = [
208             ('oai_dc',
209              'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
210              server.NS_OAIDC),
211             ('qdc',
212              'http://dublincore.org/schemas/xmls/qdc/2006/01/06/dcterms.xsd',
213              NS_DCTERMS)]
214         if 'identifier' in kw:
215             slug = self.identifier_to_slug(kw['identifier'])
216             try:
217                 b = Book.objects.get(slug=slug)
218                 return formats
219             except:
220                 try:
221                     d = Deleted.objects.get(slug=slug)
222                     return []
223                 except:
224                     raise error.IdDoesNotExistError("This id does not exist")
225         else:
226             return formats
227
228     def listSets(self, **kw):
229         raise error.NoSetHierarchyError("Wolne Lektury does not support sets.")
230         # tags = []
231         # for category in Catalogue.TAG_CATEGORIES:
232         #     for tag in Tag.objects.filter(category=category):
233         #         tags.append(("%s:%s" % (tag.category, tag.slug),
234         #                      tag.name,
235         #                      tag.description))
236         # return tags, None