From 3c5fe5b298287f92a9c6a8e485c3860db36931fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Rekucki?= Date: Sat, 26 Sep 2009 02:45:10 +0200 Subject: [PATCH] More changes to the REST API. --- apps/api/forms.py | 24 +++- apps/api/handlers/library_handlers.py | 147 ++++++++++++++--------- apps/api/response.py | 119 ++++++++++++++++++ lib/wlrepo/__init__.py | 3 + lib/wlrepo/mercurial_backend/document.py | 3 +- lib/wlrepo/mercurial_backend/library.py | 12 +- 6 files changed, 243 insertions(+), 65 deletions(-) create mode 100644 apps/api/response.py diff --git a/apps/api/forms.py b/apps/api/forms.py index 2b09e178..7c6f2c22 100644 --- a/apps/api/forms.py +++ b/apps/api/forms.py @@ -7,14 +7,28 @@ __doc__ = "Micro-forms for the API." from django import forms -class DocumentGetForm(forms.Form): - autocabinet = forms.BooleanField(required=False) - +class DocumentEntryRequest(forms.Form): + revision = forms.RegexField(regex='latest|[0-9a-f]{40}') class DocumentUploadForm(forms.Form): - ocr = forms.FileField(label='Source OCR file') + ocr_file = forms.FileField(label='Source OCR file', required=False) + ocr_data = forms.CharField(widget=forms.HiddenInput(), required=False) + bookname = forms.RegexField(regex=r'[0-9\.\w_-]+', \ label='Publication name', help_text='Example: slowacki-beniowski') - generate_dc = forms.BooleanField(required=False, initial=True, label=u"Generate DublinCore template") + generate_dc = forms.BooleanField(required=False, \ + initial=True, label=u"Generate DublinCore template") + + + def clean(self): + clean_data = self.cleaned_data + + ocr_file = clean_data['ocr_file'] + ocr_data = clean_data['ocr_data'] + + if not ocr_file and not ocr_data: + raise forms.ValidationError( + "You must either provide file descriptor or raw data." ) + return clean_data \ No newline at end of file diff --git a/apps/api/handlers/library_handlers.py b/apps/api/handlers/library_handlers.py index b47d41cc..45dff83b 100644 --- a/apps/api/handlers/library_handlers.py +++ b/apps/api/handlers/library_handlers.py @@ -5,7 +5,7 @@ __date__ = "$2009-09-25 15:49:50$" __doc__ = "Module documentation." from piston.handler import BaseHandler, AnonymousBaseHandler -from piston.utils import rc + import settings import librarian @@ -13,10 +13,13 @@ import api.forms as forms from datetime import date from django.core.urlresolvers import reverse -from wlrepo import MercurialLibrary, RevisionNotFound +from wlrepo import MercurialLibrary, RevisionNotFound, DocumentAlreadyExists from librarian import dcparser +import api.response as response +from api.response import validate_form + # # Document List Handlers # @@ -47,29 +50,39 @@ class LibraryHandler(BaseHandler): return {'documents' : document_list } - def create(self, request): + @validate_form(forms.DocumentUploadForm, 'POST') + def create(self, request, form): """Create a new document.""" lib = MercurialLibrary(path=settings.REPOSITORY_PATH) - form = forms.DocumentUploadForm(request.POST, request.FILES) - if not form.is_valid(): - return rc.BAD_REQUEST - - f = request.FILES['ocr'] - data = f.read().decode('utf-8') + if form.cleaned_data['ocr_data']: + data = form.cleaned_data['ocr_data'].encode('utf-8') + else: + data = request.FILES['ocr'].read().decode('utf-8') if form.cleaned_data['generate_dc']: data = librarian.wrap_text(data, unicode(date.today())) - # TODO: what if the file exists ? - doc = lib.document_create(form.cleaned_data['bookname']) - doc.quickwrite('xml', data, '$AUTO$ XML data uploaded.', - user=request.user.username) + docid = form.cleaned_data['bookname'] + try: + doc = lib.document_create(docid) + doc.quickwrite('xml', data, '$AUTO$ XML data uploaded.', + user=request.user.username) + + url = reverse('document_view', args=[doc.id]) - return { - 'url': reverse('document_view', args=[doc.id]), - 'name': doc.id, - 'revision': doc.revision } + + return response.EntityCreated().django_response(\ + body = { + 'url': url, + 'name': doc.id, + 'revision': doc.revision }, + url = url ) + + except DocumentAlreadyExists: + # Document is already there + return response.EntityConflict().django_response(\ + {"reason": "Document %s already exists." % docid}) # # Document Handlers @@ -78,19 +91,17 @@ class BasicDocumentHandler(AnonymousBaseHandler): allowed_methods = ('GET',) def read(self, request, docid): - lib = MercurialLibrary(path=settings.REPOSITORY_PATH) - - opts = forms.DocumentGetForm(request.GET) - if not opts.is_valid(): - return rc.BAD_REQUEST - - doc = lib.document(docid) + try: + lib = MercurialLibrary(path=settings.REPOSITORY_PATH) + doc = lib.document(docid) + except RevisionNotFound: + return rc.NOT_FOUND result = { 'name': doc.id, 'text_url': reverse('doctext_view', args=[doc.id]), 'dc_url': reverse('docdc_view', docid=doc.id), - 'latest_rev': doc.revision, + 'public_revision': doc.revision, } return result @@ -105,16 +116,12 @@ class DocumentHandler(BaseHandler): def read(self, request, docid): """Read document's meta data""" lib = MercurialLibrary(path=settings.REPOSITORY_PATH) - - opts = forms.DocumentGetForm(request.GET) - if not opts.is_valid(): - return rc.BAD_REQUEST - + try: doc = lib.document(docid) udoc = doc.take(request.user.username) except RevisionNotFound: - return rc.NOT_HERE + return request.EnityNotFound().django_response() # is_shared = udoc.ancestorof(doc) # is_uptodate = is_shared or shared.ancestorof(document) @@ -124,14 +131,9 @@ class DocumentHandler(BaseHandler): 'text_url': reverse('doctext_view', args=[udoc.id]), 'dc_url': reverse('docdc_view', args=[udoc.id]), 'parts_url': reverse('docparts_view', args=[udoc.id]), - 'latest_rev': udoc.revision, - 'latest_shared_rev': doc.revision, - # 'shared': is_shared, - # 'up_to_date': is_uptodate, - } - - #if request.GET.get('with_part', 'no') == 'yes': - # result['parts'] = document.parts() + 'user_revision': udoc.revision, + 'public_revision': doc.revision, + } return result @@ -141,13 +143,21 @@ class DocumentHandler(BaseHandler): class DocumentTextHandler(BaseHandler): allowed_methods = ('GET', 'PUT') - def read(self, request, docid): - """Read document as raw text""" + @validate_form(forms.DocumentEntryRequest) + def read(self, request, form, docid): + """Read document as raw text""" lib = MercurialLibrary(path=settings.REPOSITORY_PATH) + try: - return lib.document(docid, request.user.username).data('xml') + if request.GET['revision'] == 'latest': + document = lib.document(docid) + else: + document = lib.document_for_rev(request.GET['revision']) + + # TODO: some finer-grained access control + return document.data('xml') except RevisionNotFound: - return rc.NOT_HERE + return response.EntityNotFound().django_response() def update(self, request, docid): lib = MercurialLibrary(path=settings.REPOSITORY_PATH) @@ -164,13 +174,23 @@ class DocumentTextHandler(BaseHandler): orig = lib.document_for_rev(prev) if current != orig: - return rc.DUPLICATE_ENTRY - - doc.quickwrite('xml', data, msg) - - return rc.ALL_OK + return response.EntityConflict().django_response({ + "reason": "out-of-date", + "provided_revision": orig.revision, + "latest_revision": current.revision }) + + ndoc = doc.quickwrite('xml', data, msg) + + # return the new revision number + return { + "document": ndoc.id, + "subview": "xml", + "previous_revision": prev, + "updated_revision": ndoc.revision + } + except (RevisionNotFound, KeyError): - return rc.NOT_HERE + return response.EntityNotFound().django_response() # # Dublin Core handlers @@ -180,16 +200,20 @@ class DocumentTextHandler(BaseHandler): class DocumentDublinCoreHandler(BaseHandler): allowed_methods = ('GET', 'PUT') + @validate_form(forms.DocumentEntryRequest) def read(self, request, docid): """Read document as raw text""" lib = MercurialLibrary(path=settings.REPOSITORY_PATH) try: - doc = lib.document(docid, request.user.username).data('xml') - bookinfo = dcparser.BookInfo.from_string(doc.read()) - + if request.GET['revision'] == 'latest': + document = lib.document(docid) + else: + document = lib.document_for_rev(request.GET['revision']) + + bookinfo = dcparser.BookInfo.from_string(doc.data('xml')) return bookinfo.serialize() except RevisionNotFound: - return rc.NOT_HERE + return response.EntityNotFound().django_response() def update(self, request, docid): lib = MercurialLibrary(path=settings.REPOSITORY_PATH) @@ -205,15 +229,24 @@ class DocumentDublinCoreHandler(BaseHandler): orig = lib.document_for_rev(prev) if current != orig: - return rc.DUPLICATE_ENTRY + return response.EntityConflict().django_response({ + "reason": "out-of-date", + "provided": orig.revision, + "latest": current.revision }) xmldoc = parser.WLDocument.from_string(current.data('xml')) document.book_info = dcparser.BookInfo.from_json(bi_json) # zapisz - current.quickwrite('xml', document.serialize().encode('utf-8'),\ + ndoc = current.quickwrite('xml', \ + document.serialize().encode('utf-8'),\ message=msg, user=request.user.username) - return rc.ALL_OK + return { + "document": ndoc.id, + "subview": "xml", + "previous_revision": prev, + "updated_revision": ndoc.revision + } except (RevisionNotFound, KeyError): - return rc.NOT_HERE + return response.EntityNotFound().django_response() diff --git a/apps/api/response.py b/apps/api/response.py new file mode 100644 index 00000000..322118a0 --- /dev/null +++ b/apps/api/response.py @@ -0,0 +1,119 @@ +# -*- encoding: utf-8 -*- + +__author__= "Łukasz Rekucki" +__date__ = "$2009-09-26 00:32:18$" +__doc__ = "Extensible HTTP Responses." + +from django.http import HttpResponse +from django.utils import simplejson as json + +MIME_PLAIN = 'text/plain' +MIME_JSON = 'application/json' + +class ResponseObject(object): + + def __init__(self, code, mimetype=MIME_JSON): + self._code = code + self._mime = mimetype + + def django_response(self, body=None): + if body is None: + data = '' + elif self._mime == MIME_JSON: + data = json.dumps(body, default=lambda o: repr(o) ) + else: + data = u"%s\n%s" % (self.MESSAGE, unicode(self._info)) + data = data.encode('utf-8') + + return HttpResponse(content=data, status=self._code, \ + content_type=self._mime+'; charset=utf-8' ) + +class SuccessAllOk(ResponseObject): + def __init__(self, **kwargs): + ResponseObject.__init__(self, 200, **kwargs) + +class EntityCreated(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 201, **kwargs) + + def django_response(self, url, body): + response = ResponseObject.django_response(self, body) + response['Location'] = url + return response + +class RequestAccepted(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 202, **kwargs) + + def django_response(self, ticket_status, ticket_uri): + return ResponseObject.django_response(self, { + 'status': ticket_status, + 'refer_to': ticket_uri }) + +class SuccessNoContent(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 204, **kwargs) + + def django_response(self): + return ResponseObject.django_response(self, body=None) + + +# +# Client errors +# + +class BadRequest(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 400, **kwargs) + +class AccessDenied(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 403, **kwargs) + + def django_response(self, reason): + return ResponseObject.django_response(self, \ + body={'reason': reason}) + +class EntityNotFound(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 404, **kwargs) + +class EntityGone(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 410, **kwargs) + + +class EntityConflict(ResponseObject): + + def __init__(self, **kwargs): + ResponseObject.__init__(self, 409, **kwargs) + + +def validate_form(formclass, source='GET'): + from functools import wraps + + def decorator(func): + @wraps(func) + def decorated(self, request, *args, **kwargs): + form = formclass(getattr(request, source), request.FILES) + + if not form.is_valid(): + errorlist = [{'field': k, 'errors': e} for k,e in form.errors] + return BadRequest().django_response(errorlist) + + return func(self, request, form, *args, **kwargs) + return decorated + return decorator + + + + + + diff --git a/lib/wlrepo/__init__.py b/lib/wlrepo/__init__.py index cbc0c2c6..7e878984 100644 --- a/lib/wlrepo/__init__.py +++ b/lib/wlrepo/__init__.py @@ -110,5 +110,8 @@ class RevisionNotFound(LibraryException): LibraryException.__init__(self, "Revision %r not found." % rev) pass +class DocumentAlreadyExists(LibraryException): + pass + # import backends to local namespace from mercurial_backend.library import MercurialLibrary \ No newline at end of file diff --git a/lib/wlrepo/mercurial_backend/document.py b/lib/wlrepo/mercurial_backend/document.py index bced6741..657fa6e3 100644 --- a/lib/wlrepo/mercurial_backend/document.py +++ b/lib/wlrepo/mercurial_backend/document.py @@ -11,7 +11,7 @@ class MercurialDocument(wlrepo.Document): def data(self, entry): path = self._revision._docname + '.' + entry return self._library._filectx(path, \ - self._revision.hgrev()).data() + self._revision.hgrev()).data() def quickwrite(self, entry, data, msg, user=None): user = user or self.owner @@ -54,7 +54,6 @@ class MercurialDocument(wlrepo.Document): return self return self._library.document(docid=self._revision.document_name()) - def take(self, user): fullid = self._library.fulldocid(self.id, user) diff --git a/lib/wlrepo/mercurial_backend/library.py b/lib/wlrepo/mercurial_backend/library.py index 219a01df..e9861f4c 100644 --- a/lib/wlrepo/mercurial_backend/library.py +++ b/lib/wlrepo/mercurial_backend/library.py @@ -96,6 +96,8 @@ class MercurialLibrary(wlrepo.Library): return self.document_for_rev(self.fulldocid(docid, user)) def get_revision(self, revid): + revid = self._sanitize_string(revid) + ctx = self._changectx(revid) if ctx is None: @@ -107,6 +109,9 @@ class MercurialLibrary(wlrepo.Library): return MercurialRevision(self, ctx) def fulldocid(self, docid, user=None): + docid = self._sanitize_string(docid) + user = self._sanitize_string(user) + fulldocid = '' if user is not None: fulldocid += '$user:' + user @@ -122,11 +127,13 @@ class MercurialLibrary(wlrepo.Library): return False def document_create(self, docid): + docid = self._sanitize_string(docid) + # check if it already exists fullid = self.fulldocid(docid) if self.has_revision(fullid): - raise LibraryException("Document already exists!"); + raise wlrepo.DocumentAlreadyExists("Document %s already exists!" % docid); # doesn't exist self._create_branch(fullid) @@ -241,6 +248,9 @@ class MercurialLibrary(wlrepo.Library): @staticmethod def _sanitize_string(s): + if s is None: + return None + if isinstance(s, unicode): s = s.encode('utf-8') -- 2.20.1