Repository: learningequality/kolibri

Issue:
Force selection of facility in user login
### Observed behavior

Currently a device with multiple facilities will not let a user login to the non-default facility. This may be at odds with the desires of some deployments who have roving models.

### Expected behavior

Show a list of facilities to login with.

Any simplified login autocomplete should be based on the currently selected facility.

### User-facing consequences

Users can't login!


Discussion:
Will need to add a k-select. Might be a good time to ditch the dark background... @jtamiace @khangmach 
Would require the addition of strings.
Punting due to string freeze.
![facility changing](https://user-images.githubusercontent.com/25045166/35019492-0d6a2dfe-fadc-11e7-8f38-8b8f6769ffc0.png)
Here are some preliminary mockups to address this feature. One of my concerns is the possibility of a user becoming irked at the constant need to select a facility via the modal (screen 1) every time they encounter the log in page. Is there a way via cookies or sessions to preserve their facility selection the next time they log in? 

**Some of my notes:**
- The modal and facility selection k-select are only visible if the device has more than one facility
- The modal is close-able by pressing cancel
- The first facility radio option in the k-select is selected by default
- Screen refreshes once another facility is selected
- Resizing to smaller devices pushes the facility select k-select component to the left
- On mobile the facility selector sits below the language selection component


> One of my concerns is the possibility of a user becoming irked at the constant need to select a facility via the modal (screen 1) every time they encounter the log in page.

Agreed. Even if it's just once, going to a page and having a modal immediately pop up might be a bit jarring?

> Is there a way via cookies or sessions to preserve their facility selection the next time they log in?

I believe so, yes

> ![image](https://user-images.githubusercontent.com/2367265/35028202-9ed58d2e-fb09-11e7-9377-31e4e6a57dbe.png)

Having the facility selection down with the language selection seems risky because it's the last thing people will see, but the first thing they need to confirm before logging in or creating an account. Would be interested to see a design that puts facility selection somewhere above the login forms?

I'd also try the design on realistic mobile screen dimensions in portrait and landscape - I'm guessing you'll find the mobile view doesn't actually work as expected, particularly once the keyboard is open.

Why not just include the facility selector above the username field?

### First time landing on sign in page
No default facility set in local storage.
Autofocus on facility selector.
Select a facility
Set as default in local storage.

### All other times landing on sign in page
Default facility is already selected
Autofocus on the username.
If change in facility, will be saved in local storage.
@christianmemije I think with this simplified approach, there is no need to include a facility selector at all in the login UI.

To paraphrase Jamie, 99% of the time we won't need to inquire what facility the user is logging into. Problems only come up when there are collisions in both username and passwords. For example, when a username + password set belong to 2+ facilities, or when a learner's username belongs in 2+ facilities. 

If such ambiguities can be detected after login info input, after pressing sign in, a modal can pop up prompting the user to clarify what facility they actually want to go to. This way we reduce UI clutter.

![facility changing](https://user-images.githubusercontent.com/25045166/35070604-a1aad520-fb92-11e7-8423-feeeacfdfd25.png)


Resolving patch:
diff --git a/kolibri/auth/api.py b/kolibri/auth/api.py
--- a/kolibri/auth/api.py
+++ b/kolibri/auth/api.py
@@ -206,6 +206,14 @@ class FacilityViewSet(viewsets.ModelViewSet):
     queryset = Facility.objects.all()
     serializer_class = FacilitySerializer
 
+    def get_queryset(self, prefetch=True):
+        queryset = Facility.objects.all()
+        if prefetch:
+            # This is a default field on the serializer, so do a select_related
+            # to prevent n queries when n facilities are queried
+            return queryset.select_related('dataset')
+        return queryset
+
 
 class CurrentFacilityViewSet(viewsets.ViewSet):
     def list(self, request):
diff --git a/kolibri/plugins/user/views.py b/kolibri/plugins/user/views.py
--- a/kolibri/plugins/user/views.py
+++ b/kolibri/plugins/user/views.py
@@ -1,18 +1,8 @@
-from __future__ import absolute_import, print_function, unicode_literals
-from django.views.generic.base import TemplateView
-from kolibri.auth.models import Facility
+from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
 
+from django.views.generic.base import TemplateView
 
 class UserView(TemplateView):
     template_name = "user/user.html"
-
-    def get_context_data(self, **kwargs):
-        context = super(UserView, self).get_context_data(**kwargs)
-
-        # Put the default facility id into the page so that we can bootstrap the
-        # dataset for this facility into the page and allow for quicker rendering
-        # of the login page, without having to wait for an AJAX call.
-
-        context['currentFacilityId'] = getattr(Facility.get_default_facility(), 'id', None)
-
-        return context


Test patch: