dbca-utils 2.0.2__tar.gz → 2.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.4
2
+ Name: dbca-utils
3
+ Version: 2.1.0
4
+ Summary: Utilities for DBCA Django apps
5
+ Author-email: Rocky Chen <rocky.chen@dbca.wa.gov.au>, Ashley Felton <ashley.felton@dbca.wa.gov.au>
6
+ Project-URL: Homepage, https://github.com/dbca-wa/dbca-utils
7
+ Project-URL: Repository, https://github.com/dbca-wa/dbca-utils.git
8
+ Project-URL: Changelog, https://github.com/dbca-wa/dbca-utils/blob/master/CHANGELOG.md
9
+ Project-URL: GitHub, https://github.com/dbca-wa/dbca-utils
10
+ Classifier: Framework :: Django
11
+ Classifier: Framework :: Django :: 4.0
12
+ Classifier: Framework :: Django :: 4.2
13
+ Classifier: Framework :: Django :: 5.0
14
+ Classifier: Framework :: Django :: 5.2
15
+ Classifier: Environment :: Web Environment
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Development Status :: 5 - Production/Stable
18
+ Classifier: License :: OSI Approved :: Apache Software License
19
+ Classifier: Programming Language :: Python
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Classifier: Programming Language :: Python :: 3.13
25
+ Classifier: Topic :: Software Development :: Libraries
26
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
+ Requires-Python: <4.0,>=3.10
28
+ Description-Content-Type: text/markdown
29
+ License-File: LICENSE
30
+ Requires-Dist: django<6,>=4
31
+ Dynamic: license-file
32
+
33
+ # Overview
34
+
35
+ DBCA Django utility classes and functions.
36
+
37
+ ## Development
38
+
39
+ The recommended way to set up this project for development is using
40
+ [uv](https://docs.astral.sh/uv/)
41
+ to install and manage a Python virtual environment.
42
+ With uv installed, install the required Python version (see `pyproject.toml`). Example:
43
+
44
+ uv python install 3.12
45
+
46
+ Change into the project directory and run:
47
+
48
+ uv python pin 3.12
49
+ uv sync
50
+
51
+ Activate the virtualenv like so:
52
+
53
+ source .venv/bin/activate
54
+
55
+ Run unit tests using `pytest` (or `tox`, to test against multiple Python versions):
56
+
57
+ pytest -sv
58
+ tox -v
59
+
60
+ ## Releases
61
+
62
+ Tagged releases are built and pushed to PyPI automatically using a GitHub
63
+ workflow in the project. Update the project version in `pyproject.toml` and
64
+ tag the required commit with the same value to trigger a release. Packages
65
+ can also be built and uploaded manually, if desired.
66
+
67
+ Build the project locally using uv, [publish to the PyPI registry](https://docs.astral.sh/uv/guides/publish/#publishing-your-package)
68
+ using the same tool if you require:
69
+
70
+ uv build
71
+ uv publish
72
+
73
+ ## Installation
74
+
75
+ 1. Install via pip/etc.: `pip install dbca-utils`
76
+
77
+ ## SSO Login Middleware
78
+
79
+ This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
80
+ The logout view will redirect to a separate logout page which clears the SSO session.
81
+
82
+ ### Usage
83
+
84
+ Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
85
+ `django.contrib.sessions.middleware.SessionMiddleware` and
86
+ `django.contrib.auth.middleware.AuthenticationMiddleware`.
87
+ Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
88
+ as this middleware depends on it for retrieving the logged in user for a session.
89
+ Note that the middleware will still work without it, but will reauthenticate the session
90
+ on every request, and `request.user.is_authenticated` won't work properly/will be false.
91
+
92
+ Example:
93
+
94
+ ```python
95
+ MIDDLEWARE = [
96
+ ...,
97
+ 'django.contrib.sessions.middleware.SessionMiddleware',
98
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
99
+ 'dbca_utils.middleware.SSOLoginMiddleware'
100
+ ...,
101
+ ]
102
+ ```
103
+
104
+ ## Audit model mixin
105
+
106
+ `AuditMixin` is an extension of `Django.db.model.Model` that adds a number of additional fields:
107
+
108
+ - `creator` - FK to `AUTH_USER_MODEL`, used to record the object creator
109
+ - `modifier` - FK to `AUTH_USER_MODEL`, used to record who the object was last modified by
110
+ - `created` - a timestamp that is set on initial object save
111
+ - `modified` - an auto-updating timestamp (on each object save)
@@ -0,0 +1,79 @@
1
+ # Overview
2
+
3
+ DBCA Django utility classes and functions.
4
+
5
+ ## Development
6
+
7
+ The recommended way to set up this project for development is using
8
+ [uv](https://docs.astral.sh/uv/)
9
+ to install and manage a Python virtual environment.
10
+ With uv installed, install the required Python version (see `pyproject.toml`). Example:
11
+
12
+ uv python install 3.12
13
+
14
+ Change into the project directory and run:
15
+
16
+ uv python pin 3.12
17
+ uv sync
18
+
19
+ Activate the virtualenv like so:
20
+
21
+ source .venv/bin/activate
22
+
23
+ Run unit tests using `pytest` (or `tox`, to test against multiple Python versions):
24
+
25
+ pytest -sv
26
+ tox -v
27
+
28
+ ## Releases
29
+
30
+ Tagged releases are built and pushed to PyPI automatically using a GitHub
31
+ workflow in the project. Update the project version in `pyproject.toml` and
32
+ tag the required commit with the same value to trigger a release. Packages
33
+ can also be built and uploaded manually, if desired.
34
+
35
+ Build the project locally using uv, [publish to the PyPI registry](https://docs.astral.sh/uv/guides/publish/#publishing-your-package)
36
+ using the same tool if you require:
37
+
38
+ uv build
39
+ uv publish
40
+
41
+ ## Installation
42
+
43
+ 1. Install via pip/etc.: `pip install dbca-utils`
44
+
45
+ ## SSO Login Middleware
46
+
47
+ This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
48
+ The logout view will redirect to a separate logout page which clears the SSO session.
49
+
50
+ ### Usage
51
+
52
+ Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
53
+ `django.contrib.sessions.middleware.SessionMiddleware` and
54
+ `django.contrib.auth.middleware.AuthenticationMiddleware`.
55
+ Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
56
+ as this middleware depends on it for retrieving the logged in user for a session.
57
+ Note that the middleware will still work without it, but will reauthenticate the session
58
+ on every request, and `request.user.is_authenticated` won't work properly/will be false.
59
+
60
+ Example:
61
+
62
+ ```python
63
+ MIDDLEWARE = [
64
+ ...,
65
+ 'django.contrib.sessions.middleware.SessionMiddleware',
66
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
67
+ 'dbca_utils.middleware.SSOLoginMiddleware'
68
+ ...,
69
+ ]
70
+ ```
71
+
72
+ ## Audit model mixin
73
+
74
+ `AuditMixin` is an extension of `Django.db.model.Model` that adds a number of additional fields:
75
+
76
+ - `creator` - FK to `AUTH_USER_MODEL`, used to record the object creator
77
+ - `modifier` - FK to `AUTH_USER_MODEL`, used to record who the object was last modified by
78
+ - `created` - a timestamp that is set on initial object save
79
+ - `modified` - an auto-updating timestamp (on each object save)
@@ -0,0 +1,52 @@
1
+ [project]
2
+ name = "dbca-utils"
3
+ version = "2.1.0"
4
+ description = "Utilities for DBCA Django apps"
5
+ authors = [
6
+ { name = "Rocky Chen", email = "rocky.chen@dbca.wa.gov.au" },
7
+ { name = "Ashley Felton", email = "ashley.felton@dbca.wa.gov.au" },
8
+ ]
9
+ readme = "README.md"
10
+ classifiers = [
11
+ "Framework :: Django",
12
+ "Framework :: Django :: 4.0",
13
+ "Framework :: Django :: 4.2",
14
+ "Framework :: Django :: 5.0",
15
+ "Framework :: Django :: 5.2",
16
+ "Environment :: Web Environment",
17
+ "Intended Audience :: Developers",
18
+ "Development Status :: 5 - Production/Stable",
19
+ "License :: OSI Approved :: Apache Software License",
20
+ "Programming Language :: Python",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
26
+ "Topic :: Software Development :: Libraries",
27
+ "Topic :: Software Development :: Libraries :: Python Modules",
28
+ ]
29
+ requires-python = ">=3.10,<4.0"
30
+ dependencies = ["django>=4,<6"]
31
+
32
+ [project.urls]
33
+ Homepage = "https://github.com/dbca-wa/dbca-utils"
34
+ Repository = "https://github.com/dbca-wa/dbca-utils.git"
35
+ Changelog = "https://github.com/dbca-wa/dbca-utils/blob/master/CHANGELOG.md"
36
+ GitHub = "https://github.com/dbca-wa/dbca-utils"
37
+
38
+ [dependency-groups]
39
+ dev = [
40
+ "pytest-django>=4.11.0",
41
+ "pytest-sugar>=1.0.0",
42
+ "tox>=4.25.0",
43
+ "tox-uv>=1.25.0",
44
+ ]
45
+
46
+ [tool.pytest.ini_options]
47
+ # https://pytest-django.readthedocs.io/en/latest/configuring_django.html#pyproject-toml-settings
48
+ DJANGO_SETTINGS_MODULE = "tests.settings"
49
+ # https://pytest-django.readthedocs.io/en/latest/managing_python_path.html#using-pytest-s-pythonpath-option
50
+ pythonpath = ". src"
51
+ # https://pytest-django.readthedocs.io/en/latest/faq.html#my-tests-are-not-being-found-why
52
+ python_files = "tests.py test_*.py"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -1,10 +1,10 @@
1
- from django import http, VERSION
1
+ from django import http
2
2
  from django.conf import settings
3
- from django.contrib.auth import login, logout, get_user_model
3
+ from django.contrib.auth import get_user_model, login, logout
4
+ from django.contrib.auth.middleware import AuthenticationMiddleware, get_user
5
+ from django.utils import timezone
4
6
  from django.utils.deprecation import MiddlewareMixin
5
7
  from django.utils.functional import SimpleLazyObject
6
- from django.utils import timezone
7
- from django.contrib.auth.middleware import AuthenticationMiddleware, get_user
8
8
 
9
9
  from dbca_utils.utils import env
10
10
 
@@ -17,10 +17,14 @@ def sync_usergroups(user, groups):
17
17
  from django.contrib.auth.models import Group
18
18
 
19
19
  usergroups = (
20
- [Group.objects.get_or_create(name=name)[0] for name in groups.split(",")] if groups else []
20
+ [Group.objects.get_or_create(name=name)[0] for name in groups.split(",")]
21
+ if groups
22
+ else []
21
23
  )
22
24
  usergroups.sort(key=lambda o: o.id)
23
- existing_usergroups = list(user.groups.exclude(name__in=LOCAL_USERGROUPS).order_by("id"))
25
+ existing_usergroups = list(
26
+ user.groups.exclude(name__in=LOCAL_USERGROUPS).order_by("id")
27
+ )
24
28
  index1 = 0
25
29
  index2 = 0
26
30
  len1 = len(usergroups)
@@ -96,7 +100,6 @@ class SSOLoginMiddleware(MiddlewareMixin):
96
100
  """
97
101
 
98
102
  def process_request(self, request):
99
-
100
103
  # Logout headers included with request.
101
104
  if (
102
105
  (
@@ -118,10 +121,7 @@ class SSOLoginMiddleware(MiddlewareMixin):
118
121
  # auth2 not enabled
119
122
  return
120
123
 
121
- if VERSION < (2, 0):
122
- user_authenticated = request.user.is_authenticated()
123
- else:
124
- user_authenticated = request.user.is_authenticated
124
+ user_authenticated = request.user.is_authenticated
125
125
 
126
126
  # Auth2 is enabled.
127
127
  # Request user is not authenticated.
@@ -158,7 +158,9 @@ class SSOLoginMiddleware(MiddlewareMixin):
158
158
  user = User.objects.filter(email__iexact=attributemap["email"])[0]
159
159
  elif (
160
160
  User.__name__ != "EmailUser"
161
- and User.objects.filter(username__iexact=attributemap["username"]).exists()
161
+ and User.objects.filter(
162
+ username__iexact=attributemap["username"]
163
+ ).exists()
162
164
  ):
163
165
  user = User.objects.filter(username__iexact=attributemap["username"])[0]
164
166
  else:
@@ -1,15 +1,23 @@
1
- import ast
2
1
  import os
2
+ from ast import literal_eval
3
3
 
4
4
 
5
5
  def env(key, default=None, required=False, value_type=None):
6
6
  """
7
7
  Retrieves environment variables and returns Python natives. The (optional)
8
- default will be returned if the environment variable does not exist.
8
+ `default` value will be returned if the environment variable does not exist.
9
+ Setting `required` will cause an Exception to be thrown if the variable does
10
+ not exist.
11
+ Setting `value_type` will try to ensure that the returned value is of the
12
+ nominated type (within reason).
13
+ Supported Python object types: str, list, tuple, bool, int, float.
9
14
  """
15
+ value = None
16
+
10
17
  try:
11
18
  value = os.environ[key]
12
- value = ast.literal_eval(value)
19
+ # Evaluate the environment variable value as a Python object.
20
+ value = literal_eval(value)
13
21
  except (SyntaxError, ValueError):
14
22
  pass
15
23
  except KeyError:
@@ -17,14 +25,17 @@ def env(key, default=None, required=False, value_type=None):
17
25
  return default
18
26
  raise Exception(f"Missing required environment variable {key}")
19
27
 
20
- if value_type is None:
21
- if default is not None:
22
- value_type = default.__class__
28
+ if value_type is None and default is not None:
29
+ # If we've passed a default return value but not set a value_type, use the
30
+ # default's type.
31
+ value_type = default.__class__
23
32
 
24
33
  if value_type is None:
25
34
  return value
26
35
  elif isinstance(value, value_type):
27
36
  return value
37
+ elif issubclass(value_type, str):
38
+ return str(value)
28
39
  elif issubclass(value_type, list):
29
40
  if isinstance(value, tuple):
30
41
  return list(value)
@@ -53,7 +64,7 @@ def env(key, default=None, required=False, value_type=None):
53
64
  return False
54
65
  else:
55
66
  raise Exception(
56
- f"{key} is a boolean environment variable and only accepts 'true' ,'false' and '' (case-insensitive), but the configured value is '{value}'"
67
+ f"{key} is a boolean environment variable and only accepts 'true', 'false' or '' (case-insensitive), but the configured value is '{value}'"
57
68
  )
58
69
  elif issubclass(value_type, int):
59
70
  return int(value)
@@ -61,5 +72,5 @@ def env(key, default=None, required=False, value_type=None):
61
72
  return float(value)
62
73
  else:
63
74
  raise Exception(
64
- f"{key} is a {value_type} environment variable, but {value_type} is not supported now"
75
+ f"{key} is a {value_type} environment variable, but {value_type} is not supported"
65
76
  )
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.4
2
+ Name: dbca-utils
3
+ Version: 2.1.0
4
+ Summary: Utilities for DBCA Django apps
5
+ Author-email: Rocky Chen <rocky.chen@dbca.wa.gov.au>, Ashley Felton <ashley.felton@dbca.wa.gov.au>
6
+ Project-URL: Homepage, https://github.com/dbca-wa/dbca-utils
7
+ Project-URL: Repository, https://github.com/dbca-wa/dbca-utils.git
8
+ Project-URL: Changelog, https://github.com/dbca-wa/dbca-utils/blob/master/CHANGELOG.md
9
+ Project-URL: GitHub, https://github.com/dbca-wa/dbca-utils
10
+ Classifier: Framework :: Django
11
+ Classifier: Framework :: Django :: 4.0
12
+ Classifier: Framework :: Django :: 4.2
13
+ Classifier: Framework :: Django :: 5.0
14
+ Classifier: Framework :: Django :: 5.2
15
+ Classifier: Environment :: Web Environment
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Development Status :: 5 - Production/Stable
18
+ Classifier: License :: OSI Approved :: Apache Software License
19
+ Classifier: Programming Language :: Python
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Classifier: Programming Language :: Python :: 3.13
25
+ Classifier: Topic :: Software Development :: Libraries
26
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
+ Requires-Python: <4.0,>=3.10
28
+ Description-Content-Type: text/markdown
29
+ License-File: LICENSE
30
+ Requires-Dist: django<6,>=4
31
+ Dynamic: license-file
32
+
33
+ # Overview
34
+
35
+ DBCA Django utility classes and functions.
36
+
37
+ ## Development
38
+
39
+ The recommended way to set up this project for development is using
40
+ [uv](https://docs.astral.sh/uv/)
41
+ to install and manage a Python virtual environment.
42
+ With uv installed, install the required Python version (see `pyproject.toml`). Example:
43
+
44
+ uv python install 3.12
45
+
46
+ Change into the project directory and run:
47
+
48
+ uv python pin 3.12
49
+ uv sync
50
+
51
+ Activate the virtualenv like so:
52
+
53
+ source .venv/bin/activate
54
+
55
+ Run unit tests using `pytest` (or `tox`, to test against multiple Python versions):
56
+
57
+ pytest -sv
58
+ tox -v
59
+
60
+ ## Releases
61
+
62
+ Tagged releases are built and pushed to PyPI automatically using a GitHub
63
+ workflow in the project. Update the project version in `pyproject.toml` and
64
+ tag the required commit with the same value to trigger a release. Packages
65
+ can also be built and uploaded manually, if desired.
66
+
67
+ Build the project locally using uv, [publish to the PyPI registry](https://docs.astral.sh/uv/guides/publish/#publishing-your-package)
68
+ using the same tool if you require:
69
+
70
+ uv build
71
+ uv publish
72
+
73
+ ## Installation
74
+
75
+ 1. Install via pip/etc.: `pip install dbca-utils`
76
+
77
+ ## SSO Login Middleware
78
+
79
+ This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
80
+ The logout view will redirect to a separate logout page which clears the SSO session.
81
+
82
+ ### Usage
83
+
84
+ Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
85
+ `django.contrib.sessions.middleware.SessionMiddleware` and
86
+ `django.contrib.auth.middleware.AuthenticationMiddleware`.
87
+ Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
88
+ as this middleware depends on it for retrieving the logged in user for a session.
89
+ Note that the middleware will still work without it, but will reauthenticate the session
90
+ on every request, and `request.user.is_authenticated` won't work properly/will be false.
91
+
92
+ Example:
93
+
94
+ ```python
95
+ MIDDLEWARE = [
96
+ ...,
97
+ 'django.contrib.sessions.middleware.SessionMiddleware',
98
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
99
+ 'dbca_utils.middleware.SSOLoginMiddleware'
100
+ ...,
101
+ ]
102
+ ```
103
+
104
+ ## Audit model mixin
105
+
106
+ `AuditMixin` is an extension of `Django.db.model.Model` that adds a number of additional fields:
107
+
108
+ - `creator` - FK to `AUTH_USER_MODEL`, used to record the object creator
109
+ - `modifier` - FK to `AUTH_USER_MODEL`, used to record who the object was last modified by
110
+ - `created` - a timestamp that is set on initial object save
111
+ - `modified` - an auto-updating timestamp (on each object save)
@@ -0,0 +1,13 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/dbca_utils/__init__.py
5
+ src/dbca_utils/middleware.py
6
+ src/dbca_utils/models.py
7
+ src/dbca_utils/utils.py
8
+ src/dbca_utils.egg-info/PKG-INFO
9
+ src/dbca_utils.egg-info/SOURCES.txt
10
+ src/dbca_utils.egg-info/dependency_links.txt
11
+ src/dbca_utils.egg-info/requires.txt
12
+ src/dbca_utils.egg-info/top_level.txt
13
+ tests/tests.py
@@ -0,0 +1 @@
1
+ django<6,>=4
@@ -0,0 +1,111 @@
1
+ import os
2
+ import random
3
+ import string
4
+
5
+ import pytest
6
+ from django.contrib.auth.models import User
7
+ from django.test import TestCase
8
+ from django.test.client import Client
9
+ from django.urls import reverse
10
+
11
+ from dbca_utils.utils import env
12
+
13
+ from .models import TestModel
14
+
15
+ # Define an environment variable for testing.
16
+ letters = string.ascii_letters
17
+ TEST_VAR = "".join(random.choice(letters) for _ in range(128))
18
+ TEST_NAME = "".join(random.choice(letters) for _ in range(128))
19
+ os.environ["TEST_ENVIRONMENT_VAR"] = TEST_VAR
20
+
21
+ os.environ["TEST_STR"] = "string"
22
+ os.environ["TEST_INT"] = "42"
23
+ os.environ["TEST_FLOAT"] = "3.14159"
24
+ os.environ["TEST_LIST"] = "[1,2,3,4,5]"
25
+ os.environ["TEST_TUPLE"] = "('a', 'b', 'c')"
26
+ os.environ["TEST_BOOL"] = "False"
27
+
28
+
29
+ class TestUtils(TestCase):
30
+ def test_env_returns_str(self):
31
+ test_str = env("TEST_STR")
32
+ self.assertTrue(isinstance(test_str, str))
33
+
34
+ def test_env_returns_int(self):
35
+ test_int = env("TEST_INT")
36
+ self.assertTrue(isinstance(test_int, int))
37
+
38
+ def test_env_returns_float(self):
39
+ test_float = env("TEST_FLOAT")
40
+ self.assertTrue(isinstance(test_float, float))
41
+
42
+ def test_env_returns_list(self):
43
+ test_list = env("TEST_LIST")
44
+ self.assertTrue(isinstance(test_list, list))
45
+
46
+ def test_env_returns_tuple(self):
47
+ test_tuple = env("TEST_TUPLE")
48
+ self.assertTrue(isinstance(test_tuple, tuple))
49
+
50
+ def test_env_returns_bool(self):
51
+ test_bool = env("TEST_BOOL")
52
+ self.assertTrue(isinstance(test_bool, bool))
53
+
54
+ def test_env_returns_default(self):
55
+ test_str = env("TEST_MISSING", "foo")
56
+ self.assertTrue(isinstance(test_str, str))
57
+
58
+ def test_env_missing_not_required_no_default(self):
59
+ test_env = env("TEST_MISSING")
60
+ self.assertIsNone(test_env)
61
+
62
+ def test_env_required_throws_exception(self):
63
+ with pytest.raises(Exception):
64
+ env("TEST_MISSING_REQUIRED", required=True)
65
+
66
+ def test_env_returns_other_as_str(self):
67
+ test_str = env("TEST_FLOAT", value_type=str)
68
+ self.assertTrue(isinstance(test_str, str))
69
+
70
+
71
+ class TestModelTests(TestCase):
72
+ client = Client()
73
+ model = TestModel
74
+
75
+ def setUp(self):
76
+ self.user = User.objects.create_user(
77
+ username="test", email="test@email.com", password="secret"
78
+ )
79
+ self.test_model = TestModel.objects.create(name=TEST_NAME)
80
+ self.client.login(username="test", password="secret")
81
+
82
+ def tearDown(self):
83
+ self.user.delete()
84
+
85
+ def test_model_fields(self):
86
+ """Test a model inheriting from mixins has the required fields."""
87
+ self.assertTrue(hasattr(self.test_model, "effective_to"))
88
+ self.assertTrue(hasattr(self.test_model, "creator"))
89
+ self.assertTrue(hasattr(self.test_model, "modifier"))
90
+ self.assertTrue(hasattr(self.test_model, "created"))
91
+ self.assertTrue(hasattr(self.test_model, "modified"))
92
+
93
+ def test_active_mixin(self):
94
+ """Test the ActiveMixin manager methods."""
95
+ obj_del = TestModel.objects.create(name="Deleted object")
96
+ obj_del.delete()
97
+ all_pks = [i.pk for i in TestModel.objects.all()]
98
+ current_pks = [i.pk for i in TestModel.objects.current()]
99
+ del_pks = [i.pk for i in TestModel.objects.deleted()]
100
+ self.assertTrue(obj_del.pk in all_pks)
101
+ self.assertFalse(obj_del.pk in current_pks)
102
+ self.assertTrue(obj_del.pk in del_pks)
103
+ self.assertFalse(self.test_model.pk in del_pks)
104
+
105
+ def test_url_request_returns_view(self):
106
+ """Test the env() utility method works as expected."""
107
+ url = reverse("test_model_list")
108
+ response = self.client.get(url)
109
+ self.assertEqual(response.status_code, 200)
110
+ self.assertContains(response, TEST_VAR)
111
+ self.assertContains(response, TEST_NAME)
@@ -1,2 +0,0 @@
1
- include README.md
2
- include LICENSE
dbca-utils-2.0.2/PKG-INFO DELETED
@@ -1,73 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: dbca-utils
3
- Version: 2.0.2
4
- Summary: Utilities for Django/Python apps
5
- Home-page: https://github.com/dbca-wa/dbca-utils
6
- Author: Department of Biodiversity, Conservation and Attractions
7
- Author-email: asi@dbca.wa.gov.au
8
- Maintainer: Department of Biodiversity, Conservation and Attractions
9
- Maintainer-email: asi@dbca.wa.gov.au
10
- License: Apache License, Version 2.0
11
- Keywords: django,middleware,utility
12
- Platform: UNKNOWN
13
- Classifier: Framework :: Django
14
- Classifier: Framework :: Django :: 3.2
15
- Classifier: Framework :: Django :: 4.0
16
- Classifier: Framework :: Django :: 4.2
17
- Classifier: Framework :: Django :: 5.0
18
- Classifier: Environment :: Web Environment
19
- Classifier: Intended Audience :: Developers
20
- Classifier: Development Status :: 5 - Production/Stable
21
- Classifier: License :: OSI Approved :: Apache Software License
22
- Classifier: Programming Language :: Python
23
- Classifier: Programming Language :: Python :: 3
24
- Classifier: Programming Language :: Python :: 3.9
25
- Classifier: Programming Language :: Python :: 3.10
26
- Classifier: Programming Language :: Python :: 3.11
27
- Classifier: Programming Language :: Python :: 3.12
28
- Classifier: Topic :: Software Development :: Libraries
29
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
30
- Description-Content-Type: text/markdown
31
- License-File: LICENSE
32
-
33
- # Overview
34
-
35
- DBCA Django utility classes and functions.
36
-
37
- # SSO Login Middleware
38
-
39
- This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
40
- The logout view will redirect to a separate logout page which clears the SSO session.
41
-
42
- ## Usage
43
-
44
- Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
45
- `django.contrib.sessions.middleware.SessionMiddleware` and
46
- `django.contrib.auth.middleware.AuthenticationMiddleware`.
47
- Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
48
- as this middleware depends on it for retrieving the logged in user for a session.
49
- Note that the middleware will still work without it, but will reauthenticate the session
50
- on every request, and `request.user.is_authenticated` won't work properly/will be false.
51
-
52
- Example:
53
-
54
- ```
55
- MIDDLEWARE = [
56
- ...,
57
- 'django.contrib.sessions.middleware.SessionMiddleware',
58
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
59
- 'dbca_utils.middleware.SSOLoginMiddleware'
60
- ...,
61
- ]
62
- ```
63
-
64
- # Audit model mixin
65
-
66
- ``AuditMixin`` is an extension of ``Django.db.model.Model`` that adds a number of additional fields:
67
-
68
- * creator - FK to ``AUTH_USER_MODEL``, used to record the object creator
69
- * modifier - FK to ``AUTH_USER_MODEL``, used to record who the object was last modified by
70
- * created - a timestamp that is set on initial object save
71
- * modified - an auto-updating timestamp (on each object save)
72
-
73
-
@@ -1,39 +0,0 @@
1
- # Overview
2
-
3
- DBCA Django utility classes and functions.
4
-
5
- # SSO Login Middleware
6
-
7
- This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
8
- The logout view will redirect to a separate logout page which clears the SSO session.
9
-
10
- ## Usage
11
-
12
- Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
13
- `django.contrib.sessions.middleware.SessionMiddleware` and
14
- `django.contrib.auth.middleware.AuthenticationMiddleware`.
15
- Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
16
- as this middleware depends on it for retrieving the logged in user for a session.
17
- Note that the middleware will still work without it, but will reauthenticate the session
18
- on every request, and `request.user.is_authenticated` won't work properly/will be false.
19
-
20
- Example:
21
-
22
- ```
23
- MIDDLEWARE = [
24
- ...,
25
- 'django.contrib.sessions.middleware.SessionMiddleware',
26
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
27
- 'dbca_utils.middleware.SSOLoginMiddleware'
28
- ...,
29
- ]
30
- ```
31
-
32
- # Audit model mixin
33
-
34
- ``AuditMixin`` is an extension of ``Django.db.model.Model`` that adds a number of additional fields:
35
-
36
- * creator - FK to ``AUTH_USER_MODEL``, used to record the object creator
37
- * modifier - FK to ``AUTH_USER_MODEL``, used to record who the object was last modified by
38
- * created - a timestamp that is set on initial object save
39
- * modified - an auto-updating timestamp (on each object save)
@@ -1,73 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: dbca-utils
3
- Version: 2.0.2
4
- Summary: Utilities for Django/Python apps
5
- Home-page: https://github.com/dbca-wa/dbca-utils
6
- Author: Department of Biodiversity, Conservation and Attractions
7
- Author-email: asi@dbca.wa.gov.au
8
- Maintainer: Department of Biodiversity, Conservation and Attractions
9
- Maintainer-email: asi@dbca.wa.gov.au
10
- License: Apache License, Version 2.0
11
- Keywords: django,middleware,utility
12
- Platform: UNKNOWN
13
- Classifier: Framework :: Django
14
- Classifier: Framework :: Django :: 3.2
15
- Classifier: Framework :: Django :: 4.0
16
- Classifier: Framework :: Django :: 4.2
17
- Classifier: Framework :: Django :: 5.0
18
- Classifier: Environment :: Web Environment
19
- Classifier: Intended Audience :: Developers
20
- Classifier: Development Status :: 5 - Production/Stable
21
- Classifier: License :: OSI Approved :: Apache Software License
22
- Classifier: Programming Language :: Python
23
- Classifier: Programming Language :: Python :: 3
24
- Classifier: Programming Language :: Python :: 3.9
25
- Classifier: Programming Language :: Python :: 3.10
26
- Classifier: Programming Language :: Python :: 3.11
27
- Classifier: Programming Language :: Python :: 3.12
28
- Classifier: Topic :: Software Development :: Libraries
29
- Classifier: Topic :: Software Development :: Libraries :: Python Modules
30
- Description-Content-Type: text/markdown
31
- License-File: LICENSE
32
-
33
- # Overview
34
-
35
- DBCA Django utility classes and functions.
36
-
37
- # SSO Login Middleware
38
-
39
- This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
40
- The logout view will redirect to a separate logout page which clears the SSO session.
41
-
42
- ## Usage
43
-
44
- Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
45
- `django.contrib.sessions.middleware.SessionMiddleware` and
46
- `django.contrib.auth.middleware.AuthenticationMiddleware`.
47
- Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
48
- as this middleware depends on it for retrieving the logged in user for a session.
49
- Note that the middleware will still work without it, but will reauthenticate the session
50
- on every request, and `request.user.is_authenticated` won't work properly/will be false.
51
-
52
- Example:
53
-
54
- ```
55
- MIDDLEWARE = [
56
- ...,
57
- 'django.contrib.sessions.middleware.SessionMiddleware',
58
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
59
- 'dbca_utils.middleware.SSOLoginMiddleware'
60
- ...,
61
- ]
62
- ```
63
-
64
- # Audit model mixin
65
-
66
- ``AuditMixin`` is an extension of ``Django.db.model.Model`` that adds a number of additional fields:
67
-
68
- * creator - FK to ``AUTH_USER_MODEL``, used to record the object creator
69
- * modifier - FK to ``AUTH_USER_MODEL``, used to record who the object was last modified by
70
- * created - a timestamp that is set on initial object save
71
- * modified - an auto-updating timestamp (on each object save)
72
-
73
-
@@ -1,15 +0,0 @@
1
- LICENSE
2
- MANIFEST.in
3
- README.md
4
- setup.cfg
5
- setup.py
6
- dbca_utils/__init__.py
7
- dbca_utils/middleware.py
8
- dbca_utils/models.py
9
- dbca_utils/utils.py
10
- dbca_utils.egg-info/PKG-INFO
11
- dbca_utils.egg-info/SOURCES.txt
12
- dbca_utils.egg-info/dependency_links.txt
13
- dbca_utils.egg-info/not-zip-safe
14
- dbca_utils.egg-info/requires.txt
15
- dbca_utils.egg-info/top_level.txt
@@ -1 +0,0 @@
1
- Django>=3.2
@@ -1,10 +0,0 @@
1
- [metadata]
2
- license-file = LICENSE
3
-
4
- [flake8]
5
- ignore = E302,E501,W503
6
-
7
- [egg_info]
8
- tag_build =
9
- tag_date = 0
10
-
dbca-utils-2.0.2/setup.py DELETED
@@ -1,47 +0,0 @@
1
- #!/usr/bin/env python
2
-
3
- from setuptools import setup
4
- from pathlib import Path
5
-
6
- # Read the contents of the README file:
7
- this_directory = Path(__file__).parent
8
- long_description = (this_directory / "README.md").read_text()
9
-
10
- setup(
11
- name='dbca-utils',
12
- version='2.0.2',
13
- packages=['dbca_utils'],
14
- description='Utilities for Django/Python apps',
15
- long_description=long_description,
16
- long_description_content_type='text/markdown',
17
- url='https://github.com/dbca-wa/dbca-utils',
18
- author='Department of Biodiversity, Conservation and Attractions',
19
- author_email='asi@dbca.wa.gov.au',
20
- maintainer='Department of Biodiversity, Conservation and Attractions',
21
- maintainer_email='asi@dbca.wa.gov.au',
22
- license='Apache License, Version 2.0',
23
- zip_safe=False,
24
- keywords=['django', 'middleware', 'utility'],
25
- install_requires=[
26
- 'Django>=3.2',
27
- ],
28
- classifiers=[
29
- 'Framework :: Django',
30
- 'Framework :: Django :: 3.2',
31
- 'Framework :: Django :: 4.0',
32
- 'Framework :: Django :: 4.2',
33
- 'Framework :: Django :: 5.0',
34
- 'Environment :: Web Environment',
35
- 'Intended Audience :: Developers',
36
- 'Development Status :: 5 - Production/Stable',
37
- 'License :: OSI Approved :: Apache Software License',
38
- 'Programming Language :: Python',
39
- 'Programming Language :: Python :: 3',
40
- 'Programming Language :: Python :: 3.9',
41
- 'Programming Language :: Python :: 3.10',
42
- 'Programming Language :: Python :: 3.11',
43
- 'Programming Language :: Python :: 3.12',
44
- 'Topic :: Software Development :: Libraries',
45
- 'Topic :: Software Development :: Libraries :: Python Modules',
46
- ],
47
- )
File without changes