Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#34030 closed New feature (wontfix)

add SystemCheckError for LocaleMiddleware

Reported by: Maxim Danilov Owned by: Jay Patel
Component: Core (Management commands) Version: 4.0
Severity: Normal Keywords: i18n, SystemCheck
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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

Change History (6)

comment:1 by Jay Patel, 2 years ago

Owner: changed from nobody to Jay Patel
Status: newassigned

comment:2 by Jay Patel, 2 years ago

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?

Last edited 2 years ago by Jay Patel (previous) (diff)

comment:3 by Mark Walker, 2 years ago

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

>>> 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?

in reply to:  2 ; comment:4 by Maxim Danilov, 2 years ago

Replying to 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.

in reply to:  4 comment:5 by Jay Patel, 2 years ago

Replying to Maxim Danilov:

Replying to 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!, now where should I add these checks? should it be another translation configuration check or something else?

Last edited 2 years ago by Jay Patel (previous) (diff)

comment:6 by Carlton Gibson, 2 years ago

Resolution: wontfix
Status: assignedclosed

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 before …, 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.

Last edited 2 years ago by Carlton Gibson (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top