django.contrib.sessions.backends.signed_cookies: 35 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/sessions/backends/signed_cookies.py

Stats: 0 executed, 30 missed, 5 excluded, 58 ignored

  1. try:
  2. import cPickle as pickle
  3. except ImportError:
  4. import pickle
  5. from django.conf import settings
  6. from django.core import signing
  7. from django.contrib.sessions.backends.base import SessionBase
  8. class PickleSerializer(object):
  9. """
  10. Simple wrapper around pickle to be used in signing.dumps and
  11. signing.loads.
  12. """
  13. def dumps(self, obj):
  14. return pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
  15. def loads(self, data):
  16. return pickle.loads(data)
  17. class SessionStore(SessionBase):
  18. def load(self):
  19. """
  20. We load the data from the key itself instead of fetching from
  21. some external data store. Opposite of _get_session_key(),
  22. raises BadSignature if signature fails.
  23. """
  24. try:
  25. return signing.loads(self.session_key,
  26. serializer=PickleSerializer,
  27. max_age=settings.SESSION_COOKIE_AGE,
  28. salt='django.contrib.sessions.backends.signed_cookies')
  29. except (signing.BadSignature, ValueError):
  30. self.create()
  31. return {}
  32. def create(self):
  33. """
  34. To create a new key, we simply make sure that the modified flag is set
  35. so that the cookie is set on the client for the current request.
  36. """
  37. self.modified = True
  38. def save(self, must_create=False):
  39. """
  40. To save, we get the session key as a securely signed string and then
  41. set the modified flag so that the cookie is set on the client for the
  42. current request.
  43. """
  44. self._session_key = self._get_session_key()
  45. self.modified = True
  46. def exists(self, session_key=None):
  47. """
  48. This method makes sense when you're talking to a shared resource, but
  49. it doesn't matter when you're storing the information in the client's
  50. cookie.
  51. """
  52. return False
  53. def delete(self, session_key=None):
  54. """
  55. To delete, we clear the session key and the underlying data structure
  56. and set the modified flag so that the cookie is set on the client for
  57. the current request.
  58. """
  59. self._session_key = ''
  60. self._session_cache = {}
  61. self.modified = True
  62. def cycle_key(self):
  63. """
  64. Keeps the same data but with a new key. To do this, we just have to
  65. call ``save()`` and it will automatically save a cookie with a new key
  66. at the end of the request.
  67. """
  68. self.save()
  69. def _get_session_key(self):
  70. """
  71. Most session backends don't need to override this method, but we do,
  72. because instead of generating a random string, we want to actually
  73. generate a secure url-safe Base64-encoded string of data as our
  74. session key.
  75. """
  76. session_cache = getattr(self, '_session_cache', {})
  77. return signing.dumps(session_cache, compress=True,
  78. salt='django.contrib.sessions.backends.signed_cookies',
  79. serializer=PickleSerializer)