Guest User

Untitled

a guest
Aug 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. Can I use GAE python 2.7 django.utils.translation?
  2. from django.utils import translation
  3.  
  4. class I18NHandler(webapp.RequestHandler):
  5.  
  6. def initialize(self, request, response):
  7. webapp.RequestHandler.initialize(self, request, response)
  8. self.request.COOKIES = Cookies(self)
  9. self.request.META = os.environ
  10. self.reset_language()
  11.  
  12. def reset_language(self):
  13.  
  14. # Decide the language from Cookies/Headers
  15.  
  16. language = translation.get_language_from_request(self.request)
  17. translation.activate(language)
  18. self.request.LANGUAGE_CODE = translation.get_language()
  19.  
  20. # Set headers in response
  21.  
  22. self.response.headers['Content-Language'] =
  23. translation.get_language()
  24.  
  25.  
  26. class Cookies(UserDict.DictMixin):
  27.  
  28. def __init__(self, handler, **policy):
  29. self.response = handler.response
  30. self._in = handler.request.cookies
  31. self.policy = policy
  32. if 'secure' not in policy
  33. and handler.request.environ.get('HTTPS', '').lower()
  34. in ['on', 'true']:
  35. policy['secure'] = True
  36. self._out = {}
  37.  
  38. def __getitem__(self, key):
  39. if key in self._out:
  40. return self._out[key]
  41. if key in self._in:
  42. return self._in[key]
  43. raise KeyError(key)
  44.  
  45. def __setitem__(self, key, item):
  46. self._out[key] = item
  47. self.set_cookie(key, item, **self.policy)
  48.  
  49. def __contains__(self, key):
  50. return key in self._in or key in self._out
  51.  
  52. def keys(self):
  53. return self._in.keys() + self._out.keys()
  54.  
  55. def __delitem__(self, key):
  56. if key in self._out:
  57. del self._out[key]
  58. self.unset_cookie(key)
  59. if key in self._in:
  60. del self._in[key]
  61. p = {}
  62. if 'path' in self.policy:
  63. p['path'] = self.policy['path']
  64. if 'domain' in self.policy:
  65. p['domain'] = self.policy['domain']
  66. self.delete_cookie(key, **p)
  67.  
  68. # begin WebOb functions
  69.  
  70. def set_cookie(
  71. self,
  72. key,
  73. value='',
  74. max_age=None,
  75. path='/',
  76. domain=None,
  77. secure=None,
  78. httponly=False,
  79. version=None,
  80. comment=None,
  81. ):
  82. """
  83. Set (add) a cookie for the response
  84. """
  85.  
  86. cookies = BaseCookie()
  87. cookies[key] = value
  88. for (var_name, var_value) in [
  89. ('max-age', max_age),
  90. ('path', path),
  91. ('domain', domain),
  92. ('secure', secure),
  93. ('HttpOnly', httponly),
  94. ('version', version),
  95. ('comment', comment),
  96. ]:
  97. if var_value is not None and var_value is not False:
  98. cookies[key][var_name] = str(var_value)
  99. if max_age is not None:
  100. cookies[key]['expires'] = max_age
  101. header_value = cookies[key].output(header='').lstrip()
  102. self.response.headers._headers.append(('Set-Cookie',
  103. header_value))
  104.  
  105. def delete_cookie(
  106. self,
  107. key,
  108. path='/',
  109. domain=None,
  110. ):
  111. """
  112. Delete a cookie from the client. Note that path and domain must match
  113. how the cookie was originally set.
  114. This sets the cookie to the empty string, and max_age=0 so
  115. that it should expire immediately.
  116. """
  117.  
  118. self.set_cookie(key, '', path=path, domain=domain, max_age=0)
  119.  
  120. def unset_cookie(self, key):
  121. """
  122. Unset a cookie with the given name (remove it from the
  123. response). If there are multiple cookies (e.g., two cookies
  124. with the same name and different paths or domains), all such
  125. cookies will be deleted.
  126. """
  127.  
  128. existing = self.response.headers.get_all('Set-Cookie')
  129. if not existing:
  130. raise KeyError('No cookies at all have been set')
  131. del self.response.headers['Set-Cookie']
  132. found = False
  133. for header in existing:
  134. cookies = BaseCookie()
  135. cookies.load(header)
  136. if key in cookies:
  137. found = True
  138. del cookies[key]
  139. header = cookies.output(header='').lstrip()
  140. if header:
  141. self.response.headers.add('Set-Cookie', header)
  142. if not found:
  143. raise KeyError('No cookie has been set with the name %r'
  144. % key)
  145.  
  146. Traceback (most recent call last):
  147. File "/base/data/home/apps/s~montaoproject/main.354356023835363013/webapp2.py", line 545, in dispatch
  148. return method(*args, **kwargs)
  149. File "/base/data/home/apps/s~montaoproject/main.354356023835363013/i18n.py", line 781, in get
  150. cookie_django_language
  151. File "/base/data/home/apps/s~montaoproject/main.354356023835363013/util.py", line 65, in __setitem__
  152. self.set_cookie(key, item, **self.policy)
  153. File "/base/data/home/apps/s~montaoproject/main.354356023835363013/util.py", line 120, in set_cookie
  154. self.response.headers._headers.append(('Set-Cookie',
  155. AttributeError: ResponseHeaders instance has no attribute '_headers'
  156.  
  157. res.headers.add('Set-Cookie', header_value)
Add Comment
Please, Sign In to add comment