|
1 |
# hobo - portal to configure and deploy applications
|
|
2 |
# Copyright (C) 2019 Entr'ouvert
|
|
3 |
#
|
|
4 |
# This program is free software: you can redistribute it and/or modify it
|
|
5 |
# under the terms of the GNU Affero General Public License as published
|
|
6 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU Affero General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU Affero General Public License
|
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
16 |
|
|
17 |
from django.conf import settings
|
|
18 |
|
|
19 |
|
|
20 |
class InternalIPMiddleware(object):
|
|
21 |
def __init__(self, get_response=None):
|
|
22 |
self.get_response = get_response
|
|
23 |
|
|
24 |
def process_request(self, request):
|
|
25 |
internal_ips = getattr(settings, 'INTERNAL_IPS', [])
|
|
26 |
try:
|
|
27 |
if request.META['REMOTE_ADDR'] in internal_ips:
|
|
28 |
self.old_value = settings.DEBUG
|
|
29 |
settings.DEBUG = True
|
|
30 |
except TypeError:
|
|
31 |
pass
|
|
32 |
return None
|
|
33 |
|
|
34 |
def process_response(self, request, response):
|
|
35 |
if hasattr(self, 'old_value'):
|
|
36 |
settings.DEBUG = self.old_value
|
|
37 |
del self.old_value
|
|
38 |
return response
|
|
39 |
|
|
40 |
def __call__(self, request):
|
|
41 |
old_value = settings.DEBUG
|
|
42 |
internal_ips = getattr(settings, 'INTERNAL_IPS', [])
|
|
43 |
set_debug = request.META['REMOTE_ADDR'] in internal_ips
|
|
44 |
try:
|
|
45 |
if set_debug:
|
|
46 |
settings.DEBUG = True
|
|
47 |
return self.get_response(request)
|
|
48 |
finally:
|
|
49 |
settings.DEBUG = old_value
|
0 |
|
-
|