Dodanie obsługi błędów.
[redakcja.git] / apps / api / handlers / text_handler.py
1 # -*- encoding: utf-8 -*-
2
3 __author__= "Łukasz Rekucki"
4 __date__ = "$2009-10-19 14:34:42$"
5 __doc__ = "Module documentation."
6
7 #import api.forms as forms
8 #import api.response as response
9 #
10 #from api.utils import validate_form, hglibrary
11 #from api.models import PartCache
12 #
13
14 #
15 #from piston.handler import BaseHandler
16 #
17 #from wlrepo import *
18
19 import re
20 from library_handlers import *
21
22 import librarian
23 from librarian import parser
24
25 #
26 # Document Text View
27 #
28
29 XINCLUDE_REGEXP = r"""<(?:\w+:)?include\s+[^>]*?href=("|')wlrepo://(?P<link>[^\1]+?)\1\s*[^>]*?>"""
30 #
31 #
32 #
33
34 class DocumentTextHandler(BaseHandler):
35     allowed_methods = ('GET', 'POST')
36
37     @validate_form(forms.TextRetrieveForm, 'GET')
38     @hglibrary
39     def read(self, request, form, docid, lib):
40         """Read document as raw text"""
41         try:
42             revision = form.cleaned_data['revision']
43             chunk = form.cleaned_data['chunk']
44             user = form.cleaned_data['user'] or request.user.username
45             format = form.cleaned_data['format']
46
47             document = lib.document_for_revision(revision)
48
49             if document.id != docid:
50                 return response.BadRequest().django_response({
51                     'reason': 'name-mismatch',
52                     'message': 'Provided revision is not valid for this document'
53                 })
54
55             if document.owner != user:
56                 return response.BadRequest().django_response({
57                     'reason': 'user-mismatch',
58                     'message': "Provided revision doesn't belong to user %s" % user
59                 })
60
61             for error in check_user(request, user):
62                 return error
63
64             if not chunk:
65                 return document.data('xml')
66
67             xdoc = parser.WLDocument.from_string(document.data('xml'), parse_dublincore=False)
68
69             xchunk = xdoc.chunk(chunk)
70
71             if xchunk is None:
72                 return response.EntityNotFound().django_response({
73                       'reason': 'no-chunk-in-document',
74                       'path': chunk
75                 })
76
77             return librarian.serialize_children(xchunk, format=format)
78
79         except librarian.ParseError, e:
80             return response.EntityNotFound().django_response({
81                 'reason': 'invalid-document-state',
82                 'exception': type(e),
83                 'message': e.message
84             })
85         except (EntryNotFound, RevisionNotFound), e:
86             return response.EntityNotFound().django_response({
87                 'reason': 'not-found',
88                 'exception': type(e), 'message': e.message
89             })
90
91     @validate_form(forms.TextUpdateForm, 'POST')
92     @hglibrary
93     def create(self, request, form, docid, lib):
94         lock = lib.lock();
95         try:
96             revision = form.cleaned_data['revision']
97             msg = form.cleaned_data['message']
98             user = form.cleaned_data['user'] or request.user.username
99
100             # do not allow changing not owned documents
101             # (for now... )
102
103             if user != request.user.username:
104                 return response.AccessDenied().django_response({
105                     'reason': 'insufficient-priviliges',
106                 })
107
108             current = lib.document(docid, user)
109             orig = lib.document_for_revision(revision)
110
111             if current != orig:
112                 return response.EntityConflict().django_response({
113                         "reason": "out-of-date",
114                         "provided_revision": orig.revision,
115                         "latest_revision": current.revision })
116
117             if form.cleaned_data['contents']:
118                 data = form.cleaned_data['contents']
119             else:
120                 chunks = form.cleaned_data['chunks']
121                 data = current.data('xml')
122                 log.info(data[:600])
123                 log.info(chunks)
124
125                 xdoc = parser.WLDocument.from_string(data, parse_dublincore=False)
126                 errors = xdoc.merge_chunks(chunks)
127
128                 if len(errors):
129                     return response.EntityConflict().django_response({
130                             "reason": "invalid-chunks",
131                             "message": "Unable to merge following parts into the document: %s " % ",".join(errors)
132                     })
133
134                 data = xdoc.serialize()
135
136
137             # try to find any Xinclude tags
138             includes = [m.groupdict()['link'] for m in (re.finditer(\
139                 XINCLUDE_REGEXP, data, flags=re.UNICODE) or []) ]
140
141             log.info("INCLUDES: %s", includes)
142
143             # TODO: provide useful routines to make this simpler
144             def xml_update_action(lib, resolve):
145                 try:
146                     f = lib._fileopen(resolve('parts'), 'r')
147                     stored_includes = json.loads(f.read())
148                     f.close()
149                 except:
150                     stored_includes = []
151
152                 if stored_includes != includes:
153                     f = lib._fileopen(resolve('parts'), 'w+')
154                     f.write(json.dumps(includes))
155                     f.close()
156
157                     lib._fileadd(resolve('parts'))
158
159                     # update the parts cache
160                     PartCache.update_cache(docid, current.owner,\
161                         stored_includes, includes)
162
163                 # now that the parts are ok, write xml
164                 f = lib._fileopen(resolve('xml'), 'w+')
165                 f.write(data.encode('utf-8'))
166                 f.close()
167
168             ndoc = None
169             ndoc = current.invoke_and_commit(\
170                 xml_update_action, lambda d: (msg, user) )
171
172             try:
173                 # return the new revision number
174                 return response.SuccessAllOk().django_response({
175                     "document": ndoc.id,
176                     "user": user,
177                     "subview": "xml",
178                     "previous_revision": current.revision,
179                     "revision": ndoc.revision,
180                     'timestamp': ndoc.revision.timestamp,
181                     "url": reverse("doctext_view", args=[ndoc.id])
182                 })
183             except Exception, e:
184                 if ndoc: lib._rollback()
185                 raise e
186         except RevisionNotFound, e:
187             return response.EntityNotFound(mimetype="text/plain").\
188                 django_response(e.message)
189         finally:
190                 lock.release()