PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/kitsune/twitter/__init__.py

https://gitlab.com/jslee1/kitsune
Python | 107 lines | 87 code | 11 blank | 9 comment | 4 complexity | 0db218428c7d1214e2ea32ba1648d3bd MD5 | raw file
  1. import logging
  2. from django import http
  3. from django.conf import settings
  4. from twython import Twython
  5. log = logging.getLogger('k')
  6. PREFIX = 'custcare_'
  7. REDIRECT_NAME = PREFIX + 'redirect'
  8. REQUEST_KEY_NAME = PREFIX + 'request_key'
  9. REQUEST_SECRET_NAME = PREFIX + 'request_secret'
  10. MAX_AGE = 3600
  11. def url(request, override=None):
  12. d = {
  13. 'scheme': 'https' if request.is_secure() else 'http',
  14. 'host': request.get_host(),
  15. 'path': request.get_full_path(),
  16. }
  17. if override:
  18. d.update(override)
  19. return u'%s://%s%s' % (d['scheme'], d['host'], d['path'])
  20. def auth_wanted(view_func):
  21. """Twitter sessions are SSL only, so redirect to SSL if needed.
  22. Don't redirect if TWITTER_COOKIE_SECURE is False.
  23. """
  24. def wrapper(request, *args, **kwargs):
  25. is_secure = settings.TWITTER_COOKIE_SECURE
  26. if (request.COOKIES.get(REDIRECT_NAME) and
  27. (is_secure and not request.is_secure())):
  28. ssl_url = url(
  29. request,
  30. {'scheme': 'https' if is_secure else 'http'})
  31. return http.HttpResponseRedirect(ssl_url)
  32. return view_func(request, *args, **kwargs)
  33. return wrapper
  34. def auth_required(view_func):
  35. """Return a HttpResponseBadRequest if not authed."""
  36. def wrapper(request, *args, **kwargs):
  37. if not request.twitter.authed:
  38. return http.HttpResponseBadRequest()
  39. return view_func(request, *args, **kwargs)
  40. return wrapper
  41. class Session(object):
  42. key_key = 'twitter_oauth_key'
  43. key_secret = 'twitter_oauth_secret'
  44. @property
  45. def authed(self):
  46. return bool(self.key and self.secret)
  47. def __init__(self, key=None, secret=None):
  48. self.key = key
  49. self.secret = secret
  50. @classmethod
  51. def from_request(cls, request):
  52. s = cls()
  53. s.key = request.session.get(s.key_key)
  54. s.secret = request.session.get(s.key_secret)
  55. return s
  56. def delete(self, request, response):
  57. response.delete_cookie(REDIRECT_NAME)
  58. if self.key_key in request.session:
  59. del request.session[self.key_key]
  60. if self.key_secret in request.session:
  61. del request.session[self.key_secret]
  62. self.key = None
  63. self.secret = None
  64. def save(self, request, response):
  65. request.session[self.key_key] = self.key
  66. request.session[self.key_secret] = self.secret
  67. response.set_cookie(REDIRECT_NAME, '1', max_age=MAX_AGE)
  68. def get_twitter_api(
  69. access_token=settings.TWITTER_ACCESS_TOKEN,
  70. access_token_secret=settings.TWITTER_ACCESS_TOKEN_SECRET):
  71. return Twython(
  72. settings.TWITTER_CONSUMER_KEY,
  73. settings.TWITTER_CONSUMER_SECRET,
  74. access_token,
  75. access_token_secret,
  76. client_args={
  77. # 10 second connect timeout, and 10 second read timeout.
  78. # This is passed to the requests library.
  79. 'timeout': 10,
  80. })