Skip to main content

Production-ready utilities for Django applications in the Oxiliere ecosystem

Project description

OxUtils

Production-ready utilities for Django applications in the Oxiliere ecosystem.

PyPI version Python 3.12+ Django 5.0+ Tests License Code style: ruff

Features

  • 🔐 JWT Authentication - RS256 with JWKS caching
  • 📝 Structured Logging - JSON logs with automatic request tracking
  • 🔍 Audit System - Change tracking with S3 export
  • ⚙️ Celery Integration - Pre-configured task processing
  • 🛠️ Django Mixins - UUID, timestamps, user tracking
  • Custom Exceptions - Standardized API errors
  • 🎨 Context Processors - Site name and domain for templates
  • 💱 Currency Module - Multi-source exchange rates (BCC/OXR)
  • 📄 PDF Generation - WeasyPrint integration for Django
  • 🏢 Multi-Tenant - PostgreSQL schema-based isolation

Installation

pip install oxutils
uv add oxutils

Quick Start

1. Configure Django Settings

# settings.py
from oxutils.conf import UTILS_APPS, AUDIT_MIDDLEWARE

INSTALLED_APPS = [
    *UTILS_APPS,  # structlog, auditlog, celery_results
    # your apps...
]

MIDDLEWARE = [
    *AUDIT_MIDDLEWARE,  # RequestMiddleware, Auditlog
    # your middleware...
]

2. Environment Variables

OXI_SERVICE_NAME=my-service
OXI_JWT_JWKS_URL=https://auth.example.com/.well-known/jwks.json

3. Usage Examples

# JWT Authentication
from oxutils.jwt.client import verify_token
payload = verify_token(token)

# Structured Logging
import structlog
logger = structlog.get_logger(__name__)
logger.info("user_action", user_id=user_id)


# Model Mixins
from oxutils.models.base import BaseModelMixin
class Product(BaseModelMixin):  # UUID + timestamps + is_active
    name = models.CharField(max_length=255)

# Custom Exceptions
from oxutils.exceptions import NotFoundException
raise NotFoundException(detail="User not found")

# Context Processors
# settings.py
TEMPLATES = [{
    'OPTIONS': {
        'context_processors': [
            'oxutils.context.site_name_processor.site_name',
        ],
    },
}]
# Now {{ site_name }} and {{ site_domain }} are available in templates

Documentation

Core Modules

Additional Modules

  • Currency - Exchange rates management
  • PDF - PDF generation with WeasyPrint
  • Oxiliere - Multi-tenant architecture

Requirements

  • Python 3.12+
  • Django 5.0+
  • PostgreSQL (recommended)

Development

git clone https://github.com/oxiliere/oxutils.git
cd oxutils
uv sync
uv run pytest  # 201 tests passing, 4 skipped

Creating Migrations

To generate Django migrations for the audit module:

make migrations
# or
uv run make_migrations.py

See MIGRATIONS.md for detailed documentation.

Optional Dependencies

# Multi-tenant support
uv add oxutils[oxiliere]

# PDF generation
uv add oxutils[pdf]

# Development tools
uv add oxutils[dev]

Advanced Examples

JWT with Django Ninja

from ninja import NinjaAPI
from ninja.security import HttpBearer
from oxutils.jwt.client import verify_token

class JWTAuth(HttpBearer):
    def authenticate(self, request, token):
        try:
            return verify_token(token)
        except:
            return None

api = NinjaAPI(auth=JWTAuth())

@api.get("/protected")
def protected(request):
    return {"user_id": request.auth['sub']}

Audit Log Export

from oxutils.audit.export import export_logs_from_date
from datetime import datetime, timedelta

from_date = datetime.now() - timedelta(days=7)
export = export_logs_from_date(from_date=from_date)
print(f"Exported to {export.data.url}")

Currency Exchange Rates

from oxutils.currency.models import CurrencyState

# Sync rates from BCC (with OXR fallback)
state = CurrencyState.sync()

# Get latest rates
latest = CurrencyState.objects.latest()
usd_rate = latest.currencies.get(code='USD').rate
eur_rate = latest.currencies.get(code='EUR').rate

PDF Generation

from oxutils.pdf.printer import Printer
from oxutils.pdf.views import WeasyTemplateView

# Standalone PDF generation
printer = Printer(
    template_name='invoice.html',
    context={'invoice': invoice},
    stylesheets=['css/invoice.css']
)
pdf_bytes = printer.write_pdf()

# Class-based view
class InvoicePDFView(WeasyTemplateView):
    template_name = 'invoice.html'
    pdf_filename = 'invoice.pdf'
    pdf_stylesheets = ['css/invoice.css']

Multi-Tenant Setup

# settings.py
TENANT_MODEL = "oxiliere.Tenant"
MIDDLEWARE = [
    'oxutils.oxiliere.middleware.TenantMainMiddleware',  # First!
    # other middleware...
]

# All requests must include X-Organization-ID header
# Data is automatically isolated per tenant schema

License

Apache 2.0 License - see LICENSE

Support


Made with ❤️ by Oxiliere

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oxutils-0.1.22.tar.gz (78.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oxutils-0.1.22-py3-none-any.whl (121.4 kB view details)

Uploaded Python 3

File details

Details for the file oxutils-0.1.22.tar.gz.

File metadata

  • Download URL: oxutils-0.1.22.tar.gz
  • Upload date:
  • Size: 78.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for oxutils-0.1.22.tar.gz
Algorithm Hash digest
SHA256 b57fc19a70ade579bbf4692e60da01ff508e7bd76aba83c27bb3501aa01a81e5
MD5 a981f962e8b6321cf761c42b087a3d39
BLAKE2b-256 66ee180ba501d9cf81188bc24f394fea34336e3e6c06d824b89ac793b933f9f5

See more details on using hashes here.

File details

Details for the file oxutils-0.1.22-py3-none-any.whl.

File metadata

  • Download URL: oxutils-0.1.22-py3-none-any.whl
  • Upload date:
  • Size: 121.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for oxutils-0.1.22-py3-none-any.whl
Algorithm Hash digest
SHA256 45cb5e5f5b41c537bd87004ad9fd5e5c11b14eb3e909729eae1348a854443c91
MD5 0b5e2cd829fc0d253cab9f2544e358f0
BLAKE2b-256 4a73b684d1df82216bacb99b3c2a4a6eb4f685874ac59c62e7800dee435f4d7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page