Dodanie DocumentModel i wczytywanie danych w XMLView.
[redakcja.git] / apps / api / handlers.py
1 from piston.handler import BaseHandler, AnonymousBaseHandler
2 from piston.utils import rc, validate
3
4 import settings
5 import librarian
6 import api.forms as forms
7 from datetime import date
8
9 from django.core.urlresolvers import reverse
10 from wlrepo import MercurialLibrary, CabinetNotFound
11
12 #
13 # Document List Handlers
14 #
15 class BasicLibraryHandler(AnonymousBaseHandler):
16     allowed_methods = ('GET',)
17
18     def read(self, request):
19         """Return the list of documents."""
20         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
21         cab = lib.main_cabinet
22
23         document_list = [{
24             'url': reverse('document_view', args=[docid]),
25             'name': docid } for docid in cab.documents() ]
26
27         return {
28             'cabinet': cab.name,
29             'latest_rev': cab.shelf(),
30             'documents' : document_list }
31
32 class LibraryHandler(BaseHandler):
33     allowed_methods = ('GET', 'POST')
34     anonymous = BasicLibraryHandler
35
36     def read(self, request):
37         """Return the list of documents."""
38         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
39         cab = lib.main_cabinet
40
41         document_list = [{
42             'url': reverse('document_view', args=[docid]),
43             'name': docid } for docid in cab.documents() ]
44
45         return {
46             'cabinet': cab.name,
47             'latest_rev': cab.shelf(),
48             'documents' : document_list }
49
50     def create(self, request):
51         """Create a new document."""
52         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
53         cab = lib.main_cabinet
54
55         form = forms.DocumentUploadForm(request.POST, request.FILES)
56         if not form.is_valid():
57             return rc.BAD_REQUEST
58
59         f = request.FILES['ocr']
60         data = f.read().decode('utf-8')
61
62         if form.cleaned_data['generate_dc']:
63             data = librarian.wrap_text(data, unicode(date.today()))
64
65         doc = cab.create(form.cleaned_data['bookname'], initial_data=data)
66         
67         return {
68             'url': reverse('document_view', args=[doc.name]),
69             'name': doc.name,
70             'size': doc.size,
71             'revision': doc.shelf() }
72
73 #
74 # Document Handlers
75 #
76 class BasicDocumentHandler(AnonymousBaseHandler):
77     allowed_methods = ('GET',)
78
79     def read(self, request, docid):
80         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
81
82         opts = forms.DocumentGetForm(request.GET)
83         if not opts.is_valid():
84             return rc.BAD_REQUEST
85
86         document = lib.main_cabinet.retrieve(docid)        
87
88         result = {
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(),          
95         }
96
97         if request.GET.get('with_part', 'no') == 'yes':
98             result['parts'] = document.parts()
99
100         return result   
101         
102 class DocumentHandler(BaseHandler):
103     allowed_methods = ('GET', 'PUT')
104     anonymous = BasicDocumentHandler
105     
106     def read(self, request, docid):
107         """Read document's meta data"""        
108         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
109
110         opts = forms.DocumentGetForm(request.GET)
111         if not opts.is_valid():
112             return rc.BAD_REQUEST
113               
114         document = lib.cabinet(docid, request.user.username, \
115                 create=opts.cleaned_data['autocabinet'] ).retrieve()
116                 
117         shared = lib.main_cabinet.retrieve(docid)
118
119         result = {
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),
129         }
130
131         if request.GET.get('with_part', 'no') == 'yes':
132             result['parts'] = document.parts()
133
134         return result        
135
136 #
137 # Document Text View
138 #
139 class DocumentTextHandler(BaseHandler):
140     allowed_methods = ('GET', 'PUT')
141
142     def read(self, request, docid):
143         """Read document as raw text"""
144         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
145         try:            
146             return lib.document(docid, request.user.username).read()
147         except CabinetNotFound:
148             return rc.NOT_HERE
149
150     def update(self, request, docid):
151         lib = MercurialLibrary(path=settings.REPOSITORY_PATH)
152         try:
153             data = request.PUT['contents']
154             lib.document(docid, request.user.username).write(data)
155             return rc.ALL_OK
156         except (CabinetNotFound, KeyError):
157             return rc.NOT_HERE