1 from piston.handler import BaseHandler, AnonymousBaseHandler
2 from piston.utils import rc, validate
6 import api.forms as forms
7 from datetime import date
9 from django.core.urlresolvers import reverse
10 from wlrepo import MercurialLibrary, CabinetNotFound
12 from librarian import dcparser
15 # Document List Handlers
17 class BasicLibraryHandler(AnonymousBaseHandler):
18 allowed_methods = ('GET',)
20 def read(self, request):
21 """Return the list of documents."""
22 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
23 cab = lib.main_cabinet
26 'url': reverse('document_view', args=[docid]),
27 'name': docid } for docid in cab.documents() ]
31 'latest_rev': cab.shelf(),
32 'documents' : document_list }
34 class LibraryHandler(BaseHandler):
35 allowed_methods = ('GET', 'POST')
36 anonymous = BasicLibraryHandler
38 def read(self, request):
39 """Return the list of documents."""
40 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
41 cab = lib.main_cabinet
44 'url': reverse('document_view', args=[docid]),
45 'name': docid } for docid in cab.documents() ]
49 'latest_rev': cab.shelf(),
50 'documents' : document_list }
52 def create(self, request):
53 """Create a new document."""
54 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
55 cab = lib.main_cabinet
57 form = forms.DocumentUploadForm(request.POST, request.FILES)
58 if not form.is_valid():
61 f = request.FILES['ocr']
62 data = f.read().decode('utf-8')
64 if form.cleaned_data['generate_dc']:
65 data = librarian.wrap_text(data, unicode(date.today()))
67 doc = cab.create(form.cleaned_data['bookname'], initial_data=data)
70 'url': reverse('document_view', args=[doc.name]),
73 'revision': doc.shelf() }
78 class BasicDocumentHandler(AnonymousBaseHandler):
79 allowed_methods = ('GET',)
81 def read(self, request, docid):
82 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
84 opts = forms.DocumentGetForm(request.GET)
85 if not opts.is_valid():
88 document = lib.main_cabinet.retrieve(docid)
91 'name': document.name,
92 'size': document.size,
93 'text_url': reverse('doctext_view', args=[docid]),
94 #'dc_url': reverse('docdc_view', docid=document.name),
95 #'parts_url': reverse('docparts_view', docid=document.name),
96 'latest_rev': document.shelf(),
99 if request.GET.get('with_part', 'no') == 'yes':
100 result['parts'] = document.parts()
107 class DocumentHandler(BaseHandler):
108 allowed_methods = ('GET', 'PUT')
109 anonymous = BasicDocumentHandler
111 def read(self, request, docid):
112 """Read document's meta data"""
113 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
115 opts = forms.DocumentGetForm(request.GET)
116 if not opts.is_valid():
117 return rc.BAD_REQUEST
119 document = lib.cabinet(docid, request.user.username, \
120 create=opts.cleaned_data['autocabinet'] ).retrieve()
125 shared = lib.main_cabinet.retrieve(docid)
128 'name': document.name,
129 'size': document.size,
130 'text_url': reverse('doctext_view', args=[docid]),
131 'dc_url': reverse('docdc_view', args=[docid]),
132 'parts_url': reverse('docparts_view', args=[docid]),
133 'latest_rev': document.shelf(),
134 'latest_shared_rev': shared.shelf(),
135 #'shared': lib.isparentof(document, shared),
136 #'up_to_date': lib.isparentof(shared, document),
139 if request.GET.get('with_part', 'no') == 'yes':
140 result['parts'] = document.parts()
147 class DocumentTextHandler(BaseHandler):
148 allowed_methods = ('GET', 'PUT')
150 def read(self, request, docid):
151 """Read document as raw text"""
152 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
154 return lib.document(docid, request.user.username).read()
155 except CabinetNotFound:
158 def update(self, request, docid):
159 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
161 data = request.PUT['contents']
162 lib.document(docid, request.user.username).write(data)
164 except (CabinetNotFound, KeyError):
169 # Dublin Core handlers
171 # @requires librarian
173 class DocumentDublinCoreHandler(BaseHandler):
174 allowed_methods = ('GET', 'PUT')
176 def read(self, request, docid):
177 """Read document as raw text"""
178 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
180 doc = lib.document(docid, request.user.username)
182 # TODO: RAL:document should support file-like ops
183 bookinfo = dcparser.BookInfo.from_string(doc.read())
184 return bookinfo.serialize()
185 except CabinetNotFound:
188 def update(self, request, docid):
189 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
191 data = request.PUT['contents']
192 lib.document(docid, request.user.username).write(data)
194 except (CabinetNotFound, KeyError):