├── .bumpversion.cfg ├── .editorconfig ├── .github └── workflows │ ├── python-release.yml │ └── python-test.yml ├── .gitignore ├── CHANGES ├── LICENSE ├── Makefile ├── README.md ├── docs ├── Makefile ├── _templates │ └── sidebar-intro.html ├── conf.py ├── index.rst ├── make.bat └── requirements.txt ├── pyproject.toml ├── setup.cfg ├── setup.py ├── src └── django_session_timeout │ ├── __init__.py │ └── middleware.py ├── tests ├── __init__.py ├── conftest.py ├── test_middleware.py └── urls.py └── tox.ini /.bumpversion.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 0.1.0 3 | commit = true 4 | tag = true 5 | tag_name = {new_version} 6 | 7 | [bumpversion:file:setup.py] 8 | 9 | [bumpversion:file:docs/conf.py] 10 | 11 | [bumpversion:file:src/django_session_timeout/__init__.py] 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*.py] 4 | line_length = 79 5 | multi_line_output = 4 6 | balanced_wrapping = true 7 | known_first_party = django_session_timeout,tests 8 | use_parentheses = true 9 | 10 | [*.yml] 11 | indent_size = 2 12 | 13 | 14 | [Makefile] 15 | indent_style = tab 16 | indent_size = 4 17 | -------------------------------------------------------------------------------- /.github/workflows/python-release.yml: -------------------------------------------------------------------------------- 1 | name: Release to PyPi 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | release: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: Set up Python 3.7 13 | uses: actions/setup-python@v1 14 | with: 15 | python-version: 3.7 16 | - name: Install build requirements 17 | run: python -m pip install wheel 18 | - name: Build package 19 | run: python setup.py sdist bdist_wheel 20 | - name: Publish package 21 | uses: pypa/gh-action-pypi-publish@master 22 | with: 23 | user: __token__ 24 | password: ${{ secrets.pypi_password }} 25 | -------------------------------------------------------------------------------- /.github/workflows/python-test.yml: -------------------------------------------------------------------------------- 1 | name: Python Tests 2 | 3 | on: [push] 4 | 5 | jobs: 6 | 7 | format: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v2 11 | - name: Set up Python 3.7 12 | uses: actions/setup-python@v1 13 | with: 14 | python-version: 3.7 15 | - name: Install dependencies 16 | run: pip install tox 17 | - name: Validate formatting 18 | run: tox -e format 19 | 20 | test: 21 | runs-on: ubuntu-latest 22 | strategy: 23 | max-parallel: 4 24 | matrix: 25 | tox_env: 26 | - py36-django22 27 | - py36-django30 28 | - py37-django22 29 | - py37-django30 30 | - py38-django22 31 | - py38-django30 32 | include: 33 | - python-version: 3.6 34 | tox_env: py36-django22 35 | - python-version: 3.6 36 | tox_env: py36-django30 37 | - python-version: 3.7 38 | tox_env: py37-django22 39 | - python-version: 3.7 40 | tox_env: py37-django30 41 | - python-version: 3.8 42 | tox_env: py38-django22 43 | - python-version: 3.8 44 | tox_env: py38-django30 45 | 46 | steps: 47 | - uses: actions/checkout@v2 48 | - name: Set up Python ${{ matrix.python-version }} 49 | uses: actions/setup-python@v1 50 | with: 51 | python-version: ${{ matrix.python-version }} 52 | - name: Install dependencies 53 | run: | 54 | python -m pip install --upgrade pip 55 | pip install tox tox-gh-actions 56 | - name: Test with tox 57 | run: tox -e ${{ matrix.tox_env }} 58 | - name: Prepare artifacts 59 | run: mkdir .coverage-data && mv .coverage.* .coverage-data/ 60 | - uses: actions/upload-artifact@master 61 | with: 62 | name: coverage-data 63 | path: .coverage-data/ 64 | 65 | coverage: 66 | runs-on: ubuntu-latest 67 | needs: [test] 68 | steps: 69 | - uses: actions/checkout@v2 70 | - uses: actions/download-artifact@master 71 | with: 72 | name: coverage-data 73 | path: . 74 | - name: Set up Python 3.7 75 | uses: actions/setup-python@v1 76 | with: 77 | python-version: 3.7 78 | - name: Install dependencies 79 | run: | 80 | python -m pip install --upgrade pip 81 | pip install tox 82 | - name: Prepare Coverage report 83 | run: tox -e coverage-report 84 | - name: Upload to codecov 85 | uses: codecov/codecov-action@v1.0.6 86 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.egg-info 2 | *.pyc 3 | .tox 4 | .coverage 5 | .coverage.* 6 | .eggs 7 | .cache 8 | .python-version 9 | .venv 10 | .idea/ 11 | 12 | /build/ 13 | /dist/ 14 | /docs/_build/ 15 | /htmlcov/ 16 | 17 | 18 | # Editors 19 | .idea/ 20 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | 0.1.0 (2020-03-08) 2 | ------------------ 3 | - Add SESSION_TIMEOUT_REDIRECT to set a custom URL for redirect users which 4 | have an expired session. See #9 5 | - Update GitHub Actions CI/CD 6 | 7 | 8 | 0.0.4 (2019-04-01) 9 | ------------------ 10 | - added grace period to SESSION_EXPIRE_AFTER_LAST_ACTIVITY 11 | 12 | 13 | 0.0.3 (2017-12-14) 14 | ------------------ 15 | - Redirect user to the login page after session timeout instead of the root page 16 | 17 | 18 | 0.0.2 (2017-11-10) 19 | ------------------ 20 | - Added setting to control if session should be expired X seconds after last 21 | activity or first 22 | 23 | 24 | 0.0.1 (2017-09-06) 25 | ------------------ 26 | - Initial release 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Michael van Tellingen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: install test upload docs 2 | 3 | 4 | install: 5 | pip install -e .[docs,test] 6 | 7 | test: 8 | py.test 9 | 10 | retest: 11 | py.test -vvv --lf 12 | 13 | coverage: 14 | py.test --cov=django_session_timeout --cov-report=term-missing --cov-report=html 15 | 16 | format: 17 | isort --recursive src tests 18 | black src/ tests/ 19 | 20 | docs: 21 | $(MAKE) -C docs html 22 | 23 | release: 24 | rm -rf dist/* 25 | python setup.py sdist bdist_wheel 26 | twine upload dist/* 27 | 28 | BLACK_EXCLUDE="/(\.git|\.hg|\.mypy_cache|\.tox|\.venv|_build|buck-out|build|dist)/" 29 | black: 30 | pip install --upgrade black 31 | black --verbose --exclude $(BLACK_EXCLUDE) ./src 32 | black --verbose --exclude $(BLACK_EXCLUDE) ./tests 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![codecov](https://codecov.io/gh/labd/django-session-timeout/branch/master/graph/badge.svg)](https://codecov.io/gh/labd/django-session-timeout) 3 | [![pypi](https://img.shields.io/pypi/v/django-session-timeout.svg)](https://pypi.python.org/pypi/django-session-timeout/) 4 | [![readthedocs](https://readthedocs.org/projects/django-session-timeout/badge/)](https://django-session-timeout.readthedocs.io/en/latest/) 5 | [![tests](https://github.com/labd/django-session-timeout/workflows/Python%20Tests/badge.svg)](https://github.com/labd/django-session-timeout/actions) 6 | 7 | 8 | # django-session-timeout 9 | 10 | Add timestamp to sessions to expire them independently 11 | 12 | ## Installation 13 | 14 | ```shell 15 | pip install django-session-timeout 16 | ``` 17 | 18 | ## Usage 19 | 20 | Update your settings to add the SessionTimeoutMiddleware: 21 | 22 | ```python 23 | MIDDLEWARE_CLASSES = [ 24 | # ... 25 | 'django.contrib.sessions.middleware.SessionMiddleware', 26 | 'django_session_timeout.middleware.SessionTimeoutMiddleware', 27 | # ... 28 | ] 29 | ``` 30 | 31 | And also add the ``SESSION_EXPIRE_SECONDS``: 32 | 33 | 34 | ```python 35 | SESSION_EXPIRE_SECONDS = 3600 # 1 hour 36 | ``` 37 | 38 | By default, the session will expire X seconds after the start of the session. 39 | To expire the session X seconds after the `last activity`, use the following setting: 40 | 41 | ```python 42 | SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True 43 | ``` 44 | 45 | By default, `last activity` will be grouped per second. 46 | To group by different period use the following setting: 47 | 48 | ```python 49 | SESSION_EXPIRE_AFTER_LAST_ACTIVITY_GRACE_PERIOD = 60 # group by minute 50 | ``` 51 | 52 | To redirect to a custom URL define the following setting: 53 | 54 | ```python 55 | SESSION_TIMEOUT_REDIRECT = 'your_redirect_url_here/' 56 | ``` 57 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 16 | 17 | .PHONY: help 18 | help: 19 | @echo "Please use \`make ' where is one of" 20 | @echo " html to make standalone HTML files" 21 | @echo " dirhtml to make HTML files named index.html in directories" 22 | @echo " singlehtml to make a single large HTML file" 23 | @echo " pickle to make pickle files" 24 | @echo " json to make JSON files" 25 | @echo " htmlhelp to make HTML files and a HTML help project" 26 | @echo " qthelp to make HTML files and a qthelp project" 27 | @echo " applehelp to make an Apple Help Book" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " epub3 to make an epub3" 31 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 32 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 33 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 34 | @echo " text to make text files" 35 | @echo " man to make manual pages" 36 | @echo " texinfo to make Texinfo files" 37 | @echo " info to make Texinfo files and run them through makeinfo" 38 | @echo " gettext to make PO message catalogs" 39 | @echo " changes to make an overview of all changed/added/deprecated items" 40 | @echo " xml to make Docutils-native XML files" 41 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 42 | @echo " linkcheck to check all external links for integrity" 43 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 44 | @echo " coverage to run coverage check of the documentation (if enabled)" 45 | @echo " dummy to check syntax errors of document sources" 46 | 47 | .PHONY: clean 48 | clean: 49 | rm -rf $(BUILDDIR)/* 50 | 51 | .PHONY: html 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | .PHONY: dirhtml 58 | dirhtml: 59 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 60 | @echo 61 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 62 | 63 | .PHONY: singlehtml 64 | singlehtml: 65 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 66 | @echo 67 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 68 | 69 | .PHONY: pickle 70 | pickle: 71 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 72 | @echo 73 | @echo "Build finished; now you can process the pickle files." 74 | 75 | .PHONY: json 76 | json: 77 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 78 | @echo 79 | @echo "Build finished; now you can process the JSON files." 80 | 81 | .PHONY: htmlhelp 82 | htmlhelp: 83 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 84 | @echo 85 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 86 | ".hhp project file in $(BUILDDIR)/htmlhelp." 87 | 88 | .PHONY: qthelp 89 | qthelp: 90 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 91 | @echo 92 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 93 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 94 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/django_session_timeout.qhcp" 95 | @echo "To view the help file:" 96 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/django_session_timeout.qhc" 97 | 98 | .PHONY: applehelp 99 | applehelp: 100 | $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp 101 | @echo 102 | @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." 103 | @echo "N.B. You won't be able to view it unless you put it in" \ 104 | "~/Library/Documentation/Help or install it in your application" \ 105 | "bundle." 106 | 107 | .PHONY: devhelp 108 | devhelp: 109 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 110 | @echo 111 | @echo "Build finished." 112 | @echo "To view the help file:" 113 | @echo "# mkdir -p $$HOME/.local/share/devhelp/django_session_timeout" 114 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/django_session_timeout" 115 | @echo "# devhelp" 116 | 117 | .PHONY: epub 118 | epub: 119 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 120 | @echo 121 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 122 | 123 | .PHONY: epub3 124 | epub3: 125 | $(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3 126 | @echo 127 | @echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3." 128 | 129 | .PHONY: latex 130 | latex: 131 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 132 | @echo 133 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 134 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 135 | "(use \`make latexpdf' here to do that automatically)." 136 | 137 | .PHONY: latexpdf 138 | latexpdf: 139 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 140 | @echo "Running LaTeX files through pdflatex..." 141 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 142 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 143 | 144 | .PHONY: latexpdfja 145 | latexpdfja: 146 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 147 | @echo "Running LaTeX files through platex and dvipdfmx..." 148 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 149 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 150 | 151 | .PHONY: text 152 | text: 153 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 154 | @echo 155 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 156 | 157 | .PHONY: man 158 | man: 159 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 160 | @echo 161 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 162 | 163 | .PHONY: texinfo 164 | texinfo: 165 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 166 | @echo 167 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 168 | @echo "Run \`make' in that directory to run these through makeinfo" \ 169 | "(use \`make info' here to do that automatically)." 170 | 171 | .PHONY: info 172 | info: 173 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 174 | @echo "Running Texinfo files through makeinfo..." 175 | make -C $(BUILDDIR)/texinfo info 176 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 177 | 178 | .PHONY: gettext 179 | gettext: 180 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 181 | @echo 182 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 183 | 184 | .PHONY: changes 185 | changes: 186 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 187 | @echo 188 | @echo "The overview file is in $(BUILDDIR)/changes." 189 | 190 | .PHONY: linkcheck 191 | linkcheck: 192 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 193 | @echo 194 | @echo "Link check complete; look for any errors in the above output " \ 195 | "or in $(BUILDDIR)/linkcheck/output.txt." 196 | 197 | .PHONY: doctest 198 | doctest: 199 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 200 | @echo "Testing of doctests in the sources finished, look at the " \ 201 | "results in $(BUILDDIR)/doctest/output.txt." 202 | 203 | .PHONY: coverage 204 | coverage: 205 | $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage 206 | @echo "Testing of coverage in the sources finished, look at the " \ 207 | "results in $(BUILDDIR)/coverage/python.txt." 208 | 209 | .PHONY: xml 210 | xml: 211 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 212 | @echo 213 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 214 | 215 | .PHONY: pseudoxml 216 | pseudoxml: 217 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 218 | @echo 219 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 220 | 221 | .PHONY: dummy 222 | dummy: 223 | $(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy 224 | @echo 225 | @echo "Build finished. Dummy builder generates no files." 226 | -------------------------------------------------------------------------------- /docs/_templates/sidebar-intro.html: -------------------------------------------------------------------------------- 1 |

Links

2 |

3 | 5 |

6 | 11 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # django-session-timeout documentation build configuration file, created by 4 | # sphinx-quickstart on Wed Aug 10 17:06:14 2016. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | # If extensions (or modules to document with autodoc) are in another directory, 16 | # add these directories to sys.path here. If the directory is relative to the 17 | # documentation root, use os.path.abspath to make it absolute, like shown here. 18 | # 19 | # import os 20 | # import sys 21 | # sys.path.insert(0, os.path.abspath('.')) 22 | 23 | # -- General configuration ------------------------------------------------ 24 | 25 | # If your documentation needs a minimal Sphinx version, state it here. 26 | # 27 | # needs_sphinx = '1.0' 28 | 29 | # Add any Sphinx extension module names here, as strings. They can be 30 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 31 | # ones. 32 | extensions = ['sphinx.ext.autodoc'] 33 | 34 | # Add any paths that contain templates here, relative to this directory. 35 | templates_path = ['_templates'] 36 | 37 | # The suffix(es) of source filenames. 38 | # You can specify multiple suffix as a list of string: 39 | # 40 | # source_suffix = ['.rst', '.md'] 41 | source_suffix = '.rst' 42 | 43 | # The encoding of source files. 44 | # 45 | # source_encoding = 'utf-8-sig' 46 | 47 | # The master toctree document. 48 | master_doc = 'index' 49 | 50 | # General information about the project. 51 | project = u'django-session-timeout' 52 | copyright = u'Y, Michael van Tellingen' 53 | author = u'Michael van Tellingen' 54 | 55 | # The version info for the project you're documenting, acts as replacement for 56 | # |version| and |release|, also used in various other places throughout the 57 | # built documents. 58 | # 59 | version = '0.1.0' 60 | release = version 61 | 62 | # The language for content autogenerated by Sphinx. Refer to documentation 63 | # for a list of supported languages. 64 | # 65 | # This is also used if you do content translation via gettext catalogs. 66 | # Usually you set "language" from the command line for these cases. 67 | language = None 68 | 69 | # There are two options for replacing |today|: either, you set today to some 70 | # non-false value, then it is used: 71 | # 72 | # today = '' 73 | # 74 | # Else, today_fmt is used as the format for a strftime call. 75 | # 76 | # today_fmt = '%B %d, %Y' 77 | 78 | # List of patterns, relative to source directory, that match files and 79 | # directories to ignore when looking for source files. 80 | # This patterns also effect to html_static_path and html_extra_path 81 | exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] 82 | 83 | # The reST default role (used for this markup: `text`) to use for all 84 | # documents. 85 | # 86 | # default_role = None 87 | 88 | # If true, '()' will be appended to :func: etc. cross-reference text. 89 | # 90 | # add_function_parentheses = True 91 | 92 | # If true, the current module name will be prepended to all description 93 | # unit titles (such as .. function::). 94 | # 95 | # add_module_names = True 96 | 97 | # If true, sectionauthor and moduleauthor directives will be shown in the 98 | # output. They are ignored by default. 99 | # 100 | # show_authors = False 101 | 102 | # The name of the Pygments (syntax highlighting) style to use. 103 | pygments_style = 'sphinx' 104 | 105 | # A list of ignored prefixes for module index sorting. 106 | # modindex_common_prefix = [] 107 | 108 | # If true, keep warnings as "system message" paragraphs in the built documents. 109 | # keep_warnings = False 110 | 111 | # If true, `todo` and `todoList` produce output, else they produce nothing. 112 | todo_include_todos = False 113 | 114 | 115 | # -- Options for HTML output ---------------------------------------------- 116 | 117 | # The theme to use for HTML and HTML Help pages. See the documentation for 118 | # a list of builtin themes. 119 | # 120 | html_theme = 'alabaster' 121 | 122 | # Theme options are theme-specific and customize the look and feel of a theme 123 | # further. For a list of options available for each theme, see the 124 | # documentation. 125 | # 126 | # html_theme_options = {} 127 | html_theme_options = { 128 | 'github_user': 'LabD', 129 | 'github_banner': True, 130 | 'github_repo': 'django-session-timeout', 131 | 'travis_button': True, 132 | 'codecov_button': True, 133 | 'analytics_id': 'UA-75907833-X', 134 | } 135 | 136 | # Add any paths that contain custom themes here, relative to this directory. 137 | # html_theme_path = [] 138 | 139 | # The name for this set of Sphinx documents. 140 | # " v documentation" by default. 141 | # 142 | # html_title = u'django-session-timeout v0.1.0' 143 | 144 | # A shorter title for the navigation bar. Default is the same as html_title. 145 | # 146 | # html_short_title = None 147 | 148 | # The name of an image file (relative to this directory) to place at the top 149 | # of the sidebar. 150 | # 151 | # html_logo = None 152 | 153 | # The name of an image file (relative to this directory) to use as a favicon of 154 | # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 155 | # pixels large. 156 | # 157 | # html_favicon = None 158 | 159 | # Add any paths that contain custom static files (such as style sheets) here, 160 | # relative to this directory. They are copied after the builtin static files, 161 | # so a file named "default.css" will overwrite the builtin "default.css". 162 | html_static_path = ['_static'] 163 | 164 | # Add any extra paths that contain custom files (such as robots.txt or 165 | # .htaccess) here, relative to this directory. These files are copied 166 | # directly to the root of the documentation. 167 | # 168 | # html_extra_path = [] 169 | 170 | # If not None, a 'Last updated on:' timestamp is inserted at every page 171 | # bottom, using the given strftime format. 172 | # The empty string is equivalent to '%b %d, %Y'. 173 | # 174 | # html_last_updated_fmt = None 175 | 176 | # If true, SmartyPants will be used to convert quotes and dashes to 177 | # typographically correct entities. 178 | # 179 | # html_use_smartypants = True 180 | 181 | # Custom sidebar templates, maps document names to template names. 182 | # 183 | # html_sidebars = {} 184 | # Custom sidebar templates, maps document names to template names. 185 | html_sidebars = { 186 | '*': [ 187 | 'sidebar-intro.html', 188 | ] 189 | } 190 | 191 | 192 | # Additional templates that should be rendered to pages, maps page names to 193 | # template names. 194 | # 195 | # html_additional_pages = {} 196 | 197 | # If false, no module index is generated. 198 | # 199 | # html_domain_indices = True 200 | 201 | # If false, no index is generated. 202 | # 203 | # html_use_index = True 204 | 205 | # If true, the index is split into individual pages for each letter. 206 | # 207 | # html_split_index = False 208 | 209 | # If true, links to the reST sources are added to the pages. 210 | # 211 | # html_show_sourcelink = True 212 | 213 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 214 | # 215 | # html_show_sphinx = True 216 | 217 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 218 | # 219 | # html_show_copyright = True 220 | 221 | # If true, an OpenSearch description file will be output, and all pages will 222 | # contain a tag referring to it. The value of this option must be the 223 | # base URL from which the finished HTML is served. 224 | # 225 | # html_use_opensearch = '' 226 | 227 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 228 | # html_file_suffix = None 229 | 230 | # Language to be used for generating the HTML full-text search index. 231 | # Sphinx supports the following languages: 232 | # 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' 233 | # 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh' 234 | # 235 | # html_search_language = 'en' 236 | 237 | # A dictionary with options for the search language support, empty by default. 238 | # 'ja' uses this config value. 239 | # 'zh' user can custom change `jieba` dictionary path. 240 | # 241 | # html_search_options = {'type': 'default'} 242 | 243 | # The name of a javascript file (relative to the configuration directory) that 244 | # implements a search results scorer. If empty, the default will be used. 245 | # 246 | # html_search_scorer = 'scorer.js' 247 | 248 | # Output file base name for HTML help builder. 249 | htmlhelp_basename = 'django_session_timeout-doc' 250 | 251 | # -- Options for LaTeX output --------------------------------------------- 252 | 253 | latex_elements = { 254 | # The paper size ('letterpaper' or 'a4paper'). 255 | # 256 | # 'papersize': 'letterpaper', 257 | 258 | # The font size ('10pt', '11pt' or '12pt'). 259 | # 260 | # 'pointsize': '10pt', 261 | 262 | # Additional stuff for the LaTeX preamble. 263 | # 264 | # 'preamble': '', 265 | 266 | # Latex figure (float) alignment 267 | # 268 | # 'figure_align': 'htbp', 269 | } 270 | 271 | # Grouping the document tree into LaTeX files. List of tuples 272 | # (source start file, target name, title, 273 | # author, documentclass [howto, manual, or own class]). 274 | latex_documents = [ 275 | (master_doc, 'django_session_timeout.tex', u'django-session-timeout Documentation', 276 | u'Michael van Tellingen', 'manual'), 277 | ] 278 | 279 | # The name of an image file (relative to this directory) to place at the top of 280 | # the title page. 281 | # 282 | # latex_logo = None 283 | 284 | # For "manual" documents, if this is true, then toplevel headings are parts, 285 | # not chapters. 286 | # 287 | # latex_use_parts = False 288 | 289 | # If true, show page references after internal links. 290 | # 291 | # latex_show_pagerefs = False 292 | 293 | # If true, show URL addresses after external links. 294 | # 295 | # latex_show_urls = False 296 | 297 | # Documents to append as an appendix to all manuals. 298 | # 299 | # latex_appendices = [] 300 | 301 | # It false, will not define \strong, \code, itleref, \crossref ... but only 302 | # \sphinxstrong, ..., \sphinxtitleref, ... To help avoid clash with user added 303 | # packages. 304 | # 305 | # latex_keep_old_macro_names = True 306 | 307 | # If false, no module index is generated. 308 | # 309 | # latex_domain_indices = True 310 | 311 | 312 | # -- Options for manual page output --------------------------------------- 313 | 314 | # One entry per manual page. List of tuples 315 | # (source start file, name, description, authors, manual section). 316 | man_pages = [ 317 | (master_doc, 'django_session_timeout', u'django-session-timeout Documentation', 318 | [author], 1) 319 | ] 320 | 321 | # If true, show URL addresses after external links. 322 | # 323 | # man_show_urls = False 324 | 325 | 326 | # -- Options for Texinfo output ------------------------------------------- 327 | 328 | # Grouping the document tree into Texinfo files. List of tuples 329 | # (source start file, target name, title, author, 330 | # dir menu entry, description, category) 331 | texinfo_documents = [ 332 | (master_doc, 'django_session_timeout', u'django-session-timeout Documentation', 333 | author, 'django_session_timeout', 'One line description of project.', 334 | 'Miscellaneous'), 335 | ] 336 | 337 | # Documents to append as an appendix to all manuals. 338 | # 339 | # texinfo_appendices = [] 340 | 341 | # If false, no module index is generated. 342 | # 343 | # texinfo_domain_indices = True 344 | 345 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 346 | # 347 | # texinfo_show_urls = 'footnote' 348 | 349 | # If true, do not generate a @detailmenu in the "Top" node's menu. 350 | # 351 | # texinfo_no_detailmenu = False 352 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | =================== 2 | django-session-timeout 3 | =================== 4 | 5 | 6 | Installation 7 | ============ 8 | 9 | .. code-block:: shell 10 | 11 | pip install django_session_timeout 12 | 13 | Usage 14 | ===== 15 | 16 | Update your settings to add the SessionTimeoutMiddleware: 17 | 18 | .. code-block:: python 19 | 20 | MIDDLEWARE_CLASSES = [ 21 | # ... 22 | 'django.contrib.sessions.middleware.SessionMiddleware', 23 | 'django_session_timeout.middleware.SessionTimeoutMiddleware', 24 | # ... 25 | ] 26 | 27 | 28 | And also add the ``SESSION_EXPIRE_SECONDS``: 29 | 30 | 31 | .. code-block:: python 32 | 33 | SESSION_EXPIRE_SECONDS = 3600 # 1 hour 34 | 35 | 36 | By default, the session will expire X seconds after the start of the session. 37 | To expire the session X seconds after the `last activity`, use the following setting: 38 | 39 | .. code-block:: python 40 | 41 | SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True 42 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. epub3 to make an epub3 31 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 32 | echo. text to make text files 33 | echo. man to make manual pages 34 | echo. texinfo to make Texinfo files 35 | echo. gettext to make PO message catalogs 36 | echo. changes to make an overview over all changed/added/deprecated items 37 | echo. xml to make Docutils-native XML files 38 | echo. pseudoxml to make pseudoxml-XML files for display purposes 39 | echo. linkcheck to check all external links for integrity 40 | echo. doctest to run all doctests embedded in the documentation if enabled 41 | echo. coverage to run coverage check of the documentation if enabled 42 | echo. dummy to check syntax errors of document sources 43 | goto end 44 | ) 45 | 46 | if "%1" == "clean" ( 47 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 48 | del /q /s %BUILDDIR%\* 49 | goto end 50 | ) 51 | 52 | 53 | REM Check if sphinx-build is available and fallback to Python version if any 54 | %SPHINXBUILD% 1>NUL 2>NUL 55 | if errorlevel 9009 goto sphinx_python 56 | goto sphinx_ok 57 | 58 | :sphinx_python 59 | 60 | set SPHINXBUILD=python -m sphinx.__init__ 61 | %SPHINXBUILD% 2> nul 62 | if errorlevel 9009 ( 63 | echo. 64 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 65 | echo.installed, then set the SPHINXBUILD environment variable to point 66 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 67 | echo.may add the Sphinx directory to PATH. 68 | echo. 69 | echo.If you don't have Sphinx installed, grab it from 70 | echo.http://sphinx-doc.org/ 71 | exit /b 1 72 | ) 73 | 74 | :sphinx_ok 75 | 76 | 77 | if "%1" == "html" ( 78 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 79 | if errorlevel 1 exit /b 1 80 | echo. 81 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 82 | goto end 83 | ) 84 | 85 | if "%1" == "dirhtml" ( 86 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 87 | if errorlevel 1 exit /b 1 88 | echo. 89 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 90 | goto end 91 | ) 92 | 93 | if "%1" == "singlehtml" ( 94 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 95 | if errorlevel 1 exit /b 1 96 | echo. 97 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 98 | goto end 99 | ) 100 | 101 | if "%1" == "pickle" ( 102 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 103 | if errorlevel 1 exit /b 1 104 | echo. 105 | echo.Build finished; now you can process the pickle files. 106 | goto end 107 | ) 108 | 109 | if "%1" == "json" ( 110 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 111 | if errorlevel 1 exit /b 1 112 | echo. 113 | echo.Build finished; now you can process the JSON files. 114 | goto end 115 | ) 116 | 117 | if "%1" == "htmlhelp" ( 118 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 119 | if errorlevel 1 exit /b 1 120 | echo. 121 | echo.Build finished; now you can run HTML Help Workshop with the ^ 122 | .hhp project file in %BUILDDIR%/htmlhelp. 123 | goto end 124 | ) 125 | 126 | if "%1" == "qthelp" ( 127 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 128 | if errorlevel 1 exit /b 1 129 | echo. 130 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 131 | .qhcp project file in %BUILDDIR%/qthelp, like this: 132 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\django_session_timeout.qhcp 133 | echo.To view the help file: 134 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\django_session_timeout.ghc 135 | goto end 136 | ) 137 | 138 | if "%1" == "devhelp" ( 139 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 140 | if errorlevel 1 exit /b 1 141 | echo. 142 | echo.Build finished. 143 | goto end 144 | ) 145 | 146 | if "%1" == "epub" ( 147 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 148 | if errorlevel 1 exit /b 1 149 | echo. 150 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 151 | goto end 152 | ) 153 | 154 | if "%1" == "epub3" ( 155 | %SPHINXBUILD% -b epub3 %ALLSPHINXOPTS% %BUILDDIR%/epub3 156 | if errorlevel 1 exit /b 1 157 | echo. 158 | echo.Build finished. The epub3 file is in %BUILDDIR%/epub3. 159 | goto end 160 | ) 161 | 162 | if "%1" == "latex" ( 163 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 164 | if errorlevel 1 exit /b 1 165 | echo. 166 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 167 | goto end 168 | ) 169 | 170 | if "%1" == "latexpdf" ( 171 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 172 | cd %BUILDDIR%/latex 173 | make all-pdf 174 | cd %~dp0 175 | echo. 176 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 177 | goto end 178 | ) 179 | 180 | if "%1" == "latexpdfja" ( 181 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 182 | cd %BUILDDIR%/latex 183 | make all-pdf-ja 184 | cd %~dp0 185 | echo. 186 | echo.Build finished; the PDF files are in %BUILDDIR%/latex. 187 | goto end 188 | ) 189 | 190 | if "%1" == "text" ( 191 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 192 | if errorlevel 1 exit /b 1 193 | echo. 194 | echo.Build finished. The text files are in %BUILDDIR%/text. 195 | goto end 196 | ) 197 | 198 | if "%1" == "man" ( 199 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 200 | if errorlevel 1 exit /b 1 201 | echo. 202 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 203 | goto end 204 | ) 205 | 206 | if "%1" == "texinfo" ( 207 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 208 | if errorlevel 1 exit /b 1 209 | echo. 210 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 211 | goto end 212 | ) 213 | 214 | if "%1" == "gettext" ( 215 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 216 | if errorlevel 1 exit /b 1 217 | echo. 218 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 219 | goto end 220 | ) 221 | 222 | if "%1" == "changes" ( 223 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 224 | if errorlevel 1 exit /b 1 225 | echo. 226 | echo.The overview file is in %BUILDDIR%/changes. 227 | goto end 228 | ) 229 | 230 | if "%1" == "linkcheck" ( 231 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 232 | if errorlevel 1 exit /b 1 233 | echo. 234 | echo.Link check complete; look for any errors in the above output ^ 235 | or in %BUILDDIR%/linkcheck/output.txt. 236 | goto end 237 | ) 238 | 239 | if "%1" == "doctest" ( 240 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 241 | if errorlevel 1 exit /b 1 242 | echo. 243 | echo.Testing of doctests in the sources finished, look at the ^ 244 | results in %BUILDDIR%/doctest/output.txt. 245 | goto end 246 | ) 247 | 248 | if "%1" == "coverage" ( 249 | %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage 250 | if errorlevel 1 exit /b 1 251 | echo. 252 | echo.Testing of coverage in the sources finished, look at the ^ 253 | results in %BUILDDIR%/coverage/python.txt. 254 | goto end 255 | ) 256 | 257 | if "%1" == "xml" ( 258 | %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml 259 | if errorlevel 1 exit /b 1 260 | echo. 261 | echo.Build finished. The XML files are in %BUILDDIR%/xml. 262 | goto end 263 | ) 264 | 265 | if "%1" == "pseudoxml" ( 266 | %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml 267 | if errorlevel 1 exit /b 1 268 | echo. 269 | echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. 270 | goto end 271 | ) 272 | 273 | if "%1" == "dummy" ( 274 | %SPHINXBUILD% -b dummy %ALLSPHINXOPTS% %BUILDDIR%/dummy 275 | if errorlevel 1 exit /b 1 276 | echo. 277 | echo.Build finished. Dummy builder generates no files. 278 | goto end 279 | ) 280 | 281 | :end 282 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | -e .[docs] 2 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | 2 | [build-system] 3 | requires = ["setuptools>=40.6.0", "wheel"] 4 | build-backend = "setuptools.build_meta" 5 | 6 | [tool.coverage.run] 7 | branch = true 8 | source = ["django_session_timeout"] 9 | 10 | [tool.coverage.paths] 11 | source = ["src", ".tox/*/site-packages"] 12 | 13 | [tool.coverage.report] 14 | show_missing = true 15 | 16 | [tool.isort] 17 | line_length = 88 18 | multi_line_output = 3 19 | include_trailing_comma = true 20 | balanced_wrapping = true 21 | default_section = "THIRDPARTY" 22 | known_first_party = ["django_session_timeout", "tests"] 23 | use_parentheses = true 24 | indent_style = "space" 25 | indent_size = 4 26 | tab_width = 4 27 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [tool:pytest] 2 | minversion = 3.0 3 | strict = true 4 | testpaths = tests 5 | 6 | [wheel] 7 | universal = 1 8 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | from setuptools import find_packages, setup 4 | 5 | docs_require = ["sphinx>=1.8.4"] 6 | 7 | tests_require = [ 8 | "coverage[toml]==5.0.3", 9 | "freezegun==0.3.15", 10 | "pytest==5.3.5", 11 | "pytest-django==3.8.0", 12 | "pytest-cov==2.8.1",\ 13 | # Linting 14 | "isort[pyproject]==4.3.21", 15 | "flake8==3.7.9", 16 | "flake8-blind-except==0.1.1", 17 | "flake8-debugger==3.1.0", 18 | ] 19 | 20 | with open("README.md") as fh: 21 | long_description = re.sub( 22 | ".*\n", 23 | "", 24 | fh.read(), 25 | flags=re.M | re.S, 26 | ) 27 | 28 | setup( 29 | name="django-session-timeout", 30 | version="0.1.0", 31 | description="Middleware to expire sessions after specific amount of time", 32 | long_description=long_description, 33 | long_description_content_type="text/markdown", 34 | url="https://github.com/LabD/django-session-timeout", 35 | author="Lab Digital", 36 | author_email="opensource@labdigital.nl", 37 | install_requires=["Django>=1.11", "six>=1.12"], 38 | tests_require=tests_require, 39 | extras_require={"docs": docs_require, "test": tests_require}, 40 | use_scm_version=True, 41 | entry_points={}, 42 | package_dir={"": "src"}, 43 | packages=find_packages("src"), 44 | include_package_data=True, 45 | license="MIT", 46 | classifiers=[ 47 | "Development Status :: 5 - Production/Stable", 48 | "Environment :: Web Environment", 49 | "Framework :: Django", 50 | "Framework :: Django :: 2.0", 51 | "Framework :: Django :: 2.1", 52 | "Framework :: Django :: 2.2", 53 | "Framework :: Django :: 3.0", 54 | "License :: OSI Approved :: MIT License", 55 | "Programming Language :: Python", 56 | "Programming Language :: Python :: 3", 57 | "Programming Language :: Python :: 3.5", 58 | "Programming Language :: Python :: 3.6", 59 | "Programming Language :: Python :: 3.7", 60 | ], 61 | zip_safe=False, 62 | ) 63 | -------------------------------------------------------------------------------- /src/django_session_timeout/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = "0.1.0" 2 | -------------------------------------------------------------------------------- /src/django_session_timeout/middleware.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | from django.conf import settings 4 | from django.contrib.auth.views import redirect_to_login 5 | from django.shortcuts import redirect 6 | 7 | try: 8 | from django.utils.deprecation import MiddlewareMixin 9 | except ImportError: 10 | MiddlewareMixin = object 11 | 12 | 13 | SESSION_TIMEOUT_KEY = "_session_init_timestamp_" 14 | 15 | 16 | class SessionTimeoutMiddleware(MiddlewareMixin): 17 | def process_request(self, request): 18 | if not hasattr(request, "session") or request.session.is_empty(): 19 | return 20 | 21 | init_time = request.session.setdefault(SESSION_TIMEOUT_KEY, time.time()) 22 | 23 | expire_seconds = getattr( 24 | settings, "SESSION_EXPIRE_SECONDS", settings.SESSION_COOKIE_AGE 25 | ) 26 | 27 | session_is_expired = time.time() - init_time > expire_seconds 28 | 29 | if session_is_expired: 30 | request.session.flush() 31 | redirect_url = getattr(settings, "SESSION_TIMEOUT_REDIRECT", None) 32 | if redirect_url: 33 | return redirect(redirect_url) 34 | else: 35 | return redirect_to_login(next=request.path) 36 | 37 | expire_since_last_activity = getattr( 38 | settings, "SESSION_EXPIRE_AFTER_LAST_ACTIVITY", False 39 | ) 40 | grace_period = getattr( 41 | settings, "SESSION_EXPIRE_AFTER_LAST_ACTIVITY_GRACE_PERIOD", 1 42 | ) 43 | 44 | if expire_since_last_activity and time.time() - init_time > grace_period: 45 | request.session[SESSION_TIMEOUT_KEY] = time.time() 46 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/labd/django-session-timeout/15983b1a5fb42f117f76acfafa497ef0ca83957c/tests/__init__.py -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | from django.conf import settings 2 | 3 | 4 | def pytest_configure(): 5 | settings.configure( 6 | INSTALLED_APPS=[ 7 | "django.contrib.contenttypes", 8 | "django.contrib.auth", 9 | "django.contrib.sessions", 10 | ], 11 | MIDDLEWARE_CLASSES=[], 12 | ROOT_URLCONF="tests.urls", 13 | CACHES={ 14 | "default": { 15 | "BACKEND": "django.core.cache.backends.locmem.LocMemCache", 16 | "LOCATION": "unique-snowflake", 17 | } 18 | }, 19 | SESSION_ENGINE="django.contrib.sessions.backends.cache", 20 | DATABASES={ 21 | "default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "db.sqlite"} 22 | }, 23 | ) 24 | -------------------------------------------------------------------------------- /tests/test_middleware.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from django.contrib.sessions.middleware import SessionMiddleware 3 | from freezegun import freeze_time 4 | 5 | from django_session_timeout.middleware import ( 6 | SESSION_TIMEOUT_KEY, 7 | SessionTimeoutMiddleware, 8 | ) 9 | 10 | 11 | @pytest.fixture(scope="function") 12 | def r(rf): 13 | req = rf.get("/") 14 | middleware = SessionMiddleware() 15 | middleware.process_request(req) 16 | req.session["example_key"] = "1" 17 | 18 | req.session.save() 19 | yield req 20 | 21 | 22 | def test_session_new(r): 23 | middleware = SessionTimeoutMiddleware() 24 | assert middleware.process_request(r) is None 25 | assert r.session[SESSION_TIMEOUT_KEY] 26 | 27 | 28 | def test_session_new_empty_session(r): 29 | r.session.flush() 30 | 31 | middleware = SessionTimeoutMiddleware() 32 | assert middleware.process_request(r) is None 33 | assert SESSION_TIMEOUT_KEY not in r.session 34 | 35 | 36 | def test_session_expire(r, settings): 37 | settings.SESSION_EXPIRE_SECONDS = 3600 38 | middleware = SessionTimeoutMiddleware() 39 | 40 | with freeze_time("2017-08-31 21:46:00"): 41 | assert middleware.process_request(r) is None 42 | 43 | with freeze_time("2017-08-31 22:45:00"): 44 | assert middleware.process_request(r) is None 45 | 46 | with freeze_time("2017-08-31 22:46:01"): 47 | response = middleware.process_request(r) 48 | assert SESSION_TIMEOUT_KEY not in r.session 49 | assert response["location"] == "/accounts/login/?next=/" 50 | 51 | 52 | def test_session_expire_custom_redirect(r, settings): 53 | settings.SESSION_EXPIRE_SECONDS = 3600 54 | settings.SESSION_TIMEOUT_REDIRECT = "/foobar/" 55 | middleware = SessionTimeoutMiddleware() 56 | 57 | with freeze_time("2017-08-31 21:46:00"): 58 | assert middleware.process_request(r) is None 59 | 60 | with freeze_time("2017-08-31 22:46:01"): 61 | response = middleware.process_request(r) 62 | assert response["location"] == "/foobar/" 63 | 64 | 65 | def test_session_expire_no_expire_setting(r, settings): 66 | settings.SESSION_COOKIE_AGE = 3600 67 | middleware = SessionTimeoutMiddleware() 68 | 69 | with freeze_time("2017-08-31 21:46:00"): 70 | assert middleware.process_request(r) is None 71 | 72 | with freeze_time("2017-08-31 22:45:00"): 73 | assert middleware.process_request(r) is None 74 | 75 | with freeze_time("2017-08-31 22:46:01"): 76 | response = middleware.process_request(r) 77 | assert SESSION_TIMEOUT_KEY not in r.session 78 | assert response["location"] == "/accounts/login/?next=/" 79 | 80 | 81 | def test_session_expire_last_activity(r, settings): 82 | settings.SESSION_COOKIE_AGE = 3600 83 | settings.SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True 84 | middleware = SessionTimeoutMiddleware() 85 | 86 | with freeze_time("2017-08-31 20:46:00"): 87 | assert middleware.process_request(r) is None 88 | 89 | with freeze_time("2017-08-31 21:45:00"): 90 | assert middleware.process_request(r) is None 91 | 92 | with freeze_time("2017-08-31 21:46:01"): 93 | assert middleware.process_request(r) is None 94 | 95 | with freeze_time("2017-08-31 23:46:02"): 96 | response = middleware.process_request(r) 97 | assert SESSION_TIMEOUT_KEY not in r.session 98 | assert response["location"] == "/accounts/login/?next=/" 99 | 100 | 101 | def test_session_expire_last_activity_grace_(r, settings): 102 | settings.SESSION_COOKIE_AGE = 3600 103 | settings.SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True 104 | settings.SESSION_EXPIRE_AFTER_LAST_ACTIVITY_GRACE_PERIOD = 90 105 | middleware = SessionTimeoutMiddleware() 106 | 107 | value = None 108 | 109 | with freeze_time("2017-08-31 20:46:00"): 110 | assert middleware.process_request(r) is None 111 | value = r.session[SESSION_TIMEOUT_KEY] 112 | 113 | with freeze_time("2017-08-31 20:47:00"): 114 | assert middleware.process_request(r) is None 115 | assert r.session[SESSION_TIMEOUT_KEY] is value 116 | 117 | with freeze_time("2017-08-31 20:47:31"): 118 | assert middleware.process_request(r) is None 119 | assert r.session[SESSION_TIMEOUT_KEY] is not value 120 | 121 | with freeze_time("2017-08-31 21:47:32"): 122 | response = middleware.process_request(r) 123 | assert SESSION_TIMEOUT_KEY not in r.session 124 | assert response["location"] == "/accounts/login/?next=/" 125 | 126 | 127 | def test_session_expire_last_activity_grace_not_update(r, settings): 128 | settings.SESSION_COOKIE_AGE = 3600 129 | settings.SESSION_EXPIRE_AFTER_LAST_ACTIVITY = True 130 | settings.SESSION_EXPIRE_AFTER_LAST_ACTIVITY_GRACE_PERIOD = 90 131 | middleware = SessionTimeoutMiddleware() 132 | 133 | value = None 134 | 135 | with freeze_time("2017-08-31 20:46:00"): 136 | assert middleware.process_request(r) is None 137 | value = r.session[SESSION_TIMEOUT_KEY] 138 | 139 | with freeze_time("2017-08-31 20:47:00"): 140 | assert middleware.process_request(r) is None 141 | assert r.session[SESSION_TIMEOUT_KEY] is value 142 | 143 | with freeze_time("2017-08-31 21:46:01"): 144 | response = middleware.process_request(r) 145 | assert SESSION_TIMEOUT_KEY not in r.session 146 | assert response["location"] == "/accounts/login/?next=/" 147 | -------------------------------------------------------------------------------- /tests/urls.py: -------------------------------------------------------------------------------- 1 | urlpatterns = [] 2 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py{36,37,38}-django{22,30} 3 | 4 | 5 | [gh-actions] 6 | python = 7 | 3.6: py36 8 | 3.7: py37 9 | 3.8: py38 10 | 11 | [testenv] 12 | commands = coverage run --source django_session_timeout --parallel -m pytest {posargs} 13 | deps = 14 | django22: Django>=2.2,<2.3 15 | django30: Django>=3.0,<3.1 16 | extras = test 17 | 18 | [testenv:coverage-report] 19 | basepython = python3.7 20 | deps = coverage[toml] 21 | skip_install = true 22 | commands = 23 | coverage combine 24 | coverage xml 25 | coverage report 26 | 27 | [testenv:format] 28 | basepython = python3.7 29 | deps = 30 | black 31 | isort[toml] 32 | skip_install = true 33 | commands = 34 | isort --recursive --check-only src tests 35 | black --check src/ tests/ 36 | --------------------------------------------------------------------------------