dbca-utils 2.0.3__py3-none-any.whl
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.
- dbca_utils/__init__.py +0 -0
- dbca_utils/middleware.py +179 -0
- dbca_utils/models.py +97 -0
- dbca_utils/utils.py +65 -0
- dbca_utils-2.0.3.dist-info/LICENSE +201 -0
- dbca_utils-2.0.3.dist-info/METADATA +96 -0
- dbca_utils-2.0.3.dist-info/RECORD +8 -0
- dbca_utils-2.0.3.dist-info/WHEEL +4 -0
dbca_utils/__init__.py
ADDED
|
File without changes
|
dbca_utils/middleware.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
from django import http, VERSION
|
|
2
|
+
from django.conf import settings
|
|
3
|
+
from django.contrib.auth import login, logout, get_user_model
|
|
4
|
+
from django.utils.deprecation import MiddlewareMixin
|
|
5
|
+
from django.utils.functional import SimpleLazyObject
|
|
6
|
+
from django.utils import timezone
|
|
7
|
+
from django.contrib.auth.middleware import AuthenticationMiddleware, get_user
|
|
8
|
+
|
|
9
|
+
from dbca_utils.utils import env
|
|
10
|
+
|
|
11
|
+
ENABLE_AUTH2_GROUPS = env("ENABLE_AUTH2_GROUPS", default=False)
|
|
12
|
+
LOCAL_USERGROUPS = env("LOCAL_USERGROUPS", default=[])
|
|
13
|
+
User = get_user_model()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def sync_usergroups(user, groups):
|
|
17
|
+
from django.contrib.auth.models import Group
|
|
18
|
+
|
|
19
|
+
usergroups = (
|
|
20
|
+
[Group.objects.get_or_create(name=name)[0] for name in groups.split(",")] if groups else []
|
|
21
|
+
)
|
|
22
|
+
usergroups.sort(key=lambda o: o.id)
|
|
23
|
+
existing_usergroups = list(user.groups.exclude(name__in=LOCAL_USERGROUPS).order_by("id"))
|
|
24
|
+
index1 = 0
|
|
25
|
+
index2 = 0
|
|
26
|
+
len1 = len(usergroups)
|
|
27
|
+
len2 = len(existing_usergroups)
|
|
28
|
+
|
|
29
|
+
while True:
|
|
30
|
+
group1 = usergroups[index1] if index1 < len1 else None
|
|
31
|
+
group2 = existing_usergroups[index2] if index2 < len2 else None
|
|
32
|
+
if not group1 and not group2:
|
|
33
|
+
break
|
|
34
|
+
if not group1:
|
|
35
|
+
user.groups.remove(group2)
|
|
36
|
+
index2 += 1
|
|
37
|
+
elif not group2:
|
|
38
|
+
user.groups.add(group1)
|
|
39
|
+
index1 += 1
|
|
40
|
+
elif group1.id == group2.id:
|
|
41
|
+
index1 += 1
|
|
42
|
+
index2 += 1
|
|
43
|
+
elif group1.id < group2.id:
|
|
44
|
+
user.groups.add(group1)
|
|
45
|
+
index1 += 1
|
|
46
|
+
else:
|
|
47
|
+
user.groups.remove(group2)
|
|
48
|
+
index2 += 1
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class SimpleLazyUser(SimpleLazyObject):
|
|
52
|
+
def __init__(self, func, request, groups):
|
|
53
|
+
super().__init__(func)
|
|
54
|
+
self.request = request
|
|
55
|
+
self.usergroups = groups
|
|
56
|
+
|
|
57
|
+
def __getattr__(self, name):
|
|
58
|
+
if name == "groups":
|
|
59
|
+
sync_usergroups(self._wrapped, self.usergroups)
|
|
60
|
+
self.request.session["usergroups"] = self.usergroups
|
|
61
|
+
|
|
62
|
+
return super().__getattr__(name)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# Monkey patch AuthenticationMiddleware to add logic to process user groups.
|
|
66
|
+
if ENABLE_AUTH2_GROUPS:
|
|
67
|
+
original_process_request = AuthenticationMiddleware.process_request
|
|
68
|
+
|
|
69
|
+
def _process_request(self, request):
|
|
70
|
+
if "HTTP_X_GROUPS" in request.META:
|
|
71
|
+
groups = request.META["HTTP_X_GROUPS"] or None
|
|
72
|
+
existing_groups = request.session.get("usergroups")
|
|
73
|
+
if groups != existing_groups:
|
|
74
|
+
# User group is changed.
|
|
75
|
+
request.user = SimpleLazyUser(
|
|
76
|
+
lambda: get_user(request), request, groups
|
|
77
|
+
)
|
|
78
|
+
return
|
|
79
|
+
original_process_request(self, request)
|
|
80
|
+
|
|
81
|
+
AuthenticationMiddleware.process_request = _process_request
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class SSOLoginMiddleware(MiddlewareMixin):
|
|
85
|
+
"""Django middleware to process HTTP requests containing headers set by the Auth2
|
|
86
|
+
SSO service, specificially:
|
|
87
|
+
- `HTTP_REMOTE_USER`
|
|
88
|
+
- `HTTP_X_LAST_NAME`
|
|
89
|
+
- `HTTP_X_FIRST_NAME`
|
|
90
|
+
- `HTTP_X_EMAIL`
|
|
91
|
+
The middleware assesses requests containing these headers, and (having deferred user
|
|
92
|
+
authentication to the upstream service), retrieves the local Django User and logs
|
|
93
|
+
the user in automatically.
|
|
94
|
+
If the request path starts with one of the defined logout paths and a `HTTP_X_LOGOUT_URL`
|
|
95
|
+
value is set in the response, log out the user and redirect to that URL instead.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
def process_request(self, request):
|
|
99
|
+
|
|
100
|
+
# Logout headers included with request.
|
|
101
|
+
if (
|
|
102
|
+
(
|
|
103
|
+
request.path.startswith("/logout")
|
|
104
|
+
or request.path.startswith("/admin/logout")
|
|
105
|
+
or request.path.startswith("/ledger/logout")
|
|
106
|
+
)
|
|
107
|
+
and "HTTP_X_LOGOUT_URL" in request.META
|
|
108
|
+
and request.META["HTTP_X_LOGOUT_URL"]
|
|
109
|
+
):
|
|
110
|
+
logout(request)
|
|
111
|
+
return http.HttpResponseRedirect(request.META["HTTP_X_LOGOUT_URL"])
|
|
112
|
+
|
|
113
|
+
# Auth2 is not enabled, skip further processing.
|
|
114
|
+
if (
|
|
115
|
+
"HTTP_REMOTE_USER" not in request.META
|
|
116
|
+
or not request.META["HTTP_REMOTE_USER"]
|
|
117
|
+
):
|
|
118
|
+
# auth2 not enabled
|
|
119
|
+
return
|
|
120
|
+
|
|
121
|
+
if VERSION < (2, 0):
|
|
122
|
+
user_authenticated = request.user.is_authenticated()
|
|
123
|
+
else:
|
|
124
|
+
user_authenticated = request.user.is_authenticated
|
|
125
|
+
|
|
126
|
+
# Auth2 is enabled.
|
|
127
|
+
# Request user is not authenticated.
|
|
128
|
+
if not user_authenticated:
|
|
129
|
+
attributemap = {
|
|
130
|
+
"username": "HTTP_REMOTE_USER",
|
|
131
|
+
"last_name": "HTTP_X_LAST_NAME",
|
|
132
|
+
"first_name": "HTTP_X_FIRST_NAME",
|
|
133
|
+
"email": "HTTP_X_EMAIL",
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
for key, value in attributemap.items():
|
|
137
|
+
if value in request.META:
|
|
138
|
+
attributemap[key] = request.META[value]
|
|
139
|
+
|
|
140
|
+
# Optional setting: projects may define accepted user email domains either as
|
|
141
|
+
# a list of strings, or a single string.
|
|
142
|
+
if (
|
|
143
|
+
hasattr(settings, "ALLOWED_EMAIL_SUFFIXES")
|
|
144
|
+
and settings.ALLOWED_EMAIL_SUFFIXES
|
|
145
|
+
):
|
|
146
|
+
allowed = settings.ALLOWED_EMAIL_SUFFIXES
|
|
147
|
+
if isinstance(settings.ALLOWED_EMAIL_SUFFIXES, str):
|
|
148
|
+
allowed = [settings.ALLOWED_EMAIL_SUFFIXES]
|
|
149
|
+
if not any(
|
|
150
|
+
[attributemap["email"].lower().endswith(x) for x in allowed]
|
|
151
|
+
):
|
|
152
|
+
return http.HttpResponseForbidden()
|
|
153
|
+
|
|
154
|
+
if (
|
|
155
|
+
attributemap["email"]
|
|
156
|
+
and User.objects.filter(email__iexact=attributemap["email"]).exists()
|
|
157
|
+
):
|
|
158
|
+
user = User.objects.filter(email__iexact=attributemap["email"])[0]
|
|
159
|
+
elif (
|
|
160
|
+
User.__name__ != "EmailUser"
|
|
161
|
+
and User.objects.filter(username__iexact=attributemap["username"]).exists()
|
|
162
|
+
):
|
|
163
|
+
user = User.objects.filter(username__iexact=attributemap["username"])[0]
|
|
164
|
+
else:
|
|
165
|
+
user = User(last_login=timezone.localtime())
|
|
166
|
+
|
|
167
|
+
# Set the user's details from the supplied information.
|
|
168
|
+
user.__dict__.update(attributemap)
|
|
169
|
+
user.save()
|
|
170
|
+
user.backend = "django.contrib.auth.backends.ModelBackend"
|
|
171
|
+
|
|
172
|
+
# Log the user in.
|
|
173
|
+
login(request, user)
|
|
174
|
+
|
|
175
|
+
# Synchronize the user groups
|
|
176
|
+
if ENABLE_AUTH2_GROUPS and "HTTP_X_GROUPS" in request.META:
|
|
177
|
+
groups = request.META["HTTP_X_GROUPS"] or None
|
|
178
|
+
sync_usergroups(user, groups)
|
|
179
|
+
request.session["usergroups"] = groups
|
dbca_utils/models.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from django.conf import settings
|
|
2
|
+
from django.db import models
|
|
3
|
+
from django.utils import timezone
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ActiveMixinManager(models.Manager):
|
|
7
|
+
"""Manager class for ActiveMixin."""
|
|
8
|
+
|
|
9
|
+
def current(self):
|
|
10
|
+
return self.filter(effective_to=None)
|
|
11
|
+
|
|
12
|
+
def deleted(self):
|
|
13
|
+
return self.filter(effective_to__isnull=False)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ActiveMixin(models.Model):
|
|
17
|
+
"""Model mixin to allow objects to be saved as 'non-current' or 'inactive',
|
|
18
|
+
instead of deleting those objects.
|
|
19
|
+
The standard model delete() method is overridden.
|
|
20
|
+
|
|
21
|
+
"effective_to" is used to flag 'deleted' objects (not null==deleted).
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
effective_to = models.DateTimeField(null=True, blank=True)
|
|
25
|
+
objects = ActiveMixinManager()
|
|
26
|
+
|
|
27
|
+
class Meta:
|
|
28
|
+
abstract = True
|
|
29
|
+
|
|
30
|
+
def is_active(self):
|
|
31
|
+
return self.effective_to is None
|
|
32
|
+
|
|
33
|
+
def is_deleted(self):
|
|
34
|
+
return not self.is_active()
|
|
35
|
+
|
|
36
|
+
def delete(self, *args, **kwargs):
|
|
37
|
+
"""Overide the standard delete method; sets effective_to the current
|
|
38
|
+
date and time.
|
|
39
|
+
"""
|
|
40
|
+
if "force" in kwargs and kwargs["force"]:
|
|
41
|
+
kwargs.pop("force", None)
|
|
42
|
+
super(ActiveMixin, self).delete(*args, **kwargs)
|
|
43
|
+
else:
|
|
44
|
+
self.effective_to = timezone.now()
|
|
45
|
+
super(ActiveMixin, self).save(*args, **kwargs)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class AuditMixin(models.Model):
|
|
49
|
+
"""Model mixin to update creation/modification datestamp and user
|
|
50
|
+
automatically on save.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
creator = models.ForeignKey(
|
|
54
|
+
settings.AUTH_USER_MODEL,
|
|
55
|
+
blank=True,
|
|
56
|
+
null=True,
|
|
57
|
+
on_delete=models.PROTECT,
|
|
58
|
+
related_name="%(app_label)s_%(class)s_created",
|
|
59
|
+
editable=False,
|
|
60
|
+
)
|
|
61
|
+
modifier = models.ForeignKey(
|
|
62
|
+
settings.AUTH_USER_MODEL,
|
|
63
|
+
blank=True,
|
|
64
|
+
null=True,
|
|
65
|
+
on_delete=models.PROTECT,
|
|
66
|
+
related_name="%(app_label)s_%(class)s_modified",
|
|
67
|
+
editable=False,
|
|
68
|
+
)
|
|
69
|
+
created = models.DateTimeField(default=timezone.now, editable=False)
|
|
70
|
+
modified = models.DateTimeField(auto_now=True, editable=False)
|
|
71
|
+
|
|
72
|
+
class Meta:
|
|
73
|
+
abstract = True
|
|
74
|
+
|
|
75
|
+
def __init__(self, *args, **kwargs):
|
|
76
|
+
super(AuditMixin, self).__init__(*args, **kwargs)
|
|
77
|
+
self._initial = {}
|
|
78
|
+
if self.pk:
|
|
79
|
+
for field in self._meta.fields:
|
|
80
|
+
self._initial[field.attname] = getattr(self, field.attname)
|
|
81
|
+
|
|
82
|
+
def has_changed(self):
|
|
83
|
+
"""Returns True if the current data object differs from saved."""
|
|
84
|
+
return bool(self.changed_data)
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def changed_data(self):
|
|
88
|
+
"""Returns a list of fields with data that differs from initial
|
|
89
|
+
values. May be utilised by revision mechanisms, as required.
|
|
90
|
+
"""
|
|
91
|
+
self._changed_data = []
|
|
92
|
+
for field, value in self._initial.items():
|
|
93
|
+
if field in ["modified", "modifier_id"]:
|
|
94
|
+
continue # Disregard modifer field as a test for changed data.
|
|
95
|
+
if getattr(self, field) != value:
|
|
96
|
+
self._changed_data.append(field)
|
|
97
|
+
return self._changed_data
|
dbca_utils/utils.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def env(key, default=None, required=False, value_type=None):
|
|
6
|
+
"""
|
|
7
|
+
Retrieves environment variables and returns Python natives. The (optional)
|
|
8
|
+
default will be returned if the environment variable does not exist.
|
|
9
|
+
"""
|
|
10
|
+
try:
|
|
11
|
+
value = os.environ[key]
|
|
12
|
+
value = ast.literal_eval(value)
|
|
13
|
+
except (SyntaxError, ValueError):
|
|
14
|
+
pass
|
|
15
|
+
except KeyError:
|
|
16
|
+
if default is not None or not required:
|
|
17
|
+
return default
|
|
18
|
+
raise Exception(f"Missing required environment variable {key}")
|
|
19
|
+
|
|
20
|
+
if value_type is None:
|
|
21
|
+
if default is not None:
|
|
22
|
+
value_type = default.__class__
|
|
23
|
+
|
|
24
|
+
if value_type is None:
|
|
25
|
+
return value
|
|
26
|
+
elif isinstance(value, value_type):
|
|
27
|
+
return value
|
|
28
|
+
elif issubclass(value_type, list):
|
|
29
|
+
if isinstance(value, tuple):
|
|
30
|
+
return list(value)
|
|
31
|
+
else:
|
|
32
|
+
value = str(value).strip()
|
|
33
|
+
if not value:
|
|
34
|
+
return []
|
|
35
|
+
else:
|
|
36
|
+
return [s.strip() for s in value.split(",") if s.strip()]
|
|
37
|
+
elif issubclass(value_type, tuple):
|
|
38
|
+
if isinstance(value, list):
|
|
39
|
+
return tuple(value)
|
|
40
|
+
else:
|
|
41
|
+
value = str(value).strip()
|
|
42
|
+
if not value:
|
|
43
|
+
return tuple()
|
|
44
|
+
else:
|
|
45
|
+
return tuple([s.strip() for s in value.split(",") if s.strip()])
|
|
46
|
+
elif issubclass(value_type, bool):
|
|
47
|
+
value = str(value).strip()
|
|
48
|
+
if not value:
|
|
49
|
+
return False
|
|
50
|
+
elif value.lower() == "true":
|
|
51
|
+
return True
|
|
52
|
+
elif value.lower() == "false":
|
|
53
|
+
return False
|
|
54
|
+
else:
|
|
55
|
+
raise Exception(
|
|
56
|
+
f"{key} is a boolean environment variable and only accepts 'true' ,'false' and '' (case-insensitive), but the configured value is '{value}'"
|
|
57
|
+
)
|
|
58
|
+
elif issubclass(value_type, int):
|
|
59
|
+
return int(value)
|
|
60
|
+
elif issubclass(value_type, float):
|
|
61
|
+
return float(value)
|
|
62
|
+
else:
|
|
63
|
+
raise Exception(
|
|
64
|
+
f"{key} is a {value_type} environment variable, but {value_type} is not supported now"
|
|
65
|
+
)
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: dbca-utils
|
|
3
|
+
Version: 2.0.3
|
|
4
|
+
Summary: Utilities for DBCA Django apps
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Author: Rocky Chen
|
|
7
|
+
Author-email: rocky.chen@dbca.wa.gov.au
|
|
8
|
+
Requires-Python: >=3.9,<4.0
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Environment :: Web Environment
|
|
11
|
+
Classifier: Framework :: Django
|
|
12
|
+
Classifier: Framework :: Django :: 3.2
|
|
13
|
+
Classifier: Framework :: Django :: 4.0
|
|
14
|
+
Classifier: Framework :: Django :: 4.2
|
|
15
|
+
Classifier: Framework :: Django :: 5.0
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
18
|
+
Classifier: Programming Language :: Python
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
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-Dist: django (>=3.2,<5.1)
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# Overview
|
|
31
|
+
|
|
32
|
+
DBCA Django utility classes and functions.
|
|
33
|
+
|
|
34
|
+
## Development
|
|
35
|
+
|
|
36
|
+
This project for development is using
|
|
37
|
+
[Poetry](https://python-poetry.org/docs/) to install and manage a virtual Python
|
|
38
|
+
environment. With Poetry installed, change into the project directory and run:
|
|
39
|
+
|
|
40
|
+
poetry install
|
|
41
|
+
|
|
42
|
+
Activate the virtualenv like so:
|
|
43
|
+
|
|
44
|
+
poetry shell
|
|
45
|
+
|
|
46
|
+
Run unit tests using `pytest` (or `tox`, to test against multiple Python versions):
|
|
47
|
+
|
|
48
|
+
pytest -v
|
|
49
|
+
tox -v
|
|
50
|
+
|
|
51
|
+
## Releases
|
|
52
|
+
|
|
53
|
+
Tagged releases are built and pushed to PyPI automatically using a GitHub
|
|
54
|
+
workflow in the project. Update the project version in `pyproject.toml` and
|
|
55
|
+
tag the required commit with the same value to trigger a release.
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
1. Install via pip/Poetry/etc.: `pip install dbca-utils`
|
|
60
|
+
|
|
61
|
+
## SSO Login Middleware
|
|
62
|
+
|
|
63
|
+
This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
|
|
64
|
+
The logout view will redirect to a separate logout page which clears the SSO session.
|
|
65
|
+
|
|
66
|
+
### Usage
|
|
67
|
+
|
|
68
|
+
Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
|
|
69
|
+
`django.contrib.sessions.middleware.SessionMiddleware` and
|
|
70
|
+
`django.contrib.auth.middleware.AuthenticationMiddleware`.
|
|
71
|
+
Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
|
|
72
|
+
as this middleware depends on it for retrieving the logged in user for a session.
|
|
73
|
+
Note that the middleware will still work without it, but will reauthenticate the session
|
|
74
|
+
on every request, and `request.user.is_authenticated` won't work properly/will be false.
|
|
75
|
+
|
|
76
|
+
Example:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
MIDDLEWARE = [
|
|
80
|
+
...,
|
|
81
|
+
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
82
|
+
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
83
|
+
'dbca_utils.middleware.SSOLoginMiddleware'
|
|
84
|
+
...,
|
|
85
|
+
]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Audit model mixin
|
|
89
|
+
|
|
90
|
+
`AuditMixin` is an extension of `Django.db.model.Model` that adds a number of additional fields:
|
|
91
|
+
|
|
92
|
+
- creator - FK to `AUTH_USER_MODEL`, used to record the object creator
|
|
93
|
+
- modifier - FK to `AUTH_USER_MODEL`, used to record who the object was last modified by
|
|
94
|
+
- created - a timestamp that is set on initial object save
|
|
95
|
+
- modified - an auto-updating timestamp (on each object save)
|
|
96
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
dbca_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
dbca_utils/middleware.py,sha256=h8_Eg31CI7UJ3QY6d5HOkT4RvaaXGbkh-2Me-nLLy-g,6623
|
|
3
|
+
dbca_utils/models.py,sha256=tb-_dxYXbh7pT_q1qo1pC4AKCX7wq8nq2k7anrY6MNI,3036
|
|
4
|
+
dbca_utils/utils.py,sha256=XnwDCr2-09ffyGlc8hSk_yiVGpWnJXkRrgCl9WivGKc,2123
|
|
5
|
+
dbca_utils-2.0.3.dist-info/LICENSE,sha256=hQNdSQ5JD3TmzgScnDLFrwFybIj5bZgwHXXoz4sDX-w,10801
|
|
6
|
+
dbca_utils-2.0.3.dist-info/METADATA,sha256=h4855afmCYWrRn4lCCGUR6T-yjLD79Lvcw38ShJSxnE,3331
|
|
7
|
+
dbca_utils-2.0.3.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
8
|
+
dbca_utils-2.0.3.dist-info/RECORD,,
|