Tuesday, November 22, 2016

Re: New-style middleware

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJYNM87AAoJEC0ft5FqUuEhAbkP+wc//NvqoIIl3hxxNKLAjDfv
uAbka5uuDwjT38VlDgyw8SAsyAvfQuD7s8p6i+J6GT4IO4VVzdQeVT6OcyWFk1To
n1NSuEZNyWqtrgXrwzYMqcNr1331VGVsqX2qVwQzNy1ZVqVYJy0DVP22oQZklSdx
byTTSptVuANUfGNa/QxUKEgextZ3goQtfEwSki9Ge2rhkGD2xtWNjpxvMZeIqEQA
2amPLtPjW2BAKrCVNZiJWa9gZcrTtoF9bCopMJvCWUT9psCGXpnYkAGoFUTuTWtx
pNSNHsmXymDYjXApsl1/wSCanPCSRuXKk1YCpmiL0p4fwedoC+FDSEPmlvO6kg8W
Kc33HlfuNVACgqqUNRUshkhSjf7etWZk224nNd16IfdZTtvxSxIlIc0uz0wiyi+s
7V3FpM83B/mHFr0maXWbFFjiQh8Dydst1f6KyX9yZGyWxb4CHNb5L4+t+2474tyQ
tpSnaSgqyu9LVrNPUAqnIb6lolD84LErbeBxpj93iyFp3AWwF4kM6KIyxD1AcjTX
cAnaCmBDUHaBbbHS/v0YSYMCibw9aBIskJPuBly+zcMlUmDgSWkqApvPILMmUIb8
kAc1S7NUMiOsZc3SjWCNdXoo1zRHdODdaEmxRCBNG6q1Qc+OA/vteon8Zo0wH4qj
AX5bjzJ0tmpaKSEcoMKG
=HpCf
-----END PGP SIGNATURE-----
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1e256b81-9e79-88ec-46d9-ee8041e5d712%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment