Skip to content
This repository was archived by the owner on Dec 28, 2017. It is now read-only.

Commit 9702897

Browse files
author
Greg Taylor
committed
Merge pull request #6 from bjinwright/master
Fix for the TypeError: Unsupported type "" for value "None" erros
2 parents 8fb4fae + d0d8661 commit 9702897

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
*.swp
33
*.tmp
44
*~
5+
*.pydevproject
6+
*.project
57
.idea
68
MANIFEST
79
build
810
dist
911
boto
12+
testapp/
13+
manage.py
1014
*.egg-info

dynamodb_sessions/backends/dynamodb.py

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import boto
55
from django.conf import settings
66
from django.contrib.sessions.backends.base import SessionBase, CreateError
7-
from django.utils.hashcompat import md5_constructor
7+
from django.core.exceptions import SuspiciousOperation
8+
89
from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError
910
from boto.exception import DynamoDBResponseError
1011

@@ -65,11 +66,11 @@ def load(self):
6566
:rtype: dict
6667
:returns: The de-coded session data, as a dict.
6768
"""
68-
logger.debug("Loading session data for: %s" % self.session_key)
69+
6970
try:
7071
item = self.table.get_item(self.session_key,
7172
consistent_read=ALWAYS_CONSISTENT)
72-
except DynamoDBKeyNotFoundError:
73+
except (DynamoDBKeyNotFoundError,SuspiciousOperation):
7374
self.create()
7475
return {}
7576

@@ -84,32 +85,26 @@ def exists(self, session_key):
8485
:returns: ``True`` if a session with the given key exists in the DB,
8586
``False`` if not.
8687
"""
87-
logger.debug("Session key exists?: %s" % session_key)
8888
key_already_exists = self.table.has_item(
8989
session_key,
9090
consistent_read=ALWAYS_CONSISTENT,
9191
)
9292
if key_already_exists:
93-
logger.debug(" - Yes, key exists.")
9493
return True
9594
else:
96-
logger.debug(" - No, key doesn't exist.")
9795
return False
9896

9997
def create(self):
10098
"""
10199
Creates a new entry in DynamoDB. This may or may not actually
102100
have anything in it.
103101
"""
104-
logger.debug("Creating a new session")
105102
while True:
106103
try:
107104
# Save immediately to ensure we have a unique entry in the
108105
# database.
109106
self.save(must_create=True)
110107
except CreateError:
111-
# Key wasn't unique. Try again.
112-
logger.debug(" - Key wasn't unique, trying again...")
113108
continue
114109
self.modified = True
115110
self._session_cache = {}
@@ -125,40 +120,30 @@ def save(self, must_create=False):
125120
:raises: ``CreateError`` if ``must_create`` is ``True`` and a session
126121
with the current session key already exists.
127122
"""
128-
# This base64 encodes session data.
129-
data = self.encode(self._get_session(no_load=must_create))
130-
123+
# If the save method is called with must_create equal to True, I'm
124+
# setting self._session_key equal to None and when
125+
# self.get_or_create_session_key is called the new
126+
# session_key will be created.
131127
if must_create:
132-
# Force the generation of a new session key.
133-
self._session_key = self._get_new_session_key()
134-
logger.debug(" - Saving new session: %s" % self.session_key)
135-
item = self.table.new_item(
136-
self.session_key,
137-
# Stuff the base64 encoded stuff into the 'data' attrib.
138-
attrs={
139-
'data': data,
140-
# This will be used for session expiration.
141-
'created': int(time.time()),
142-
}
143-
)
144-
try:
145-
# We expect the 'data' attribute to not exist.
146-
item.put(expected_value={'data': False})
147-
except DynamoDBResponseError:
148-
# There's already an item with this key.
149-
raise CreateError
128+
self._session_key = None
129+
130+
self._get_or_create_session_key()
131+
item = self.table.new_item(self.session_key)
132+
# Queue up a PUT operation for UpdateItem, which preserves the
133+
# existing 'created' attribute.
134+
item.put_attribute('data',self.encode(self._get_session(no_load=must_create)))
135+
136+
if must_create:
137+
138+
item.put_attribute('created',int(time.time()))
139+
# We expect the data value to be False because we are creating a
140+
# new session
141+
item.put(expected_value={'data': False})
150142
else:
151-
logger.debug("Saving existing session: %s" % self.session_key)
152-
# This isn't really creating a new item, just a container for
153-
# us to use put_attribute to queue an attrib update to.
154-
item = self.table.new_item(self.session_key)
155-
# Queue up a PUT operation for UpdateItem, which preserves the
156-
# existing 'created' attribute.
157-
item.put_attribute('data', data)
158143
# Commits the PUT UpdateItem for the 'data' attrib, meanwhile
159-
# leaving the 'created' attrib un-touched.
144+
# leaving the 'created' attrib un-touched.
160145
item.save()
161-
146+
162147
def delete(self, session_key=None):
163148
"""
164149
Deletes the current session, or the one specified in ``session_key``.
@@ -171,7 +156,6 @@ def delete(self, session_key=None):
171156
return
172157
session_key = self.session_key
173158

174-
logger.debug("Deleting session: %s" % session_key)
175159
key = self.table.layer2.build_key_from_values(
176160
self.table.schema,
177161
session_key,

dynamodb_sessions/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'''
2+
Created on Feb 13, 2013
3+
4+
@author: brian
5+
'''

dynamodb_sessions/tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Created on Feb 13, 2013
3+
4+
@author: brian
5+
'''
6+
from django.contrib.sessions.tests import SessionTestsMixin,DatabaseSessionTests
7+
from django.test import TestCase
8+
9+
from .backends.dynamodb import SessionStore as DynamoDBSession
10+
from .backends.cached_dynamodb import SessionStore as CachedDynamoDBSession
11+
12+
class DynamoDBTestCase(SessionTestsMixin,TestCase):
13+
14+
backend = DynamoDBSession
15+
16+
class CachedDynamoDBTestCase(SessionTestsMixin,TestCase):
17+
18+
backend = CachedDynamoDBSession
19+

0 commit comments

Comments
 (0)