Maintenance banner functionality, including a new waffle switch and setting
This adds the following new waffle switch (and namespace): open_edx_util.display_maintenance_warning and a new setting: settings.MAINTENANCE_BANNER_TEXT The provided view decorator allows developers to select which views receive the static banner, and the banner can be globally toggled via the waffle switch. The text of the banner is determined by the setting, which is currently not translated.
This commit is contained in:
43
openedx/core/djangoapps/util/maintenance_banner.py
Normal file
43
openedx/core/djangoapps/util/maintenance_banner.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
View decorator to add a maintenance banner configured in settings.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from functools import wraps
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from openedx.core.djangoapps.util.user_messages import PageLevelMessages
|
||||
from openedx.core.djangoapps.util.waffle import DISPLAY_MAINTENANCE_WARNING, waffle
|
||||
|
||||
|
||||
def add_maintenance_banner(func):
|
||||
"""
|
||||
View decorator to select where exactly the banner will appear
|
||||
|
||||
Add to function-based views like this:
|
||||
|
||||
from openedx.core.djangoapps.util.maintenance_banner import add_maintenance_banner
|
||||
|
||||
@add_maintenance_banner
|
||||
def my_view(request):
|
||||
...
|
||||
|
||||
Add to class-based views using method_decorator:
|
||||
|
||||
from openedx.core.djangoapps.util.maintenance_banner import add_maintenance_banner
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
@method_decorator(add_maintenance_banner, name='dispatch')
|
||||
class MyView(View):
|
||||
...
|
||||
"""
|
||||
@wraps(func)
|
||||
def _decorated(request, *args, **kwargs): # pylint: disable=missing-docstring
|
||||
if waffle().is_enabled(DISPLAY_MAINTENANCE_WARNING):
|
||||
if hasattr(settings, 'EDXAPP_MAINTENANCE_BANNER_TEXT') and settings.EDXAPP_MAINTENANCE_BANNER_TEXT:
|
||||
# The waffle switch is enabled and the banner text is defined
|
||||
# and non-empty. We can now register the message:
|
||||
PageLevelMessages.register_warning_message(request, settings.EDXAPP_MAINTENANCE_BANNER_TEXT)
|
||||
return func(request, *args, **kwargs)
|
||||
return _decorated
|
||||
18
openedx/core/djangoapps/util/waffle.py
Normal file
18
openedx/core/djangoapps/util/waffle.py
Normal file
@@ -0,0 +1,18 @@
|
||||
"""
|
||||
Waffle flags and switches
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace
|
||||
|
||||
WAFFLE_NAMESPACE = u'open_edx_util'
|
||||
|
||||
# Switches
|
||||
DISPLAY_MAINTENANCE_WARNING = u'display_maintenance_warning'
|
||||
|
||||
|
||||
def waffle():
|
||||
"""
|
||||
Returns the namespaced, cached, audited Waffle class for open_edx_util.
|
||||
"""
|
||||
return WaffleSwitchNamespace(name=WAFFLE_NAMESPACE, log_prefix=u'OpenEdX Util: ')
|
||||
Reference in New Issue
Block a user