Files
mizan/packages/mizan-django
Ryth Azhur b06a65e133 Fix critical cache issues: user_id scoping, AND purge, error handling, TTL
Fixes from 8-expert review:
- Pass user_id from request.user.pk to cache key derivation (data leak fix)
- Scoped purge uses AND (intersection) not OR (union) semantics
- All cache ops in executor wrapped in try/except with logging fallthrough
- Thread-safe cache initialization with threading.Lock
- RedisCache: 24h safety-net TTL, connection timeouts, MULTI/EXEC pipeline
- RedisCache.clear() uses pipelined UNLINK instead of per-batch DELETE
- build_index_keys now stringifies values matching derive_cache_key
- get_cache() logs warnings for partial config and connection failures
- Wire-protocol internals removed from __all__

Remaining open: purge atomicity (Lua script), cross-language str() canon,
broad purge sub-index cleanup, thundering herd protection, RedisCache tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 22:50:05 -04:00
..

mizan (Python)

Django server functions framework. See the monorepo root for full documentation.

Install

uv add "mizan[channels,allauth] @ git+https://git.impactsoundworks.com/isw/mizan.git#subdirectory=django"

Setup

# settings.py
INSTALLED_APPS = ["mizan", ...]

# urls.py
path("api/mizan/", include("mizan.urls"))

# asgi.py (optional, for WebSocket)
from mizan import wrap_asgi
application = wrap_asgi(get_asgi_application())

Define Functions

from mizan.client import client
from mizan.setup.registry import register
from pydantic import BaseModel

class Output(BaseModel):
    message: str

@client
def echo(request, text: str) -> Output:
    return Output(message=text)

register(echo, "echo")

Register in apps.py:

def ready(self):
    import myapp.mizan_clients

Auth

@client(auth=True)          # requires authentication
@client(auth='staff')       # requires is_staff
@client(auth='superuser')   # requires is_superuser
@client(auth=my_callable)   # custom check

Contexts

@client(context='global')   # fetched once, SSR-hydrated, becomes useCurrentUser()
@client(context='local')    # fetched with params, becomes <GreetProvider>

Forms

from mizan.forms import mizanFormMixin, mizanFormMeta

class ContactForm(mizanFormMixin, forms.Form):
    mizan = mizanFormMeta(name="contact", title="Contact Us")
    name = forms.CharField()
    email = forms.EmailField()

    def on_submit_success(self, request):
        return {"sent": True}

Auto-registers contact.schema, contact.validate, contact.submit. Generates useContactForm() with Zod validation.

Channels

from mizan.channels import ReactChannel

class ChatChannel(ReactChannel):
    class Params(BaseModel):
        room: str
    class DjangoMessage(BaseModel):
        text: str

    def authorize(self, params):
        return self.user.is_authenticated
    def group(self, params):
        return f"chat_{params.room}"

Generates useChatChannel({ room }).

Running Tests

uv sync --extra dev --extra channels
uv run pytest