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