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