This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created a web session contrib application. early days
- Loading branch information
quantmind
committed
Nov 12, 2010
1 parent
85de399
commit 830a7e0
Showing
11 changed files
with
114 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
from stdnet import structures | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from datetime import datetime | ||
|
||
from django.contrib.sessions.backends.base import SessionBase | ||
from django.conf import settings | ||
|
||
from stdnet import ObjectNotFound | ||
from stdnet.contrib.sessions.models import Session, session_settings | ||
|
||
session_settings['SECRET_KEY'] = settings.SECRET_KEY | ||
|
||
now = datetime.now | ||
|
||
|
||
class SessionStore(SessionBase): | ||
""" | ||
Implements Redis session store. | ||
""" | ||
|
||
def exists(self, session_key): | ||
if db.exists("session:%s" % session_key): | ||
return True | ||
return False | ||
|
||
def create(self): | ||
while True: | ||
self.session_key = self._get_new_session_key() | ||
try: | ||
self.save(must_create=True) | ||
except CreateError: | ||
# The key wasn't unique, try again | ||
continue | ||
self.modified = True | ||
return | ||
|
||
def save(self, must_create=False): | ||
if must_create: | ||
func = db.setnx | ||
else: | ||
func = db.set | ||
key = "session:%s" % self.session_key | ||
result = func( | ||
key, | ||
self.encode(self._get_session(no_load=must_create))) | ||
if must_create and result is None: | ||
raise CreateError | ||
# The key has been set, set its expiration | ||
db.expire(key, self.get_expiry_age()) | ||
|
||
def delete(self, session_key=None): | ||
if session_key is None: | ||
session_key = self.session_key | ||
db.delete("session:%s" % session_key) | ||
|
||
def load(self): | ||
session_data = db.get("session:%s" % self.session_key) | ||
if session_data is None: | ||
self.create() | ||
return {} | ||
return self.decode(session_data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import base64 | ||
import cPickle as pickle | ||
|
||
from stdnet import orm | ||
from django.utils.hashcompat import md5_constructor | ||
|
||
session_settings = {'SECRET_KEY': None} | ||
|
||
class SuspiciousOperation(Exception): | ||
pass | ||
|
||
|
||
class EncodedPickledObjectField(orm.CharField): | ||
|
||
def to_python(self, value): | ||
encoded_data = base64.decodestring(self.data) | ||
pickled, tamper_check = encoded_data[:-32], encoded_data[-32:] | ||
if md5_constructor(pickled + session_settings['SECRET_KEY']).hexdigest() != tamper_check: | ||
raise SuspiciousOperation("User tampered with session cookie.") | ||
try: | ||
return pickle.loads(pickled) | ||
except: | ||
return {} | ||
|
||
def serialise(self, value): | ||
pickled = pickle.dumps(session_dict) | ||
pickled_md5 = md5_constructor(pickled + session_settings['SECRET_KEY']).hexdigest() | ||
return base64.encodestring(pickled + pickled_md5) | ||
|
||
|
||
class Session(orm.StdModel): | ||
id = orm.SymbolField(primary_key=True) | ||
data = orm.PickleObjectField() | ||
expiry = orm.DateTimeField(index = False, required = False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters