简体   繁体   中英

Changing a setting variable from settings.py

I am using Django 1.3 with mod_wsgi

In my settings.py

DISABLE_SYSTEM = False
DISABLE_USER_INTERFACE = False
MIDDLEWARE_CLASSES = [
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]
if DISABLE_SYSTEM:
    MIDDLEWARE_CLASSES.insert(0, 'SomeMiddleware')
if DISABLE_USER_INTERFACE:
    MIDDLEWARE_CLASSES.append('SomeOtherMiddleware')

When i set DISABLE_SYSTEM to True , SomeMiddleware 's process_request function returns a warning message with HttpResponse and following Middewares do not run at all. That gives me a kind of System shutdown for maintenance

DISABLE_USER_INTERFACE filters request and any view function called by a user is blocked, while admin urls and management functions runs normally.

Up to now, i used this for maintenance of different kinds, and i simply change it from the file and touch wsgi to re-reload python modules. But now, i need to use a kind of scheduled routine to stop user based requests and do some maintanance on the background and some from admin.

At this point, i system (but not user) needs to set DISABLE_USER_INTERFACE to True from settings.py and start maintenance. But i could not find porper way to do it or a better way to handle this not from settings.py but somewhere else.

Lines in settings py than changes DISABLE_USER_INTERFACE is because i need to use this function once per day and using these two middleware for each request do not seem logical to me. So in my current model, they run only when they are needed to be run

Any suggestions will be appreciated.

UPDATE: What i want to do is disabling user interface during 19:00 - 19:30 everyday. I am not sure about making a middleware level check like:

if 19:00<now()<19:30:
    stop system

for every request. I need something more efficient that avoids unnecessary process. Or is middleware the right choice for that kind of works?

When you have scheduled changes to your settings, you might want to reconsider what you're doing.

Your Middleware can be much, much smarter. This makes it possible for your settings to be much, much dumber.

I would strongly suggest that you have one SomeMiddleware class which is always installed.

That SomeMiddleware class can then check the settings and decide which behavior it should perform.

There are still better ways to do this.

You don't necessarily need to revise the settings for the scheduled maintenance. You have lots and lots of ways of communicating with your middleware in a running Django application. One of the fastest is through the database.

You have can an "operating mode" class definition with one (or a few) attributes which are simply fetched by the middleware to see what's going on. You can write your admin apps to do a simple Update on this table to change the mode.

You can have an "operating mode" file names which (if present) change the middleware's behavior. You need only do os.path.exists() kind functions to check (quickly) what to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM