37
41
HttpResponseRedirect,
39
43
from django.utils.http import urlquote_plus
40
from maasserver.exceptions import MAASAPIException
44
from maasserver.exceptions import (
45
ExternalComponentException,
43
50
def get_relative_path(path):
104
PROFILES_CHECK_DONE_KEY = 'profile-check-done'
106
# The profiles check done by check_profiles_cached is only done at most once
107
# every PROFILE_CHECK_DELAY seconds for efficiency.
108
PROFILE_CHECK_DELAY = 2 * 60
111
def check_profiles_cached():
112
"""Check Cobbler's profiles. The check is actually done at most once every
113
PROFILE_CHECK_DELAY seconds for performance reasons.
115
# Avoid circular imports.
116
from maasserver.provisioning import check_profiles
117
if not cache.get(PROFILES_CHECK_DONE_KEY, False):
118
# Mark the profile check as done beforehand as the actual check
119
# might raise an exception.
120
cache.set(PROFILES_CHECK_DONE_KEY, True, PROFILE_CHECK_DELAY)
124
def clear_profiles_check_cache():
125
"""Force a profile check next time the MAAS server is accessed."""
126
cache.delete(PROFILES_CHECK_DONE_KEY)
129
class ExternalComponentsMiddleware:
130
"""This middleware performs checks for external components (right
131
now only Cobbler is checked) at regular intervals.
133
def process_request(self, request):
134
# This middleware hijacks the request to perform checks. Any
135
# error raised during these checks should be caught to avoid
136
# disturbing the handling of the request. Proper error reporting
137
# should be handled in the check method itself.
139
check_profiles_cached()
97
145
class ExceptionMiddleware:
98
146
"""Convert exceptions into appropriate HttpResponse responses.
163
211
path_regex = settings.API_URL_REGEXP
214
class ErrorsMiddleware:
215
"""Handle ExternalComponentException exceptions in POST requests: add a
216
message with the error string and redirect to the same page (using GET).
219
def process_exception(self, request, exception):
220
should_process_exception = (
221
request.method == 'POST' and
222
isinstance(exception, ExternalComponentException))
223
if should_process_exception:
224
messages.error(request, unicode(exception))
225
return HttpResponseRedirect(request.path)
227
# Not an ExternalComponentException or not a POST request: do not
166
232
class ExceptionLoggerMiddleware:
168
234
def process_exception(self, request, exception):