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