Use appropriate status codes in http response

This commit is contained in:
Jan Philipp Timme 2016-11-18 20:48:02 +01:00
parent 1b66fbb7c6
commit b0be8acdd4
Signed by: JPT
GPG Key ID: 5F2C85EC6F3754B7
1 changed files with 9 additions and 0 deletions

View File

@ -88,8 +88,10 @@ class RESTView(object):
# Store file data in database # Store file data in database
self.db_add_file(file_id, file_data) self.db_add_file(file_id, file_data)
# Hopefully everything worked out, so return the data from the file # Hopefully everything worked out, so return the data from the file
self.request.response.status = '201 Created'
return file_data return file_data
except: except:
self.request.response.status = '503 Service Unavailable'
return dict(success=False) return dict(success=False)
@view_config(request_method='GET', route_name='files_uuid') @view_config(request_method='GET', route_name='files_uuid')
@ -101,8 +103,10 @@ class RESTView(object):
external_filename = file_data['name'] external_filename = file_data['name']
file_response = FileResponse(os.path.join(self.path_file_storage, '%s' % file_id)) file_response = FileResponse(os.path.join(self.path_file_storage, '%s' % file_id))
file_response.content_disposition = 'attachment; filename=%s' % external_filename file_response.content_disposition = 'attachment; filename=%s' % external_filename
self.request.response.status = '200 OK'
return file_response return file_response
except: except:
self.request.response.status = '404 Not Found'
return dict(success=False) return dict(success=False)
@view_config(request_method='GET', route_name='file_details') @view_config(request_method='GET', route_name='file_details')
@ -112,8 +116,10 @@ class RESTView(object):
try: try:
file_data = self.db_fetch_file(file_id) file_data = self.db_fetch_file(file_id)
del file_data['key'] # Remove the secret key only the creator is supposed to know del file_data['key'] # Remove the secret key only the creator is supposed to know
self.request.response.status = '200 OK'
return file_data return file_data
except: except:
self.request.response.status = '404 Not Found'
return dict(success=False) return dict(success=False)
@view_config(request_method='DELETE', route_name='file_uuid_key') @view_config(request_method='DELETE', route_name='file_uuid_key')
@ -124,13 +130,16 @@ class RESTView(object):
try: try:
file_data = self.db_fetch_file(file_id) file_data = self.db_fetch_file(file_id)
except: except:
self.request.response.status = '404 Not Found'
return dict(success=False) return dict(success=False)
if file_data['key'] == file_key: if file_data['key'] == file_key:
# Delete file and db entry # Delete file and db entry
file_path = os.path.join(self.path_file_storage, '%s' % file_id) file_path = os.path.join(self.path_file_storage, '%s' % file_id)
os.remove(file_path) os.remove(file_path)
self.db_delete_file(file_id) self.db_delete_file(file_id)
self.request.response.status = '200 OK'
return dict(success=True) return dict(success=True)
else: else:
self.request.response.status = '500 Internal Server Error'
return dict(success=False) return dict(success=False)