Skip to content

Commit

Permalink
feat: Add delete method and remove session_key attr in DataModel
Browse files Browse the repository at this point in the history
  • Loading branch information
MissterHao committed Feb 3, 2023
1 parent a3c1217 commit 2f7ce97
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
python -m pip install --upgrade pip
pip install setuptools wheel twine
python setup.py sdist bdist_wheel
cp dist/django-dysession-${{ env.STRIP_RELEASE_VERSION }}.tar.gz dist/django-dysession.tar.gz
- name: Archive wheel artifacts file
uses: actions/upload-artifact@v3
Expand Down
39 changes: 32 additions & 7 deletions dysession/aws/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def insert_session_item(
insert_item = {pk: data.session_key}
for key in data:
insert_item[key] = data[key]
print(key, data[key])

response = table.put_item(
TableName=table_name,
Expand All @@ -147,6 +148,32 @@ def insert_session_item(
return response


def delete_session_item(
data: SessionDataModel,
table_name: Optional[str] = None,
) -> bool:
"""Delete a session key"""

assert type(data.session_key) is str, "session_key should be string type"

if table_name is None:
table_name = get_config()["DYNAMODB_TABLENAME"]

resource = boto3.resource("dynamodb", region_name=get_config()["DYNAMODB_REGION"])
table = resource.Table(table_name)
pk = get_config()["PARTITION_KEY_NAME"]

insert_item = {pk: data.session_key}
for key in data:
insert_item[key] = data[key]

response = table.delete_item(
Key={pk: data.session_key},
)

return response


class DynamoDB:
def __init__(self, client=None) -> None:
self.client = client
Expand Down Expand Up @@ -207,10 +234,8 @@ def exists(self, session_key: str) -> bool:

return key_exists(session_key=session_key)

def delete(self, session_key: Optional[str]) -> bool:
if type(session_key) is not str:
raise TypeError(
f"session_key should be type of str instead of {type(session_key)}."
)

return True
def delete(self, data: SessionDataModel, table_name: Optional[str] = None) -> bool:
try:
delete_session_item(data=data, table_name=table_name)
except AssertionError:
pass
11 changes: 7 additions & 4 deletions dysession/backends/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ def delete(self, session_key=None):
Delete the session data under this key. If the key is None, use the
current session key value.
"""
if session_key is None:
session_key = self._session_key

print(f"On Delete: session_key: {session_key}")
self.db.delete(self._get_session())

try:
if session_key is None:
session_key = self._session_key
print(f"On Delete: session_key: {session_key}")
self.db.delete(session_key=session_key)
pass
except DeleteSessionError:
print(f"On Delete Error: session_key: {session_key}")

Expand Down
19 changes: 13 additions & 6 deletions dysession/backends/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
from typing import Any, Optional

from dysession.settings import get_config


class SessionDataModel:

Expand All @@ -11,8 +13,8 @@ def __init__(self, session_key: Optional[str] = None) -> None:
if type(session_key) is not str and session_key is not None:
raise TypeError("session_key should be type str or None")

self.session_key = session_key
self.__variables_names = set(["session_key"])
self.__variables_names = set([])
self[get_config()["PARTITION_KEY_NAME"]] = session_key

def __getitem__(self, key) -> Any:
# Set SESSION_EXPIRE_AT_BROWSER_CLOSE to False
Expand All @@ -27,10 +29,15 @@ def __getitem__(self, key) -> Any:
raise KeyError
raise

def __setitem__(self, key, value):
# if key == "session_key":
# raise ValueError()
def __get_session_key(self):
return self[get_config()["PARTITION_KEY_NAME"]]

def __set_session_key(self, value: Any):
self[get_config()["PARTITION_KEY_NAME"]] = value

session_key = property(__get_session_key, __set_session_key)

def __setitem__(self, key, value):
setattr(self, key, value)
self.__variables_names.add(key)

Expand All @@ -42,7 +49,7 @@ def __iter__(self):
return iter(self.__variables_names)

def __is_empty(self):
return "session_key" in self.__variables_names and len(self.__variables_names) == 1
return len(self.__variables_names) == 0

is_empty = property(__is_empty)

Expand Down
2 changes: 1 addition & 1 deletion dysession/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.0.2
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ description = "django-dysession is a django extension which integrated AWS Dynam
long_description = file: README.md
long_description_content_type = text/markdown
keywords = "django,session,aws,dynamodb"
url = https://github.com/MissterHao/django-dysession/releases/download/v1.0.1/django-dysession-1.0.1.tar.gz
download_url = https://github.com/MissterHao/django-dysession/releases/download/v1.0.1/django-dysession-1.0.1.tar.gz
url = https://github.com/MissterHao/django-dysession/releases/latest/download/django-dysession.tar.gz
download_url = https://github.com/MissterHao/django-dysession/releases/latest/download/django-dysession.tar.gz
author = "Hao-Wei, Li"
author_email = "[email protected]"
maintainer = "MissterHao"
Expand Down

0 comments on commit 2f7ce97

Please sign in to comment.