def _obscure_json_response(*args, **kwargs): req = args[0] https_req, error = get_optional_parameter(req, 'https', default=0, formatter=int) if error: https_req = 1 retval = func(*args, **kwargs) if not isinstance(retval, HttpResponse): charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8') try: charsets = parse_charsets(charset) content = simplejson.dumps(retval, ensure_ascii=False) content, charset = try_encode(content, charsets, 'utf-8') if https_req < 1: #only obscure response for request without https content = obscure(content) response = HttpResponse( content, content_type='application/json; charset=%s' % charset) except ValueError, err: _LOGGER.error(err) return internal_server_error(req, err, exc_info=sys.exc_info()) except Exception, err: _LOGGER.error('failed to convert encoding: %s' % err, exc_info=1) return internal_server_error(req, 'Unexpected error happened.', exc_info=sys.exc_info())
def json_responsed(*args, **kwargs): req = args[0] retval = func(*args, **kwargs) if not isinstance(retval, HttpResponse): charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8') try: #ignore request charset since it might request *, which is not valid #charsets = parse_charsets(charset) charsets = [] content = simplejson.dumps(retval, ensure_ascii=False, default=unicode) content, charset = try_encode(content, charsets, 'utf-8') response = HttpResponse( content, content_type='application/json; charset=%s' % charset) except ValueError, err: _LOGGER.error(err) return internal_server_error(req, err, exc_info=sys.exc_info()) except Exception, err: _LOGGER.error('failed to convert encoding: %s' % err, exc_info=1) return internal_server_error(req, 'Unexpected error happened.', exc_info=sys.exc_info())
def string_responsed(*args, **kwargs): req = args[0] retval = func(*args, **kwargs) if not isinstance(retval, HttpResponse): charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8') try: response = HttpResponse(retval, content_type='text/html; charset=%s' % charset) except ValueError, err: _LOGGER.error("Error:%s. retval: %s" % (err, retval)) return internal_server_error(req, err, exc_info=sys.exc_info()) except Exception, err: _LOGGER.error('failed to convert encoding: %s' % err, exc_info=1) return internal_server_error(req, 'Unexpected error happened.', exc_info=sys.exc_info())
def authenticate_func(*args, **kwargs): if not self._enabled: return func(*args, **kwargs) req = args[0] user_key = req.COOKIES.get("user_key", None) auth_id = None key_name = None if self._key_pos is not None: try: auth_id = args[self._key_pos] except IndexError as err: return internal_server_error(req, err, sys.exc_info()) else: for i in range(len(self._key_names)): # default formatter is str key_name = self._key_names[i] fmt = self._key_formatters[i] if i < len(self._key_formatters) else str auth_id, error = get_optional_parameter(req, key_name, formatter=fmt, default=None, method=self._method) if error: return error elif auth_id is not None: break success = self._authenticate(key_name, auth_id, user_key) if success: retval = func(*args, **kwargs) if isinstance(retval, HttpResponse) and user_key is not None: retval.set_cookie("user_key", user_key, path=self._cookie_path, domain=self._cookie_domain, httponly=True) return retval else: return retval else: return authentication_fail(req, self._fail_info)
def json_responsed(*args, **kwargs): req = args[0] retval = func(*args, **kwargs) if not isinstance(retval, HttpResponse): charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8') try: #ignore request charset since it might request *, which is not valid #charsets = parse_charsets(charset) charsets = [] content = simplejson.dumps(retval, ensure_ascii=False, default=unicode) content, charset = try_encode(content, charsets, 'utf-8') response = HttpResponse(content, content_type='application/json; charset=%s' % charset) except ValueError, err: _LOGGER.error(err) return internal_server_error(req, err, exc_info=sys.exc_info()) except Exception, err: _LOGGER.error('failed to convert encoding: %s' % err, exc_info=1) return internal_server_error(req, 'Unexpected error happened.', exc_info=sys.exc_info())
def _obscure_json_response(*args, **kwargs): req = args[0] https_req, error = get_optional_parameter(req, 'https', default=0, formatter=int) if error: https_req = 1 retval = func(*args, **kwargs) if not isinstance(retval, HttpResponse): charset = req.META.get('HTTP_ACCEPT_CHARSET', 'utf-8') try: charsets = parse_charsets(charset) content = simplejson.dumps(retval, ensure_ascii=False) content, charset = try_encode(content, charsets, 'utf-8') if https_req < 1: #only obscure response for request without https content = obscure(content) response = HttpResponse(content, content_type='application/json; charset=%s' % charset) except ValueError, err: _LOGGER.error(err) return internal_server_error(req, err, exc_info=sys.exc_info()) except Exception, err: _LOGGER.error('failed to convert encoding: %s' % err, exc_info=1) return internal_server_error(req, 'Unexpected error happened.', exc_info=sys.exc_info())
def authenticate_func(*args, **kwargs): if not self._enabled: return func(*args, **kwargs) req = args[0] user_key = req.COOKIES.get("user_key", None) auth_id = None key_name = None if self._key_pos is not None: try: auth_id = args[self._key_pos] except IndexError as err: return internal_server_error(req, err, sys.exc_info()) else: for i in range(len(self._key_names)): # default formatter is str key_name = self._key_names[i] fmt = self._key_formatters[i] if i < len( self._key_formatters) else str auth_id, error = get_optional_parameter( req, key_name, formatter=fmt, default=None, method=self._method) if error: return error elif auth_id is not None: break success = self._authenticate(key_name, auth_id, user_key) if success: retval = func(*args, **kwargs) if isinstance(retval, HttpResponse) and user_key is not None: retval.set_cookie("user_key", user_key, path=self._cookie_path, domain=self._cookie_domain, httponly=True) return retval else: return retval else: return authentication_fail(req, self._fail_info)
def catch_except(func): """ Indicating a method process general exception. """ @wraps(func) def _catch_except(*args, **kwargs): """ catch exception raised by func """ try: request = args[0] return func(*args, **kwargs) except (FormatterError, RequiredNoDefaultError, RequiredLackedError, MethodError, ParameterError), err: _LOGGER.error(err) return parameter_error(request, str(err)) except InvalidName, err: msg = 'keys in document error: %s' % err _LOGGER.exception(msg) return internal_server_error(request, msg, sys.exc_info())
from weibonews.utils.web.errors import parameter_error, internal_server_error from wechat.utils.exceptions import ParameterError, FormatterError, RequiredNoDefaultError, RequiredLackedError, MethodError _LOGGER = logging.getLogger('weibonews.wechat') def catch_except(func): """ Indicating a method process general exception. """ @wraps(func) def _catch_except(*args, **kwargs): """ catch exception raised by func """ try: request = args[0] return func(*args, **kwargs) except (FormatterError, RequiredNoDefaultError, RequiredLackedError, MethodError, ParameterError), err: _LOGGER.error(err) return parameter_error(request, str(err)) except InvalidName, err: msg = 'keys in document error: %s' % err _LOGGER.exception(msg) return internal_server_error(request, msg, sys.exc_info()) except Exception, err: _LOGGER.exception(err) return internal_server_error(request, err, sys.exc_info()) return _catch_except
from wechat.utils.exceptions import ParameterError, FormatterError, RequiredNoDefaultError, RequiredLackedError, MethodError _LOGGER = logging.getLogger('weibonews.wechat') def catch_except(func): """ Indicating a method process general exception. """ @wraps(func) def _catch_except(*args, **kwargs): """ catch exception raised by func """ try: request = args[0] return func(*args, **kwargs) except (FormatterError, RequiredNoDefaultError, RequiredLackedError, MethodError, ParameterError), err: _LOGGER.error(err) return parameter_error(request, str(err)) except InvalidName, err: msg = 'keys in document error: %s' % err _LOGGER.exception(msg) return internal_server_error(request, msg, sys.exc_info()) except Exception, err: _LOGGER.exception(err) return internal_server_error(request, err, sys.exc_info()) return _catch_except