[Django] #34030: add SystemCheckError for LocaleMiddleware

17 views
Skip to first unread message

Django

unread,
Sep 22, 2022, 2:53:23 PM9/22/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim | Owner: nobody
Danilov |
Type: New | Status: new
feature |
Component: Core | Version: 4.0
(Management commands) |
Severity: Normal | Keywords: i18n, SystemCheck
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
From documentation:
To use LocaleMiddleware, add 'django.middleware.locale.LocaleMiddleware'
to your MIDDLEWARE setting.
It should come after SessionMiddleware. And it should come before
CommonMiddleware

for AuthenticationMiddleware we have already this systemCheck admin.E410:

ERRORS: (admin.E410)
'django.contrib.sessions.middleware.SessionMiddleware' must be in
MIDDLEWARE in order to use the admin application.
HINT: Insert
'django.contrib.sessions.middleware.SessionMiddleware' before
'django.contrib.auth.middleware.AuthenticationMiddleware'.

But we don't have those check for LocaleMiddleware

--
Ticket URL: <https://code.djangoproject.com/ticket/34030>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 23, 2022, 3:08:22 AM9/23/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: Jay Patel
Type: New feature | Status: assigned
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution:

Keywords: i18n, SystemCheck | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jay Patel):

* owner: nobody => Jay Patel
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:1>

Django

unread,
Sep 24, 2022, 11:13:53 AM9/24/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: Jay Patel
Type: New feature | Status: assigned
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution:
Keywords: i18n, SystemCheck | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jay Patel):

Check for LocaleMiddleware to come after SessionMiddleware can be done by
`if not hasattr(request, "session")` in `def process_request` of class
LocalMiddleware but how to check that it should come before
CommonMiddleware? or am I thinking in a complete wrong direction.

--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:2>

Django

unread,
Sep 24, 2022, 1:04:54 PM9/24/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: Jay Patel
Type: New feature | Status: assigned
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution:
Keywords: i18n, SystemCheck | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mark Walker):

You can check the position in the middleware of a given path;

{{{
#!bash
>>> MIDDLEWARE.index('django.middleware.locale.LocaleMiddleware')
12
>>>
}}}

I can implement this - it's just a matter of where to run the check. When
the settings are configured there are some checks depending on `USE_L10N`,
so perhaps this check could hang off `USE_I18N` being set?

--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:3>

Django

unread,
Sep 24, 2022, 6:44:25 PM9/24/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: Jay Patel
Type: New feature | Status: assigned
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution:
Keywords: i18n, SystemCheck | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Maxim Danilov):

Replying to [comment:2 Jay Patel]:


> Check for LocaleMiddleware to come after SessionMiddleware can be done
by `if not hasattr(request, "session")` in `def process_request` of class
LocalMiddleware but how to check that it should come before
CommonMiddleware?

Hi, Jay. On my talk on django con i use this trick:


{{{
# settings.py

if USE_I18N:
MIDDLEWARE.insert(
next((idx for idx, _mw in enumerate(MIDDLEWARE, 1) if
_mw.endswith('SessionMiddleware')), 0),
'django.middleware.locale.LocaleMiddleware'
)
}}}

i check settings USE_I18N, and only after that i start to do something
with SessionMiddleware. I add LocaleMiddleware directly after
SessionMiddleware
otherwise it should be exception, but i skip it.

probably you can use it to check idx_of_SessionMiddleware <
idx_of_LocaleMiddleware < idx_of_commonMiddlevare.

It is don't matter, where to check, to check settings you don't need the
request.

--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:4>

Django

unread,
Sep 25, 2022, 6:05:22 AM9/25/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: Jay Patel
Type: New feature | Status: assigned
Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution:
Keywords: i18n, SystemCheck | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jay Patel):

Replying to [comment:4 Maxim Danilov]:


> Replying to [comment:2 Jay Patel]:
> > Check for LocaleMiddleware to come after SessionMiddleware can be done
by `if not hasattr(request, "session")` in `def process_request` of class
LocalMiddleware but how to check that it should come before
CommonMiddleware?
>
> Hi, Jay. On my talk on django con i use this trick:
>
>
> {{{
> # settings.py
>
> if USE_I18N:
> MIDDLEWARE.insert(
> next((idx for idx, _mw in enumerate(MIDDLEWARE, 1) if
_mw.endswith('SessionMiddleware')), 0),
> 'django.middleware.locale.LocaleMiddleware'
> )
> }}}
>
> i check settings USE_I18N, and only after that i start to do something
with SessionMiddleware. I add LocaleMiddleware directly after
SessionMiddleware
> otherwise it should be exception, but i skip it.
>
> probably you can use it to check idx_of_SessionMiddleware <
idx_of_LocaleMiddleware < idx_of_commonMiddlevare.
>
> It is don't matter, where to check, to check settings you don't need the
request.


Got it! Maxim, now where should I add these checks? should it be another
translation configuration check or something else?

--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:5>

Django

unread,
Sep 28, 2022, 6:57:47 AM9/28/22
to django-...@googlegroups.com
#34030: add SystemCheckError for LocaleMiddleware
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: Jay Patel
Type: New feature | Status: closed

Component: Core (Management | Version: 4.0
commands) |
Severity: Normal | Resolution: wontfix

Keywords: i18n, SystemCheck | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: assigned => closed
* resolution: => wontfix


Comment:

TBH I don't think this is worth the complexity.

`LocaleMiddleware` is not required, and it's not in the default template,
so someone has to both go and add it, and add it in the wrong place, which
given they got through the i18n docs, I'm not sure is likely.

Note also that whilst the `hint` for `admin.E410` says to ''insert between
…'', we don't actually check that — just that `SessionMiddleware` is
installed.

I think trying to check exact/relative positions would be an unnecessary
burden.

I hope that makes sense.

--
Ticket URL: <https://code.djangoproject.com/ticket/34030#comment:6>

Reply all
Reply to author
Forward
0 new messages