Repository: learningequality/kolibri

Issue:
redirect error: Reverse for 'facility_management' not found. 


## Observed behavior

After logging in I get this error:

```
kolibri start
INFO: No C extensions are available for this platform
INFO     Option DEBUG in section [Server] being overridden by environment variable KOLIBRI_DEBUG
INFO     Option DEBUG_LOG_DATABASE in section [Server] being overridden by environment variable KOLIBRI_DEBUG_LOG_DATABASE
INFO     Option HTTP_PORT in section [Deployment] being overridden by environment variable KOLIBRI_HTTP_PORT
INFO     Option RUN_MODE in section [Deployment] being overridden by environment variable KOLIBRI_RUN_MODE
INFO     Running Kolibri with the following settings: kolibri.deployment.default.settings.base
INFO     Importing 'tasks' module from django apps
INFO     Starting Kolibri 0.15.0a2.dev0+git.315.gc60a56e5
INFO     Bus state: ENTER
INFO     Listening for SIGTERM.
INFO     Listening for SIGHUP.
INFO     Listening for SIGUSR1.
INFO     Listening for SIGINT.
INFO     Bus state: IDLE
INFO     Bus state: START
INFO     Starting async task workers.
INFO     Started monitor thread 'ZeroConfPlugin'.
INFO     Started monitor thread 'ProcessControlPlugin'.
INFO     Starting on 0.0.0.0:9000
INFO     Waiting for 0.0.0.0:9000
INFO     Serving on 0.0.0.0:9000
INFO     Registering ourselves to zeroconf network with id '175dec5b5a80130d52152dd9b5c258fe' and port '9000'
INFO     Kolibri running on: http://127.0.0.1:9000/
INFO     Kolibri running on: http://10.0.0.48:9000/
INFO     Starting on 0.0.0.0:60167
INFO     Waiting for 0.0.0.0:60167
INFO     Serving on 0.0.0.0:60167
INFO     Bus state: RUN
INFO: 127.0.0.1 - - "GET /" 302 0 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /en/" 302 0 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /en/user/" 200 0 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /api/auth/session/current/" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /api/auth/facility/" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /api/auth/facilitydataset/" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /api/auth/facility/" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /api/auth/usernameexists" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "POST /api/auth/session/" 400 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /api/auth/usernameexists" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "POST /api/auth/session/" 200 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
ERROR    Internal Server Error: /en/redirectuser/
Traceback (most recent call last):
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/d/Projects/le/kolibri/kolibri/core/views.py", line 150, in get
    url = url or get_url_by_role(user_kinds.ADMIN)
  File "/Users/d/Projects/le/kolibri/kolibri/core/views.py", line 108, in get_url_by_role
    return obj.url
  File "/Users/d/Projects/le/kolibri/kolibri/plugins/facility/kolibri_plugin.py", line 37, in url
    return self.plugin_url(FacilityManagementPlugin, "facility_management")
  File "/Users/d/Projects/le/kolibri/kolibri/core/hooks.py", line 47, in plugin_url
    return plugin_url(plugin_class, url_name)
  File "/Users/d/Projects/le/kolibri/kolibri/plugins/utils/__init__.py", line 54, in plugin_url
    return reverse(
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/urls/base.py", line 91, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/Users/d/Projects/le/kolibri/.venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'facility_management' not found. 'facility_management' is not a valid view function or pattern name.
INFO: 127.0.0.1 - - "GET /en/redirectuser/" 500 0 "http://localhost:9000/en/user/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
INFO: 127.0.0.1 - - "GET /favicon.ico" 404 0 "http://localhost:9000/en/redirectuser/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0"
```


## Steps to reproduce

Log in as a super user

## Context

latest state of `develop`


Discussion:
Confusingly, it appears I set this up as a learn-only device, which I was under the impression wouldn't have a super user.

![image](https://user-images.githubusercontent.com/2367265/132040370-1fc2cc7d-c00e-460f-a3af-708b31de8449.png)

the user I logged in with was (I think) the super user from the _other_ device I originally imported from
If you set it up with a super user, then the super user is imported as well, and granted super user privileges (this was not originally planned, but it ended up being significantly harder not to do this).
Looks like this specific error is an unintended consequence of our blocking of facility URLs.
Correct fix will be to make the redirect hook conditional on SoUD too.
> If you set it up with a super user, then the super user is imported as well, and granted super user privileges (this was not originally planned, but it ended up being significantly harder not to do this).

I wonder if the app messaging could be tweaked to reflect this?

![image](https://user-images.githubusercontent.com/2367265/132074862-648d2f29-686f-4736-99d8-74f8d5dad8fd.png)




Resolving patch:
diff --git a/kolibri/core/views.py b/kolibri/core/views.py
--- a/kolibri/core/views.py
+++ b/kolibri/core/views.py
@@ -100,7 +100,11 @@ def get_urls_by_role(role):
 
 def get_url_by_role(role):
     obj = next(
-        (hook for hook in RoleBasedRedirectHook.registered_hooks if role in hook.roles),
+        (
+            hook
+            for hook in RoleBasedRedirectHook.registered_hooks
+            if role in hook.roles and hook.url
+        ),
         None,
     )
 
diff --git a/kolibri/plugins/coach/kolibri_plugin.py b/kolibri/plugins/coach/kolibri_plugin.py
--- a/kolibri/plugins/coach/kolibri_plugin.py
+++ b/kolibri/plugins/coach/kolibri_plugin.py
@@ -29,7 +29,10 @@ class CoachRedirect(RoleBasedRedirectHook):
 
     @property
     def url(self):
-        return self.plugin_url(Coach, "coach")
+        # Also disable attempting to redirect to the coach page
+        # if we are on a subset of users device.
+        if not get_device_setting("subset_of_users_device", False):
+            return self.plugin_url(Coach, "coach")
 
 
 @register_hook
diff --git a/kolibri/plugins/facility/kolibri_plugin.py b/kolibri/plugins/facility/kolibri_plugin.py
--- a/kolibri/plugins/facility/kolibri_plugin.py
+++ b/kolibri/plugins/facility/kolibri_plugin.py
@@ -34,7 +34,10 @@ class FacilityRedirect(RoleBasedRedirectHook):
 
     @property
     def url(self):
-        return self.plugin_url(FacilityManagementPlugin, "facility_management")
+        # Also disable attempting to redirect to the facility management page
+        # if we are on a subset of users device.
+        if not get_device_setting("subset_of_users_device", False):
+            return self.plugin_url(FacilityManagementPlugin, "facility_management")
 
 
 @register_hook


Test patch: