PageRenderTime 34ms CodeModel.GetById 21ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/django/contrib/sessions/models.py

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