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