def request_data(): if hasattr(flask.request, 'parsed_data'): return flask.request.parsed_data if not flask.request.content_length > 0: LOG.debug("Empty body provided in request") return dict() if flask.request.file_upload: return flask.request.data deserializer = None content_type = flask.request.mimetype if not content_type or content_type in RT_JSON: deserializer = wsgi.JSONDeserializer() elif content_type in RT_XML: abort_and_log(400, _("XML requests are not supported yet")) else: abort_and_log(400, _("Content type '%s' isn't supported") % content_type) # parsed request data to avoid unwanted re-parsings parsed_data = deserializer.deserialize(flask.request.data)['body'] flask.request.parsed_data = parsed_data return flask.request.parsed_data
def render(res=None, resp_type=None, status=None, **kwargs): if not res: res = {} if type(res) is dict: res.update(kwargs) elif kwargs: # can't merge kwargs into the non-dict res abort_and_log(500, _("Non-dict and non-empty kwargs passed to render")) status_code = getattr(flask.request, 'status_code', None) if status: status_code = status if not status_code: status_code = 200 if not resp_type: resp_type = getattr(flask.request, 'resp_type', None) if not resp_type: resp_type = RT_JSON serializer = None if "application/json" in resp_type: resp_type = RT_JSON serializer = wsgi.JSONDictSerializer() elif "application/xml" in resp_type: resp_type = RT_XML serializer = wsgi.XMLDictSerializer() else: abort_and_log(400, _("Content type '%s' isn't supported") % resp_type) body = serializer.serialize(res) resp_type = str(resp_type) return flask.Response(response=body, status=status_code, mimetype=resp_type)
class DistilException(Exception): """Base Exception for the project To correctly use this class, inherit from it and define a 'message' and 'code' properties. """ message = _("An unknown exception occurred") code = 500 def __str__(self): return self.message def __init__(self, message=None): if message is not None: self.message = message super(DistilException, self).__init__( '%s: %s' % (self.code, self.message))
def _from_json(self, datastring): try: return jsonutils.loads(datastring) except ValueError: msg = _("cannot understand JSON") raise exceptions.MalformedRequestBody(reason=msg)
class ERPException(DistilException): code = 500 message = _("ERP server error.")
class InvalidDriver(DistilException): """A driver was not found or loaded.""" message = _("Failed to load driver")
class Forbidden(DistilException): code = 403 message = _("You are not authorized to complete this action")
class MalformedRequestBody(DistilException): code = 400 message = _("Malformed message body.")
class DBException(DistilException): message = _("Database exception.")
class InvalidConfig(DistilException): message = _("Invalid configuration.")
class DuplicateException(DistilException): code = 409 message = _("An object with the same identifier already exists.")
class NotFoundException(DistilException): code = 404 message = _("Object not found.")
class IncorrectStateError(DistilException): message = _("Incorrect state.")
class DateTimeException(DistilException): code = 400 message = _("An unexpected date, date format, or date range was given.")
def ctx(): if not has_ctx(): raise ex.IncorrectStateError(_("Context isn't available here")) return getattr(_CTX_STORE, _CTX_KEY)