Metadata-Version: 2.4
Name: dbca-utils
Version: 3.0.7
Summary: Utilities for DBCA Django apps
Author-Email: Rocky Chen <rocky.chen@dbca.wa.gov.au>, Ashley Felton <ashley.felton@dbca.wa.gov.au>
License-Expression: Apache-2.0
Classifier: Framework :: Django
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Project-URL: Homepage, https://github.com/dbca-wa/dbca-utils
Project-URL: Repository, https://github.com/dbca-wa/dbca-utils.git
Project-URL: Changelog, https://github.com/dbca-wa/dbca-utils/blob/main/CHANGELOG.md
Project-URL: GitHub, https://github.com/dbca-wa/dbca-utils
Requires-Python: <4.0,>=3.12
Requires-Dist: django<6.2,>=5.2
Requires-Dist: markupsafe>=3.0.3
Description-Content-Type: text/markdown

# Overview

DBCA Django utility classes and functions.

## Requirements

- Python 3.12 or later
- Django 5.2 or later

## Development

Dependencies for this project are managed using [uv](https://docs.astral.sh/uv/).
With uv installed, change into the project directory and run:

    uv sync

Activate the virtualenv like so:

    source .venv/bin/activate

Run unit tests using `pytest` (or `tox`, to test against multiple Python versions):

    pytest -sv
    tox -v

## Releases

Tagged releases are built and pushed to PyPI automatically using a GitHub
workflow in the project. Update the project version in `pyproject.toml` and
tag the required commit with the same value to trigger a release. Packages
can also be built and uploaded manually to PyPI using [uv](https://docs.astral.sh/uv/guides/publish/#publishing-your-package),
if required:

    uv build
    uv publish

## Installation

1. Install via uv/pip/etc.: `pip install dbca-utils`

## SSO Login Middleware

This will automatically login and create users using headers from an upstream proxy (REMOTE_USER and some others).
The logout view will redirect to a separate logout page which clears the SSO session.

### Usage

Add `dbca_utils.middleware.SSOLoginMiddleware` to `settings.MIDDLEWARE` (after both of
`django.contrib.sessions.middleware.SessionMiddleware` and
`django.contrib.auth.middleware.AuthenticationMiddleware`.
Ensure that `AUTHENTICATION_BACKENDS` contains `django.contrib.auth.backends.ModelBackend`,
as this middleware depends on it for retrieving the logged in user for a session.
Note that the middleware will still work without it, but will reauthenticate the session
on every request, and `request.user.is_authenticated` won't work properly/will be false.

Example:

```python
MIDDLEWARE = [
    ...,
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'dbca_utils.middleware.SSOLoginMiddleware'
    ...,
]
```

## Audit model mixin

`AuditMixin` is an extension of `Django.db.model.Model` that adds a number of additional fields:

- `creator` - FK to `AUTH_USER_MODEL`, used to record the object creator
- `modifier` - FK to `AUTH_USER_MODEL`, used to record who the object was last modified by
- `created` - a timestamp that is set on initial object save
- `modified` - an auto-updating timestamp (on each object save)
  
## Healthcheck feature
### Requirements
  - Django 5.2 or later
  - Declared the default cache and also the cache is shared by all pod instances
  - The image has the command 'ps' which is used to collect the cpu and memory data
    
  ### Usage
  - Install the app 'dbca_utils' in INSTALLED_APPS
  - Service Configuration
      - HEALTHCHECK_ENABLED: Optional. enable/disable the healthcheck service. default is 'true'
      - PROCESS_FILTER: Optional. find the web app related processes from command 'ps aux'. default is '| grep python'
      - CACHE_PREFIX: Optional. used as the prefix of the cache key. default is ''
      - PORT: Optional. The listening port of the web application. default is '8080'
      - WORKLOADS: Optional. Used if the web app has a fixed replicas.
      - WORKLOAD_DEPLOYMENT: Optional. the workload is deployment if it is true; otherwise it is statefulset. default is 'true'
      - WORKLOAD_FAILED_THRESHOLD: Optional. The number of continuous failed times to treat a pod is offline.
  - Nginx Configuration.
      - Add a location 'location /healthcheck/' and configure it to use basic auth in nginx.
  - Access the url : https://xxx.dbca.wa.gov.au/healthcheck/healthdata to get the health json data
