You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

52 lines
1.6 KiB

  1. """
  2. This module allows importing AbstractBaseSession even
  3. when django.contrib.sessions is not in INSTALLED_APPS.
  4. """
  5. from __future__ import unicode_literals
  6. from django.db import models
  7. from django.utils.encoding import python_2_unicode_compatible
  8. from django.utils.translation import ugettext_lazy as _
  9. class BaseSessionManager(models.Manager):
  10. def encode(self, session_dict):
  11. """
  12. Return the given session dictionary serialized and encoded as a string.
  13. """
  14. session_store_class = self.model.get_session_store_class()
  15. return session_store_class().encode(session_dict)
  16. def save(self, session_key, session_dict, expire_date):
  17. s = self.model(session_key, self.encode(session_dict), expire_date)
  18. if session_dict:
  19. s.save()
  20. else:
  21. s.delete() # Clear sessions with no data.
  22. return s
  23. @python_2_unicode_compatible
  24. class AbstractBaseSession(models.Model):
  25. session_key = models.CharField(_('session key'), max_length=40, primary_key=True)
  26. session_data = models.TextField(_('session data'))
  27. expire_date = models.DateTimeField(_('expire date'), db_index=True)
  28. objects = BaseSessionManager()
  29. class Meta:
  30. abstract = True
  31. verbose_name = _('session')
  32. verbose_name_plural = _('sessions')
  33. def __str__(self):
  34. return self.session_key
  35. @classmethod
  36. def get_session_store_class(cls):
  37. raise NotImplementedError
  38. def get_decoded(self):
  39. session_store_class = self.get_session_store_class()
  40. return session_store_class().decode(self.session_data)