Hi Torsten,
I worked on the design and implementation of new-style middleware.
On 11/22/2016 01:30 PM, Torsten Bronger wrote:
> Hallöchen!
>
> Considering the following old-style middleware class:
>
> class ExceptionsMiddleware:
> def process_exception(self, request, exception):
> ...
>
> I convert this to new-style middleware by inserting two methods:
>
> class ExceptionsMiddleware:
> def __init__(self, get_response):
> self.get_response = get_response
> def __call__(self, request):
> return self.get_response(request)
> def process_exception(self, request, exception):
> ...
>
> Is this really correct? Actually, the old way looks more concise to
> me. In particular, is there a reason why Django does not provide a
> non-deprecated base class for middleware like:
>
> class Middleware:
> def __init__(self, get_response):
> self.get_response = get_response
> def __call__(self, request):
> return self.get_response(request)
Yes, I agree with you that for middleware which don't implement request
or response processing (only process_exception, process_view, or
process_template_response), the new style is less concise, because it
requires implementing a boilerplate __init__ and __call__. To be honest,
the primary consideration in the new middleware was request and response
processing, and your case did not receive as much attention as it
perhaps should have.
I've considered a few possible approaches to fix this, and I think your
suggestion of a built-in base class that implements the
simplest-possible version of __init__ and __call__ makes sense; it is
the least magical and most explicit option.
Carl