Ticket #5549: ticket_5549__rev_6814.diff
File ticket_5549__rev_6814.diff, 4.1 KB (added by , 17 years ago) |
---|
-
django/contrib/sessions/models.py
1 import os2 import sys3 import time4 import datetime5 import base646 import md57 import random8 import cPickle as pickle9 10 1 from django.db import models 11 2 from django.utils.translation import ugettext_lazy as _ 12 from django.conf import settings13 3 14 class SessionManager(models.Manager):15 def encode(self, session_dict):16 "Returns the given session dictionary pickled and encoded as a string."17 pickled = pickle.dumps(session_dict)18 pickled_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()19 return base64.encodestring(pickled + pickled_md5)20 21 def save(self, session_key, session_dict, expire_date):22 s = self.model(session_key, self.encode(session_dict), expire_date)23 if session_dict:24 s.save()25 else:26 s.delete() # Clear sessions with no data.27 return s28 29 4 class Session(models.Model): 30 5 """ 31 Django provides full support for anonymous sessions. The session 32 framework lets you store and retrieve arbitrary data on a 33 per-site-visitor basis. It stores data on the server side and 34 abstracts the sending and receiving of cookies. Cookies contain a 35 session ID -- not the data itself. 6 Saves sessions in database. 36 7 37 The Django sessions framework is entirely cookie-based. It does 38 not fall back to putting session IDs in URLs. This is an intentional 39 design decision. Not only does that behavior make URLs ugly, it makes 40 your site vulnerable to session-ID theft via the "Referer" header. 8 Not for direct usage, please use SessionStore class 9 in django.contrib.sessions.backends.db module to access sessions. 41 10 42 11 For complete documentation on using Sessions in your code, consult 43 12 the sessions documentation that is shipped with Django (also available … … 46 15 session_key = models.CharField(_('session key'), max_length=40, primary_key=True) 47 16 session_data = models.TextField(_('session data')) 48 17 expire_date = models.DateTimeField(_('expire date')) 49 objects = SessionManager()50 18 51 19 class Meta: 52 20 db_table = 'django_session' 53 21 verbose_name = _('session') 54 22 verbose_name_plural = _('sessions') 55 56 def get_decoded(self):57 encoded_data = base64.decodestring(self.session_data)58 pickled, tamper_check = encoded_data[:-32], encoded_data[-32:]59 if md5.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check:60 from django.core.exceptions import SuspiciousOperation61 raise SuspiciousOperation, "User tampered with session cookie."62 try:63 return pickle.loads(pickled)64 # Unpickling can cause a variety of exceptions. If something happens,65 # just return an empty dictionary (an empty session).66 except:67 return {} -
docs/sessions.txt
202 202 >>> s = SessionStore(session_key='2b1189a188b44ad18c35e113ac6ceead') 203 203 >>> s['last_login'] = datetime.datetime(2005, 8, 20, 13, 35, 10) 204 204 >>> s['last_login'] 205 datetime.datetime(2005, 8, 20, 13, 35, 0)205 datetime.datetime(2005, 8, 20, 13, 35, 10) 206 206 >>> s.save() 207 207 208 208 If you're using the ``django.contrib.sessions.backends.db`` backend, each … … 215 215 >>> s.expire_date 216 216 datetime.datetime(2005, 8, 20, 13, 35, 12) 217 217 218 Note that you'll need to call ``get_decoded()`` to get the session dictionary. 218 Note that you'll need to use the ``SessionStore`` class 219 in ``django.contrib.sessions.backends.db`` to get the session dictionary. 219 220 This is necessary because the dictionary is stored in an encoded format:: 220 221 221 222 >>> s.session_data 222 223 'KGRwMQpTJ19hdXRoX3VzZXJfaWQnCnAyCkkxCnMuMTExY2ZjODI2Yj...' 223 >>> s.get_decoded()224 {'user_id': 42}225 224 226 225 When sessions are saved 227 226 =======================