Skip to content

Commit

Permalink
Automatic table creation and capacity parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemanja Trivic authored and Lxstr committed Mar 21, 2024
1 parent 5fb1b53 commit a160fc0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/flask_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def _get_interface(self, app):
SESSION_DYNAMODB_URL = config.get(
"SESSION_DYNAMODB_URL", Defaults.SESSION_DYNAMODB_URL
)
SESSION_DYNAMODB_READ = config.get(
"SESSION_DYNAMODB_READ", Defaults.SESSION_DYNAMODB_READ
)
SESSION_DYNAMODB_WRITE = config.get(
"SESSION_DYNAMODB_WRITE", Defaults.SESSION_DYNAMODB_WRITE
)

common_params = {
"app": app,
Expand Down Expand Up @@ -182,6 +188,8 @@ def _get_interface(self, app):
client=SESSION_DYNAMODB,
table_name=SESSION_DYNAMODB_TABLE,
url=SESSION_DYNAMODB_URL,
read_capacity=SESSION_DYNAMODB_READ,
write_capacity=SESSION_DYNAMODB_WRITE,
)

else:
Expand Down
2 changes: 2 additions & 0 deletions src/flask_session/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ class Defaults:
SESSION_DYNAMODB = None
SESSION_DYNAMODB_TABLE = "FlaskSession"
SESSION_DYNAMODB_URL = "http://localhost:8000"
SESSION_DYNAMODB_READ = 5
SESSION_DYNAMODB_WRITE = 5
22 changes: 21 additions & 1 deletion src/flask_session/dynamodb/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class DynamoDBSessionInterface(ServerSideSessionInterface):
:param sid_length: The length of the generated session id in bytes.
:param table_name: DynamoDB table name to store the session.
:param url: DynamoDB URL for local testing.
:param read_capacity: DynamoDB table read capacity units.
:param write_capacity: DynamoDB table write capacity units.
.. versionadded:: 0.6
The `sid_length` parameter was added.
Expand All @@ -54,23 +56,41 @@ def __init__(
serialization_format: str = Defaults.SESSION_SERIALIZATION_FORMAT,
table_name: str = Defaults.SESSION_DYNAMODB_TABLE,
url: str = Defaults.SESSION_DYNAMODB_URL,
read_capacity: int = Defaults.SESSION_DYNAMODB_READ,
write_capacity: int = Defaults.SESSION_DYNAMODB_WRITE,
):

if client is None:
warnings.warn(
"No valid DynamoDBServiceResource instance provided, attempting to create a new instance on localhost:8000.",
RuntimeWarning,
stacklevel=1,
)
client = boto3.resource("dynamodb", endpoint_url=url)

try:
client.create_table(
AttributeDefinitions=[
{"AttributeName": "id", "AttributeType": "S"},
],
TableName=table_name,
KeySchema=[
{"AttributeName": "id", "KeyType": "HASH"},
],
ProvisionedThroughput={
"ReadCapacityUnits": read_capacity,
"WriteCapacityUnits": write_capacity,
},
)

client.meta.client.update_time_to_live(
TableName=self.table_name,
TimeToLiveSpecification={
"Enabled": True,
"AttributeName": "expiration",
},
)
except AttributeError:
except (AttributeError, client.meta.client.exceptions.ResourceInUseException):
pass

self.client = client
Expand Down
22 changes: 11 additions & 11 deletions tests/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def setup_dynamodb(self):
self.client = boto3.resource(
"dynamodb", endpoint_url=Defaults.SESSION_DYNAMODB_URL
)
self.store = self.client.Table(Defaults.SESSION_DYNAMODB_TABLE)
try:
self.store = self.client.Table(Defaults.SESSION_DYNAMODB_TABLE)
scan = self.store.scan()
with self.store.batch_writer() as batch:
for each in scan.get("Items"):
Expand All @@ -28,17 +28,17 @@ def setup_dynamodb(self):
"id": each.get("id"),
}
)
yield
finally:
scan = self.store.scan()
with self.store.batch_writer() as batch:
for each in scan.get("Items"):
batch.delete_item(
Key={
"id": each.get("id"),
}
)
except self.client.meta.client.exceptions.ResourceNotFoundException:
pass
yield
scan = self.store.scan()
with self.store.batch_writer() as batch:
for each in scan.get("Items"):
batch.delete_item(
Key={
"id": each.get("id"),
}
)

def test_dynamodb_default(self, app_utils):
with self.setup_dynamodb():
Expand Down

0 comments on commit a160fc0

Please sign in to comment.