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

Commit 0efddd1

Browse files
author
Rense VanderHoek
committed
Added support for a different AWS region.
1 parent 9702897 commit 0efddd1

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The following settings may be used in your ``settings.py``:
9292
to use for DynamoDB.
9393
:DYNAMODB_SESSIONS_AWS_SECRET_ACCESS_KEY: The secret for the AWS account
9494
to use for DynamoDB.
95+
:DYNAMODB_SESSIONS_AWS_REGION_NAME: The region to use for DynamoDB.
9596

9697
Changes
9798
-------

dynamodb_sessions/backends/dynamodb.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@
2727
if not AWS_SECRET_ACCESS_KEY:
2828
AWS_SECRET_ACCESS_KEY = getattr(settings, 'AWS_SECRET_ACCESS_KEY')
2929

30+
AWS_REGION_NAME = getattr(settings, 'DYNAMODB_SESSIONS_AWS_REGION_NAME', False)
31+
if not AWS_REGION_NAME:
32+
AWS_REGION_NAME = getattr(settings, 'AWS_REGION_NAME', 'us-east-1')
33+
3034
# We'll find some better way to do this.
3135
_DYNAMODB_CONN = None
3236

3337
logger = logging.getLogger(__name__)
3438

39+
3540
def dynamodb_connection_factory():
3641
"""
3742
Since SessionStore is called for every single page view, we'd be
@@ -45,10 +50,12 @@ def dynamodb_connection_factory():
4550
logger.debug("Creating a DynamoDB connection.")
4651
_DYNAMODB_CONN = boto.connect_dynamodb(
4752
aws_access_key_id=AWS_ACCESS_KEY_ID,
48-
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
53+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
54+
region=AWS_REGION_NAME
4955
)
5056
return _DYNAMODB_CONN
5157

58+
5259
class SessionStore(SessionBase):
5360
"""
5461
Implements DynamoDB session store.
@@ -66,7 +73,7 @@ def load(self):
6673
:rtype: dict
6774
:returns: The de-coded session data, as a dict.
6875
"""
69-
76+
7077
try:
7178
item = self.table.get_item(self.session_key,
7279
consistent_read=ALWAYS_CONSISTENT)
@@ -120,30 +127,30 @@ def save(self, must_create=False):
120127
:raises: ``CreateError`` if ``must_create`` is ``True`` and a session
121128
with the current session key already exists.
122129
"""
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
130+
# If the save method is called with must_create equal to True, I'm
131+
# setting self._session_key equal to None and when
132+
# self.get_or_create_session_key is called the new
126133
# session_key will be created.
127134
if must_create:
128135
self._session_key = None
129-
136+
130137
self._get_or_create_session_key()
131138
item = self.table.new_item(self.session_key)
132139
# Queue up a PUT operation for UpdateItem, which preserves the
133140
# existing 'created' attribute.
134141
item.put_attribute('data',self.encode(self._get_session(no_load=must_create)))
135-
142+
136143
if must_create:
137-
144+
138145
item.put_attribute('created',int(time.time()))
139146
# We expect the data value to be False because we are creating a
140147
# new session
141148
item.put(expected_value={'data': False})
142149
else:
143150
# Commits the PUT UpdateItem for the 'data' attrib, meanwhile
144-
# leaving the 'created' attrib un-touched.
151+
# leaving the 'created' attrib un-touched.
145152
item.save()
146-
153+
147154
def delete(self, session_key=None):
148155
"""
149156
Deletes the current session, or the one specified in ``session_key``.

0 commit comments

Comments
 (0)