fabiocaccamo / django-maintenance-mode

:construction: :hammer_and_wrench: shows a 503 error page when maintenance-mode is on.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow override_settings in tests

ziima opened this issue · comments

Use django-appsettings or similar approach to enable override_settings to be used in tests.

Why don't use the django decorator?

I'd like to, but without changing the settings file it won't work. The settings module is loaded sometime during the tests and all maintenance mode related settings got interpreted at that point and fixed. Any further call of override_settings will not affect the settings module and thus will have no effect on the code.

Yes, can we please get support for this? The way settings are being loaded for this submodule means they are not dynamic—they are read on init and then set permanently (in maintenance_mode/settings.py), regardless of what the parent Django app ends up doing with them.

Overriding in a test case doesn't work for that reason, no matter how you do it, because this project references it's own settings instead of dynamically referencing Django settings. The likely culprit here is the references to maintenance_mode import settings instead of from django.conf import settings.

I've forked the repo and will see if I can find some time to resolve this issue. For now, here's how I've worked around the issue, if others run into the same problem and are lost:

https://github.com/HeliumEdu/platform/blob/b27a20a51e5ee84f5215cebfabad27b85a5ca8e1/helium/common/tests/testcaseerrorviews.py#L71

Using something like https://github.com/Genida/django-appsettings should fix problem.

@ziima While that would work, it seems a bit overkill—I'm assuming @fabiocaccamo doesn't want to incur additional dependencies for this submodule just to make this work, and I don't think we should need to.

PR tested and submitted. CI building looking good.

Thoughts, @fabiocaccamo? Could we get this in a minor version released to pip ASAP?

@alexdlaird settings.configure is not meant for application settings, but as an alternative to DJANGO_SETTINGS_MODULE.

The common approach is to create some sort of custom settings class, e.g. https://github.com/encode/django-rest-framework/blob/master/rest_framework/settings.py#L191-L247, but it's kind of waste to create one in every Django third party application. That's why I suggested to use django-appsettings application.

@alexdlaird thanks for the PR, I merged it and released a 0.6.7 version and a 0.7.0 version (which adds django 2.0 compatibility).