PageRenderTime 44ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/sessions/models.py

https://code.google.com/p/mango-py/
Python | 57 lines | 38 code | 6 blank | 13 comment | 2 complexity | cb4c6ece3759913b83feb2a394db35da MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import base64
  2. import cPickle as pickle
  3. from django.db import models
  4. from django.utils.translation import ugettext_lazy as _
  5. class SessionManager(models.Manager):
  6. def encode(self, session_dict):
  7. """
  8. Returns the given session dictionary pickled and encoded as a string.
  9. """
  10. return SessionStore().encode(session_dict)
  11. def save(self, session_key, session_dict, expire_date):
  12. s = self.model(session_key, self.encode(session_dict), expire_date)
  13. if session_dict:
  14. s.save()
  15. else:
  16. s.delete() # Clear sessions with no data.
  17. return s
  18. class Session(models.Model):
  19. """
  20. Django provides full support for anonymous sessions. The session
  21. framework lets you store and retrieve arbitrary data on a
  22. per-site-visitor basis. It stores data on the server side and
  23. abstracts the sending and receiving of cookies. Cookies contain a
  24. session ID -- not the data itself.
  25. The Django sessions framework is entirely cookie-based. It does
  26. not fall back to putting session IDs in URLs. This is an intentional
  27. design decision. Not only does that behavior make URLs ugly, it makes
  28. your site vulnerable to session-ID theft via the "Referer" header.
  29. For complete documentation on using Sessions in your code, consult
  30. the sessions documentation that is shipped with Django (also available
  31. on the Django Web site).
  32. """
  33. session_key = models.CharField(_('session key'), max_length=40,
  34. primary_key=True)
  35. session_data = models.TextField(_('session data'))
  36. expire_date = models.DateTimeField(_('expire date'), db_index=True)
  37. objects = SessionManager()
  38. class Meta:
  39. db_table = 'django_session'
  40. verbose_name = _('session')
  41. verbose_name_plural = _('sessions')
  42. def get_decoded(self):
  43. return SessionStore().decode(self.session_data)
  44. # At bottom to avoid circular import
  45. from django.contrib.sessions.backends.db import SessionStore