simonw / djp

A plugin system for Django
https://djp.readthedocs.io
Apache License 2.0
90 stars 2 forks source link

Ability to specify exactly which middleware to run before or after #10

Closed simonw closed 2 months ago

simonw commented 2 months ago

@carltongibson suggested that middleware be able to say "I should be run directly before/after this other named middleware".

simonw commented 2 months ago

Example in this test: https://github.com/simonw/djp/blob/a8d8add2cfcd653a44a68d798ea541dede489d38/tests/plugins/middleware.py#L4-L26

Documentation here: https://djp.readthedocs.io/en/latest/plugin_hooks.html#middleware

simonw commented 2 months ago

This took longer than expected because I got stuck on a gnarly bug, which I eventually resolved using OpenAI's o1-preview:

files-to-prompt **/*.py -c | llm -m o1-preview "

The middleware test is failing showing all ogf these - why is MiddlewareAfter repeated so many times?

['MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware5', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware2', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware5', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware4', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware5', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware2', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware5', 'MiddlewareAfter', 'Middleware3', 'MiddlewareAfter', 'Middleware', 'MiddlewareBefore']"

https://gist.github.com/simonw/03776d9f80534aa8e5348580dc6a800b

rptmat57 commented 2 months ago

Just a thought, but I am wondering if the item cannot be found if it should throw an error or automatically put the middleware at the beginning/end of the list. I have a use case where a plugin should be before another plugin if it's installed. if that other plugin is not installed it can be somewhere else. Users don't always have that other plugin installed.

Maybe adding a default position like nulls_last, or pass it a flag to not raise an error if it's not found?

I haven't tried it, but maybe I can also implement my conditional ordering logic in the function itself?

@djp.hookimpl
def middleware():