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