+

:4736 Function create_function() is deprecated [8192]

Last commit for mycoserver/wsgi_helpers.py: 7004713412b51ec58cf01149ba736fadf769acaf

Initial commit

Benjamin Renard [2014-01-12 00:33:07]
Initial commit
  1. # -*- coding: utf-8 -*-
  2.  
  3.  
  4. import collections
  5. import json
  6.  
  7. from markupsafe import Markup
  8. from webhelpers.html import tags
  9. import webob.dec
  10. import webob.exc
  11.  
  12. from . import templates
  13.  
  14.  
  15. N_ = lambda message: message
  16.  
  17.  
  18. errors_explanation = {
  19. 400: N_("Request is faulty"),
  20. 401: N_("Access is restricted to authorized persons."),
  21. 403: N_("Access is forbidden."),
  22. 404: N_("The requested page was not found."),
  23. }
  24. errors_message = {
  25. 401: N_("You must login to access this page."),
  26. }
  27. errors_title = {
  28. 400: N_("Unable to Access"),
  29. 401: N_("Access Denied"),
  30. 403: N_("Access Denied"),
  31. 404: N_("Unable to Access"),
  32. }
  33.  
  34.  
  35. def bad_request(ctx, **kw):
  36. return error(ctx, 400, **kw)
  37.  
  38.  
  39. def discard_empty_items(data):
  40. if isinstance(data, collections.Mapping):
  41. # Use type(data) to keep OrderedDicts.
  42. data = type(data)(
  43. (name, discard_empty_items(value))
  44. for name, value in data.iteritems()
  45. if value is not None
  46. )
  47. return data
  48.  
  49.  
  50. def error(ctx, code, **kw):
  51. response = webob.exc.status_map[code](headers=kw.pop('headers', None))
  52. if code != 204: # No content
  53. body = kw.pop('body', None)
  54. if body is None:
  55. template_path = kw.pop('template_path', '/http-error.mako')
  56. explanation = kw.pop('explanation', None)
  57. if explanation is None:
  58. explanation = errors_explanation.get(code)
  59. explanation = ctx._(explanation) if explanation is not None else response.explanation
  60. message = kw.pop('message', None)
  61. if message is None:
  62. message = errors_message.get(code)
  63. if message is not None:
  64. message = ctx._(message)
  65. comment = kw.pop('comment', None)
  66. if isinstance(comment, dict):
  67. comment = tags.ul(u'{0} : {1}'.format(key, value) for key, value in comment.iteritems())
  68. elif isinstance(comment, list):
  69. comment = tags.ul(comment)
  70. title = kw.pop('title', None)
  71. if title is None:
  72. title = errors_title.get(code)
  73. title = ctx._(title) if title is not None else response.status
  74. body = templates.render(ctx, template_path,
  75. comment=comment,
  76. explanation=explanation,
  77. message=message,
  78. response=response,
  79. title=title,
  80. **kw)
  81. response.body = body.encode('utf-8') if isinstance(body, unicode) else body
  82. return response
  83.  
  84.  
  85. def forbidden(ctx, **kw):
  86. return error(ctx, 403, **kw)
  87.  
  88.  
  89. def method_not_allowed(ctx, **kw):
  90. return error(ctx, 405, **kw)
  91.  
  92.  
  93. def no_content(ctx, headers=None):
  94. return error(ctx, 204, headers=headers)
  95.  
  96.  
  97. def not_found(ctx, **kw):
  98. return error(ctx, 404, **kw)
  99.  
  100.  
  101. def redirect(ctx, code=302, location=None, **kw):
  102. assert location is not None
  103. location_str = location.encode('utf-8') if isinstance(location, unicode) else location
  104. response = webob.exc.status_map[code](headers=kw.pop('headers', None), location=location_str)
  105. body = kw.pop('body', None)
  106. if body is None:
  107. template_path = kw.pop('template_path', '/http-error.mako')
  108. explanation = kw.pop('explanation', None)
  109. if explanation is None:
  110. explanation = Markup(u'{0} <a href="{1}">{1}</a>.').format(ctx._(u"You'll be redirected to page"), location)
  111. message = kw.pop('message', None)
  112. if message is None:
  113. message = errors_message.get(code)
  114. if message is not None:
  115. message = ctx._(message)
  116. title = kw.pop('title', None)
  117. if title is None:
  118. title = ctx._("Redirection in progress...")
  119. body = templates.render(ctx, template_path,
  120. comment=kw.pop('comment', None),
  121. explanation=explanation,
  122. message=message,
  123. response=response,
  124. title=title,
  125. **kw)
  126. response.body = body.encode('utf-8') if isinstance(body, unicode) else body
  127. return response
  128.  
  129.  
  130. def respond_json(ctx, data, code=None, headers=None, jsonp=None):
  131. """Return a JSON response.
  132.  
  133. This function is optimized for JSON following
  134. `Google JSON Style Guide <http://google-styleguide.googlecode.com/svn/trunk/jsoncstyleguide.xml>`_, but will handle
  135. any JSON except for HTTP errors.
  136. """
  137. if isinstance(data, collections.Mapping):
  138. # Remove null properties as recommended by Google JSON Style Guide.
  139. data = discard_empty_items(data)
  140. error = data.get('error')
  141. else:
  142. error = None
  143. if headers is None:
  144. headers = []
  145. if jsonp:
  146. headers.append(('Content-Type', 'application/javascript; charset=utf-8'))
  147. else:
  148. headers.append(('Content-Type', 'application/json; charset=utf-8'))
  149. if error:
  150. code = code or error['code']
  151. assert isinstance(code, int)
  152. response = webob.exc.status_map[code](headers=headers)
  153. if error.get('code') is None:
  154. error['code'] = code
  155. if error.get('message') is None:
  156. error['message'] = response.title
  157. else:
  158. response = ctx.req.response
  159. if code is not None:
  160. response.status = code
  161. response.headers.update(headers)
  162. text = unicode(json.dumps(data, encoding='utf-8', ensure_ascii=False, indent=2, sort_keys=True))
  163. if jsonp:
  164. text = u'{0}({1})'.format(jsonp, text)
  165. response.text = text
  166. return response
  167.  
  168.  
  169. def unauthorized(ctx, **kw):
  170. return error(ctx, 401, **kw)