Skip to content

Commit

Permalink
Merge pull request #7024 from rtibbles/provisioning_error_handling
Browse files Browse the repository at this point in the history
Handle backend provisioning errors.
  • Loading branch information
indirectlylit authored Jun 17, 2020
2 parents 5920853 + 46bc63a commit 95a9a0c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions kolibri/core/device/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def url(self):

def plugin_url(self, plugin_class, url_name):
return plugin_url(plugin_class, url_name)

@classmethod
def provision_url(cls):
return next(hook.url for hook in cls.registered_hooks)
16 changes: 16 additions & 0 deletions kolibri/core/device/middleware.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.urls import is_valid_path
from django.utils import translation

from .translation import get_language_from_request_and_is_from_path
from kolibri.core.device.hooks import SetupHook
from kolibri.core.device.utils import DeviceNotProvisioned
from kolibri.utils.conf import OPTIONS


Expand Down Expand Up @@ -79,3 +82,16 @@ def __call__(self, request):
response["Content-Language"] = language

return response


class ProvisioningErrorHandler(object):
def __init__(self, get_response):
self.get_response = get_response

def process_exception(self, request, exception):
if isinstance(exception, DeviceNotProvisioned) and SetupHook.provision_url():
return redirect(SetupHook.provision_url())
return None

def __call__(self, request):
return self.get_response(request)
6 changes: 2 additions & 4 deletions kolibri/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,8 @@ def get(self, request):
Redirects user based on the highest role they have for which a redirect is defined.
"""
# If it has not been provisioned and we have something that can handle setup, redirect there.
if not is_provisioned():
SETUP_WIZARD_URLS = [hook.url for hook in SetupHook.registered_hooks]
if SETUP_WIZARD_URLS:
return redirect(SETUP_WIZARD_URLS[0])
if not is_provisioned() and SetupHook.provision_url:
return redirect(SetupHook.provision_url())

if request.user.is_authenticated():
url = None
Expand Down
1 change: 1 addition & 0 deletions kolibri/deployment/default/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@

MIDDLEWARE = [
"kolibri.core.analytics.middleware.cherrypy_access_log_middleware",
"kolibri.core.device.middleware.ProvisioningErrorHandler",
"django.middleware.cache.UpdateCacheMiddleware",
"kolibri.core.analytics.middleware.MetricsMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand Down

0 comments on commit 95a9a0c

Please sign in to comment.