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
13 # Document List Handlers
15 class BasicLibraryHandler(AnonymousBaseHandler):
16 allowed_methods = ('GET',)
18 def read(self, request):
19 """Return the list of documents."""
20 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
21 cab = lib.main_cabinet
24 'url': reverse('document_view', args=[docid]),
25 'name': docid } for docid in cab.documents() ]
29 'latest_rev': cab.shelf(),
30 'documents' : document_list }
32 class LibraryHandler(BaseHandler):
33 allowed_methods = ('GET', 'POST')
34 anonymous = BasicLibraryHandler
36 def read(self, request):
37 """Return the list of documents."""
38 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
39 cab = lib.main_cabinet
42 'url': reverse('document_view', args=[docid]),
43 'name': docid } for docid in cab.documents() ]
47 'latest_rev': cab.shelf(),
48 'documents' : document_list }
50 def create(self, request):
51 """Create a new document."""
52 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
53 cab = lib.main_cabinet
55 form = forms.DocumentUploadForm(request.POST, request.FILES)
56 if not form.is_valid():
59 f = request.FILES['ocr']
60 data = f.read().decode('utf-8')
62 if form.cleaned_data['generate_dc']:
63 data = librarian.wrap_text(data, unicode(date.today()))
65 doc = cab.create(form.cleaned_data['bookname'], initial_data=data)
68 'url': reverse('document_view', args=[doc.name]),
71 'revision': doc.shelf() }
76 class BasicDocumentHandler(AnonymousBaseHandler):
77 allowed_methods = ('GET',)
79 def read(self, request, docid):
80 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
82 opts = forms.DocumentGetForm(request.GET)
83 if not opts.is_valid():
86 document = lib.main_cabinet.retrieve(docid)
89 'name': document.name,
90 'size': document.size,
91 'text_url': reverse('doctext_view', args=[docid]),
92 #'dc_url': reverse('docdc_view', docid=document.name),
93 #'parts_url': reverse('docparts_view', docid=document.name),
94 'latest_rev': document.shelf(),
97 if request.GET.get('with_part', 'no') == 'yes':
98 result['parts'] = document.parts()
102 class DocumentHandler(BaseHandler):
103 allowed_methods = ('GET', 'PUT')
104 anonymous = BasicDocumentHandler
106 def read(self, request, docid):
107 """Read document's meta data"""
108 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
110 opts = forms.DocumentGetForm(request.GET)
111 if not opts.is_valid():
112 return rc.BAD_REQUEST
114 document = lib.cabinet(docid, request.user.username, \
115 create=opts.cleaned_data['autocabinet'] ).retrieve()
117 shared = lib.main_cabinet.retrieve(docid)
120 'name': document.name,
121 'size': document.size,
122 'text_url': reverse('doctext_view', args=[docid]),
123 'dc_url': reverse('docdc_view', args=[docid]),
124 'parts_url': reverse('docparts_view', args=[docid]),
125 'latest_rev': document.shelf(),
126 'latest_shared_rev': shared.shelf(),
127 #'shared': lib.isparentof(document, shared),
128 #'up_to_date': lib.isparentof(shared, document),
131 if request.GET.get('with_part', 'no') == 'yes':
132 result['parts'] = document.parts()
139 class DocumentTextHandler(BaseHandler):
140 allowed_methods = ('GET', 'PUT')
142 def read(self, request, docid):
143 """Read document as raw text"""
144 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
146 return lib.document(docid, request.user.username).read()
147 except CabinetNotFound:
150 def update(self, request, docid):
151 lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
153 data = request.PUT['contents']
154 lib.document(docid, request.user.username).write(data)
156 except (CabinetNotFound, KeyError):