From 6c8ee9a2fd0897a97c0e5aa93e3af2cb42e0436a Mon Sep 17 00:00:00 2001 From: MissterHao Date: Thu, 2 Feb 2023 16:48:22 +0800 Subject: [PATCH 1/8] update README.md --- MANIFEST.in | 9 +- Makefile | 3 +- README.md | 130 +++++++++++++----- .../management/commands/dysession_clear.py | 28 ---- dysession/settings.py | 4 +- setup.cfg | 19 ++- 6 files changed, 119 insertions(+), 74 deletions(-) delete mode 100644 dysession/management/commands/dysession_clear.py diff --git a/MANIFEST.in b/MANIFEST.in index 4cf3920..ed677e8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,8 @@ include LICENSE -include README* -recursive-include django-dysession *.py \ No newline at end of file +include README.md +include MANIFEST.in +recursive-include django-dysession *.py +graft dysession +graft tests +global-exclude __pycache__ +global-exclude *.py[co] \ No newline at end of file diff --git a/Makefile b/Makefile index 29cac4c..af9f89d 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,8 @@ coverage: .PHONY: release release: - python3 setup.py bdist + python setup.py sdist bdist_wheel + twine check dist/* clean: find . -type f -name *.pyc -delete diff --git a/README.md b/README.md index c990e2b..ff10ca5 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,56 @@
-

django-dysession

-

- django-dysessino is a django extension by using AWS DynamoDB as a session backend -

- django-dysession - -

- - - codecov - - - Supported Python version badge - -
- - - Github Issue badge - - - Lience badge - - - Downloads badge - -
-

+

django-dysession

+

django-dysession is a django extension by using AWS DynamoDB as a session backend

+django-dysession + +

+ + +codecov + + +Supported Python version badge + +
+ + +Github Issue badge + + +Lience badge + + +Downloads badge + +
+

-## What is a django-session? +## What is a django-dysession? + +Django-dysession is a simple and easy-to-use app which allow Django developers to take DyanmoDB as Session Backend Database. + ++ Easy to use! All you need is add two lines of Code! ++ Support ttl attribute + Django's default session won't delete expired session data. + By using DynamoDB, we can take advantage of DynamoDB's ttl attrubute to auto delete expired session data. ++ + ## Requirements +django-dysession use `boto3` to communicate with DynamoDB. +Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of services like **DynamoDB**. -## Installation ++ Django >= 3.2 ++ boto3 >= 1.26.59 -## Example +## Installation +## Getting Started +First of all, add `dysession` into `INSTALLED_APPS` in settings.py. +Change `SESSION_ENGINE` to `dysession.backends.db` in order use our SessionStore. ```python INSTALLED_APPS = [ ... @@ -51,4 +60,55 @@ INSTALLED_APPS = [ ] SESSION_ENGINE = "dysession.backends.db" -``` \ No newline at end of file +``` + +Second, we need to create a DynamoDB to store session data which's name is `sessions` as default. + + +Run the commands bellow in cmd. + +## Django Commands + +django-dysession offer two commands for developers: ++ dysession_destory: Destory DynamoDB Table ( Will delete whole data of the table ) ++ dysession_init: Create DyanmoDB Table + +```bash +python manage.py --help + +Type 'manage.py help ' for help on a specific subcommand. + +Available subcommands: + +[dysession] + dysession_destory + dysession_init +``` + +## Settings + +This section outlines all the settings and configurations that you can put in Django's settings.py to adjust `dysession`'s behavior. + +You can overwrite any value in `DYSESSION` or just ignore it to use the default value! +```python +DYSESSION = { + "DYNAMODB_TABLENAME": "sessions", + "PARTITION_KEY_NAME": "PK", + "SORT_KEY_NAME": "SK", + "TTL_ATTRIBUTE_NAME": "ttl", + "CACHE_PERIOD": 3600, + "DYNAMODB_REGION": "ap-northeast-1", +} +``` + + +| Argument | Default | Description | +| ------------------ | -------------- | ------------------------------------------------------------ | +| DYNAMODB_TABLENAME | sessions | DynamoDB table name | +| PARTITION_KEY_NAME | PK | Partition key name | +| TTL_ATTRIBUTE_NAME | ttl | Time to live attribute name | +| CACHE_PERIOD | 3600 | Define how long should be the cache live in DynamoDB's table | +| DYNAMODB_REGION | ap-northeast-1 | The region of the DynamoDB table | + + + diff --git a/dysession/management/commands/dysession_clear.py b/dysession/management/commands/dysession_clear.py deleted file mode 100644 index 4a1d29c..0000000 --- a/dysession/management/commands/dysession_clear.py +++ /dev/null @@ -1,28 +0,0 @@ -from typing import Any, Optional -from django.core.management.base import BaseCommand -from django.core.management.base import CommandParser - - -__all__ = ["Command"] - - -class Command(BaseCommand): - - help = "Clear all session record which stored in DynamoDB" - - def add_arguments(self, parser: CommandParser) -> None: - parser.add_argument( - "-u", - "--uid", - action="append", - help=" Indicate to clear specified user's session data.", - required=False, - ) - return super().add_arguments(parser) - - def handle(self, *args: Any, **options: Any) -> Optional[str]: - userids = options.get("uid", None) - if userids: - ... - return - diff --git a/dysession/settings.py b/dysession/settings.py index 7b48c5d..40a76a3 100644 --- a/dysession/settings.py +++ b/dysession/settings.py @@ -31,14 +31,14 @@ def get_config() -> Dict[str, Union[str, int]]: Dict[str, Union[str, int]] """ config = DEFAULT_CONFIG.copy() - custom_config = getattr(settings, "DYSESSION_CONFIG", {}) + custom_config = getattr(settings, "DYSESSION", {}) config.update(custom_config) return config @receiver(setting_changed) def update_dysession_config(*, setting, **kwargs): - if setting == "DYSESSION_CONFIG": # pragma: no cover + if setting == "DYSESSION": # pragma: no cover get_config.cache_clear() # pragma: no cover diff --git a/setup.cfg b/setup.cfg index 943c9d8..bba155e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,21 +1,26 @@ [metadata] name = django-dysession -version = 0.0.1 -description = django-dysession is a django extension by using AWS DynamoDB as a session backend -long_description = file:README.md +version = 1.0.0 +description = "django-dysession is a django extension by using AWS DynamoDB as a session backend" +long_description = file: README.md +long_description_content_type = "text/markdown" keywords = "django,session,aws,dynamodb" -url = https://github.com/MissterHao/django-dysession +url = "https://github.com/MissterHao/django-dysession/" download_url = "https://pypi.org/project/django-dysession/" author = "Hao-Wei, Li" author_email = "henryliking@gmail.com" maintainer = "MissterHao" maintainer_email = "henryliking@gmail.com" license = MIT +license_file = LICENSE classifiers = Development Status :: 4 - Beta Environment :: Web Environment Framework :: Django Framework :: Django :: 3.2 + Framework :: Django :: 4 + Framework :: Django :: 4.0 + Framework :: Django :: 4.1 Intended Audience :: Developers License :: OSI Approved :: MIT License Operating System :: OS Independent @@ -24,11 +29,12 @@ classifiers = Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.6 - Programming Language :: Python :: Implementation :: CPython + Programming Language :: Python :: Implementation Topic :: Software Development :: Libraries :: Application Frameworks Topic :: Software Development :: Libraries :: Python Modules Topic :: Internet :: WWW/HTTP :: Session Topic :: Security + Topic :: Database [options] packages=find: @@ -37,4 +43,5 @@ python_requires = >=3.6 setup_requires = setuptools >= 38.3.0 install_requires = - Django>=3.2 \ No newline at end of file + Django>=3.2 + boto3>=1.26.59 \ No newline at end of file From 477c6dd3d693604f9e2a01ce79f454ffc8ac2098 Mon Sep 17 00:00:00 2001 From: HaoWei Date: Fri, 3 Feb 2023 02:04:32 +0800 Subject: [PATCH 2/8] Update README.md --- README.md | 23 +++++++++++++++++++++-- setup.cfg | 6 +++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ff10ca5..d07a425 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Django-dysession is a simple and easy-to-use app which allow Django developers t + Support ttl attribute Django's default session won't delete expired session data. By using DynamoDB, we can take advantage of DynamoDB's ttl attrubute to auto delete expired session data. -+ ++ Taking advantage of AWS serverless service! ( No more effort to maintain ) ## Requirements @@ -47,6 +47,10 @@ Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python ## Installation +Install from PyPI ( or manually download from PyPI): +```bash +pip install django-dysession +``` ## Getting Started First of all, add `dysession` into `INSTALLED_APPS` in settings.py. @@ -63,9 +67,24 @@ SESSION_ENGINE = "dysession.backends.db" ``` Second, we need to create a DynamoDB to store session data which's name is `sessions` as default. +Run the commands bellow in cmd. +```bash +python manage.py dysession_init +``` +Then, we can enjoy it now! +```python +from django.http import HttpResponse + +def mainpage(request): + request.session["is_admin"] = True + request.session["bottle_of_milks"] = 20 + request.session["planet_have_been_to"] = ["Earth", "Jupiter", "Saturn"] + + return HttpResponse("Ayyy") + +``` -Run the commands bellow in cmd. ## Django Commands diff --git a/setup.cfg b/setup.cfg index bba155e..138d05d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,12 +1,12 @@ [metadata] name = django-dysession version = 1.0.0 -description = "django-dysession is a django extension by using AWS DynamoDB as a session backend" +description = "django-dysession is a django extension which integrated AWS DynamoDB as a session backend" long_description = file: README.md long_description_content_type = "text/markdown" keywords = "django,session,aws,dynamodb" url = "https://github.com/MissterHao/django-dysession/" -download_url = "https://pypi.org/project/django-dysession/" +download_url = "https://github.com/MissterHao/django-dysession/" author = "Hao-Wei, Li" author_email = "henryliking@gmail.com" maintainer = "MissterHao" @@ -14,7 +14,7 @@ maintainer_email = "henryliking@gmail.com" license = MIT license_file = LICENSE classifiers = - Development Status :: 4 - Beta + Development Status :: 5 - Production/Stable Environment :: Web Environment Framework :: Django Framework :: Django :: 3.2 From a73b366dcd6cf9e8296b8faa08d490ecf7c2b46b Mon Sep 17 00:00:00 2001 From: MissterHao Date: Fri, 3 Feb 2023 08:27:41 +0800 Subject: [PATCH 3/8] feat: add github actions --- .github/workflows/publish.yml | 28 ++++++++++++++++++ .github/workflows/release.yml | 55 +++++++++++++++++++++++++++++++++++ MANIFEST.in | 1 + dysession/version.txt | 1 + setup.cfg | 4 +-- 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/release.yml create mode 100644 dysession/version.txt diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..7f29c53 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,28 @@ +name: Upload Python Package to PyPI + +on: + release: + types: [created] + +jobs: + deploy: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine upload dist/* \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..427a515 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,55 @@ +name: Release new version + +on: + push: + branches: + - readme + tags: + - "v[0-9]+.[0-9]+.[0-9]+" + +jobs: + build: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + python-version: + - 3.6 + - 3.7 + - 3.8 + - 3.9 + steps: + - uses: actions/checkout@v3 + + - name: Setup Python ${{ matrix.python-version }} Environment + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements/coverage.txt + + - name: Coverage Test + run: | + coverage run runtests.py + coverage report + + - name: Set env + run: | + echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + echo "STRIP_RELEASE_VERSION=${GITHUB_REF#refs/*/v}" >> $GITHUB_ENV + + - name: Build + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + python setup.py sdist bdist_wheel + + - name: Archive wheel artifacts file + uses: actions/upload-artifact@v3 + with: + name: django-dysession-${{ env.RELEASE_VERSION }}-artifacts + path: | + dist/* diff --git a/MANIFEST.in b/MANIFEST.in index ed677e8..18900e7 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,6 +1,7 @@ include LICENSE include README.md include MANIFEST.in +include dysession/version.txt recursive-include django-dysession *.py graft dysession graft tests diff --git a/dysession/version.txt b/dysession/version.txt new file mode 100644 index 0000000..afaf360 --- /dev/null +++ b/dysession/version.txt @@ -0,0 +1 @@ +1.0.0 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 138d05d..19a1255 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = django-dysession -version = 1.0.0 +version = file: dysession/version.txt description = "django-dysession is a django extension which integrated AWS DynamoDB as a session backend" long_description = file: README.md long_description_content_type = "text/markdown" @@ -12,7 +12,7 @@ author_email = "henryliking@gmail.com" maintainer = "MissterHao" maintainer_email = "henryliking@gmail.com" license = MIT -license_file = LICENSE +license_files = LICENSE classifiers = Development Status :: 5 - Production/Stable Environment :: Web Environment From cdde0bd13e2ec724fe6e4b53fea0e81ce154e3ef Mon Sep 17 00:00:00 2001 From: MissterHao Date: Fri, 3 Feb 2023 08:30:20 +0800 Subject: [PATCH 4/8] fix: indent problem in github action --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 427a515..0c2e98d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,7 +34,7 @@ jobs: - name: Coverage Test run: | coverage run runtests.py - coverage report + coverage report - name: Set env run: | From 05cc4e730e6ea34b80483c35a125dd7547686e69 Mon Sep 17 00:00:00 2001 From: MissterHao Date: Fri, 3 Feb 2023 08:32:57 +0800 Subject: [PATCH 5/8] fix ci problem --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0c2e98d..edd7074 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ on: jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 strategy: fail-fast: true matrix: From c23989cbeb9acffb4b495f16bd9f029568923d68 Mon Sep 17 00:00:00 2001 From: MissterHao Date: Fri, 3 Feb 2023 08:34:20 +0800 Subject: [PATCH 6/8] fix ci problem --- .github/workflows/release.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index edd7074..6453893 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,8 +14,6 @@ jobs: fail-fast: true matrix: python-version: - - 3.6 - - 3.7 - 3.8 - 3.9 steps: From 77aa8489a4f75408a89d4b34e6dc791ed03c2c90 Mon Sep 17 00:00:00 2001 From: MissterHao Date: Fri, 3 Feb 2023 08:53:25 +0800 Subject: [PATCH 7/8] change workflow python version to 3.8,3.9 --- .github/workflows/publish.yml | 3 +++ .github/workflows/release.yml | 1 + setup.cfg | 4 +--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7f29c53..ec1dc91 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,14 +11,17 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.x' + - name: Install dependencies run: | python -m pip install --upgrade pip pip install setuptools wheel twine + - name: Build and publish env: TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6453893..bdaeeb3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,6 +16,7 @@ jobs: python-version: - 3.8 - 3.9 + - 3.10 steps: - uses: actions/checkout@v3 diff --git a/setup.cfg b/setup.cfg index 19a1255..19917de 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,8 +27,6 @@ classifiers = Programming Language :: Python :: 3.10 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.8 - Programming Language :: Python :: 3.7 - Programming Language :: Python :: 3.6 Programming Language :: Python :: Implementation Topic :: Software Development :: Libraries :: Application Frameworks Topic :: Software Development :: Libraries :: Python Modules @@ -39,7 +37,7 @@ classifiers = [options] packages=find: include_package_data = true -python_requires = >=3.6 +python_requires = >=3.8 setup_requires = setuptools >= 38.3.0 install_requires = From 32bec709b2902a6d56fafc640739e63b48de2c58 Mon Sep 17 00:00:00 2001 From: MissterHao Date: Fri, 3 Feb 2023 09:00:58 +0800 Subject: [PATCH 8/8] fix bug in setup.cfg --- .github/workflows/release.yml | 1 - setup.cfg | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bdaeeb3..6453893 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,7 +16,6 @@ jobs: python-version: - 3.8 - 3.9 - - 3.10 steps: - uses: actions/checkout@v3 diff --git a/setup.cfg b/setup.cfg index 19917de..ba786cb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ name = django-dysession version = file: dysession/version.txt description = "django-dysession is a django extension which integrated AWS DynamoDB as a session backend" long_description = file: README.md -long_description_content_type = "text/markdown" +long_description_content_type = text/markdown keywords = "django,session,aws,dynamodb" url = "https://github.com/MissterHao/django-dysession/" download_url = "https://github.com/MissterHao/django-dysession/"