1 # -*- encoding: utf-8 -*-
3 __author__= "Ćukasz Rekucki"
4 __date__ = "$2009-09-25 15:49:50$"
5 __doc__ = "Module documentation."
7 from piston.handler import BaseHandler, AnonymousBaseHandler
12 import api.forms as forms
13 from datetime import date
15 from django.core.urlresolvers import reverse
17 from wlrepo import MercurialLibrary, RevisionNotFound, DocumentAlreadyExists
18 from librarian import dcparser
20 import api.response as response
21 from api.response import validate_form
24 # Document List Handlers
26 class BasicLibraryHandler(AnonymousBaseHandler):
27 allowed_methods = ('GET',)
29 def read(self, request):
30 """Return the list of documents."""
31 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
34 'url': reverse('document_view', args=[docid]),
35 'name': docid } for docid in lib.documents() ]
37 return {'documents' : document_list}
40 class LibraryHandler(BaseHandler):
41 allowed_methods = ('GET', 'POST')
42 anonymous = BasicLibraryHandler
44 def read(self, request):
45 """Return the list of documents."""
46 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
49 'url': reverse('document_view', args=[docid]),
50 'name': docid } for docid in lib.documents() ]
52 return {'documents' : document_list }
54 @validate_form(forms.DocumentUploadForm, 'POST')
55 def create(self, request, form):
56 """Create a new document."""
57 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
59 if form.cleaned_data['ocr_data']:
60 data = form.cleaned_data['ocr_data'].encode('utf-8')
62 data = request.FILES['ocr'].read().decode('utf-8')
64 if form.cleaned_data['generate_dc']:
65 data = librarian.wrap_text(data, unicode(date.today()))
67 docid = form.cleaned_data['bookname']
69 doc = lib.document_create(docid)
70 doc.quickwrite('xml', data, '$AUTO$ XML data uploaded.',
71 user=request.user.username)
73 url = reverse('document_view', args=[doc.id])
76 return response.EntityCreated().django_response(\
80 'revision': doc.revision },
83 except DocumentAlreadyExists:
84 # Document is already there
85 return response.EntityConflict().django_response(\
86 {"reason": "Document %s already exists." % docid})
91 class BasicDocumentHandler(AnonymousBaseHandler):
92 allowed_methods = ('GET',)
94 def read(self, request, docid):
96 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
97 doc = lib.document(docid)
98 except RevisionNotFound:
103 'text_url': reverse('doctext_view', args=[doc.id]),
104 'dc_url': reverse('docdc_view', docid=doc.id),
105 'public_revision': doc.revision,
113 class DocumentHandler(BaseHandler):
114 allowed_methods = ('GET', 'PUT')
115 anonymous = BasicDocumentHandler
117 def read(self, request, docid):
118 """Read document's meta data"""
119 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
122 doc = lib.document(docid)
123 udoc = doc.take(request.user.username)
124 except RevisionNotFound:
125 return request.EnityNotFound().django_response()
127 # is_shared = udoc.ancestorof(doc)
128 # is_uptodate = is_shared or shared.ancestorof(document)
132 'text_url': reverse('doctext_view', args=[udoc.id]),
133 'dc_url': reverse('docdc_view', args=[udoc.id]),
134 'parts_url': reverse('docparts_view', args=[udoc.id]),
135 'user_revision': udoc.revision,
136 'public_revision': doc.revision,
141 def update(self, request, docid):
142 """Update information about the document, like display not"""
148 class DocumentTextHandler(BaseHandler):
149 allowed_methods = ('GET', 'PUT')
151 @validate_form(forms.DocumentEntryRequest)
152 def read(self, request, form, docid):
153 """Read document as raw text"""
154 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
157 if request.GET['revision'] == 'latest':
158 document = lib.document(docid)
160 document = lib.document_for_rev(request.GET['revision'])
162 # TODO: some finer-grained access control
163 return document.data('xml')
164 except RevisionNotFound:
165 return response.EntityNotFound().django_response()
167 def update(self, request, docid):
168 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
170 data = request.PUT['contents']
171 prev = request.PUT['revision']
173 if request.PUT.has_key('message'):
174 msg = u"$USER$ " + request.PUT['message']
176 msg = u"$AUTO$ XML content update."
178 current = lib.document(docid, request.user.username)
179 orig = lib.document_for_rev(prev)
182 return response.EntityConflict().django_response({
183 "reason": "out-of-date",
184 "provided_revision": orig.revision,
185 "latest_revision": current.revision })
187 ndoc = doc.quickwrite('xml', data, msg)
189 # return the new revision number
193 "previous_revision": prev,
194 "updated_revision": ndoc.revision
197 except (RevisionNotFound, KeyError):
198 return response.EntityNotFound().django_response()
201 # Dublin Core handlers
203 # @requires librarian
205 class DocumentDublinCoreHandler(BaseHandler):
206 allowed_methods = ('GET', 'PUT')
208 @validate_form(forms.DocumentEntryRequest)
209 def read(self, request, docid):
210 """Read document as raw text"""
211 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
213 if request.GET['revision'] == 'latest':
214 document = lib.document(docid)
216 document = lib.document_for_rev(request.GET['revision'])
218 bookinfo = dcparser.BookInfo.from_string(doc.data('xml'))
219 return bookinfo.serialize()
220 except RevisionNotFound:
221 return response.EntityNotFound().django_response()
223 def update(self, request, docid):
224 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
226 bi_json = request.PUT['contents']
227 prev = request.PUT['revision']
228 if request.PUT.has_key('message'):
229 msg = u"$USER$ " + request.PUT['message']
231 msg = u"$AUTO$ Dublin core update."
233 current = lib.document(docid, request.user.username)
234 orig = lib.document_for_rev(prev)
237 return response.EntityConflict().django_response({
238 "reason": "out-of-date",
239 "provided": orig.revision,
240 "latest": current.revision })
242 xmldoc = parser.WLDocument.from_string(current.data('xml'))
243 document.book_info = dcparser.BookInfo.from_json(bi_json)
246 ndoc = current.quickwrite('xml', \
247 document.serialize().encode('utf-8'),\
248 message=msg, user=request.user.username)
253 "previous_revision": prev,
254 "updated_revision": ndoc.revision
256 except (RevisionNotFound, KeyError):
257 return response.EntityNotFound().django_response()