44 lines
1.7 KiB
Python
44 lines
1.7 KiB
Python
# hobo - portal to configure and deploy applications
|
|
# Copyright (C) 2015-2022 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from django.conf import settings
|
|
from django.http import HttpResponse
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
def pass_through(request):
|
|
remote_addr = request.META.get('REMOTE_ADDR')
|
|
if remote_addr:
|
|
pass_through_ips = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', [])
|
|
if remote_addr in pass_through_ips:
|
|
return True
|
|
pass_through_header = getattr(settings, 'MAINTENANCE_PASS_THROUGH_HEADER', '')
|
|
if pass_through_header and pass_through_header in request.headers:
|
|
return True
|
|
return False
|
|
|
|
|
|
class MaintenanceMiddleware:
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
|
|
def __call__(self, request):
|
|
maintenance_mode = getattr(settings, 'MAINTENANCE_MODE', None)
|
|
if maintenance_mode and not pass_through(request):
|
|
maintenance_msg = _('The site is under maintenance')
|
|
return HttpResponse('<h1>%s</h1>' % maintenance_msg, status=503)
|
|
return self.get_response(request)
|