Mercurial > odfcast > odfcast
comparison odfcast/convert.py @ 94:2ef34abbad8d 1.5
Use http error code 422 in CheckView when a merge is not possible.
Previously the response used the code 500 in this case. This is not
useful because it's not an internal server error when the check whether
a PDF file can be merged fails because that's the point of the
CheckView. The code used now means "Unprocessable Entity" and fits
better.
Part of mpuls/issue6009
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Fri, 28 Apr 2017 20:56:22 +0200 |
parents | 48dabf4bf680 |
children | b2f96072b8d7 |
comparison
equal
deleted
inserted
replaced
93:48dabf4bf680 | 94:2ef34abbad8d |
---|---|
244 | 244 |
245 def get(self): | 245 def get(self): |
246 return render_template("check.html") | 246 return render_template("check.html") |
247 | 247 |
248 def post(self): | 248 def post(self): |
249 """Check that the attached PDF file is ready for merging. | |
250 If it is not ready a MergeErrorResponse is returned with | |
251 http_error_code=422. The default error code of 500 is not really | |
252 sensible because it is not an internal server error if the | |
253 attachment cannot be merged. The code 422 is used in WEB-DAV | |
254 with the meaning "Unprocessable Entity" which fits relatively | |
255 well. | |
256 """ | |
249 log.debug("Checking a PDF document's readiness for merging") | 257 log.debug("Checking a PDF document's readiness for merging") |
250 | 258 |
251 ffile = request.files['file'] | 259 ffile = request.files['file'] |
252 if not ffile.filename: | 260 if not ffile.filename: |
253 return ErrorResponse( | 261 return ErrorResponse( |
259 merger = PdfFileMerger(strict=False) | 267 merger = PdfFileMerger(strict=False) |
260 try: | 268 try: |
261 merger.append(ffile, import_bookmarks=False) | 269 merger.append(ffile, import_bookmarks=False) |
262 except Exception, e: | 270 except Exception, e: |
263 log.exception("Error testing merger.append of %s" % ffile) | 271 log.exception("Error testing merger.append of %s" % ffile) |
264 return MergeErrorResponse(details=str(e)) | 272 return MergeErrorResponse(details=str(e), http_error_code=422) |
265 | 273 |
266 try: | 274 try: |
267 merger.write(outfile) | 275 merger.write(outfile) |
268 except Exception, e: | 276 except Exception, e: |
269 log.exception("Error testing merger.write of merged %s" % ffile) | 277 log.exception("Error testing merger.write of merged %s" % ffile) |
270 return MergeErrorResponse(details=str(e)) | 278 return MergeErrorResponse(details=str(e), http_error_code=422) |
271 | 279 |
272 merger.close() | 280 merger.close() |
273 | 281 |
274 log.debug("PDF document %s checked." % ffile) | 282 log.debug("PDF document %s checked." % ffile) |
275 return Response("Okay.") | 283 return Response("Okay.") |