Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions kalite/facility/api_resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import os
from dateutil.tz import tzlocal

from django.db.models.signals import post_save
from tastypie import fields
from tastypie.http import HttpUnauthorized
from tastypie.resources import ModelResource, ALL_WITH_RELATIONS
Expand Down Expand Up @@ -40,6 +39,27 @@ class Meta:
resource_name = 'group'
authorization = TeacherOrAdminCanReadWrite()

FACILITY_LIST = None

def facility_list():
global FACILITY_LIST

if FACILITY_LIST is None:
if settings.CENTRAL_SERVER:
FACILITY_LIST = []
else:
# To enable login, list the id and names of all facilities.
# This keeps it cached so that the status api call can return this to the client side
# without significantly increasing DB load on every status call.
FACILITY_LIST = [{"id": id, "name": name} for id, name in Facility.objects.values_list("id", "name")]

return FACILITY_LIST

def flag_facility_cache(**kwargs):
global FACILITY_LIST
FACILITY_LIST = None

post_save.connect(flag_facility_cache, sender=Facility)

class FacilityUserResource(ModelResource):
facility = fields.ForeignKey(FacilityResource, 'facility')
Expand Down Expand Up @@ -188,7 +208,7 @@ def generate_status(self, request, **kwargs):
"messages": message_dicts,
"status_timestamp": datetime.datetime.now(tzlocal()),
"version": version.VERSION,
"facilities": request.session.get("facilities"),
"facilities": facility_list(),
"simplified_login": settings.SIMPLIFIED_LOGIN,
"docs_exist": getattr(settings, "DOCS_EXIST", False),
"zone_id": getattr(Device.get_own_device().get_zone(), "id", "None"),
Expand Down
5 changes: 4 additions & 1 deletion kalite/facility/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,17 @@ def __init__(self, facility, *args, **kwargs):
self.fields["facility"].initial = facility
self.fields["facility"].queryset = Facility.objects.by_zone(facility.get_zone())

if self.fields["facility"].queryset.count() < 2:
self.fields["facility"].widget = forms.HiddenInput()

class Meta:
model = FacilityGroup
fields = ("name", "description", "facility", "zone_fallback", )
widgets = {
"facility": forms.HiddenInput(),
"zone_fallback": forms.HiddenInput(), # TODO(jamalex): this shouldn't be in here
}


def clean_name(self):
name = self.cleaned_data.get("name", "")
ungrouped = re.compile("[uU]+ngrouped")
Expand Down
4 changes: 0 additions & 4 deletions kalite/facility/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ def refresh_session_facility_info(request, facility_count):
request.session["facility_count"] = facility_count
request.session["facility_exists"] = request.session["facility_count"] > 0

# To enable simplified login, also list the id and names of all facilities in the session object.
# This keeps it cached so that the status api call can return this to the client side
# without significantly increasing DB load on every status call.
request.session["facilities"] = [{"id": id, "name": name} for id, name in Facility.objects.values_list("id", "name")]

def flag_facility_cache(**kwargs):
global FACILITY_CACHE_STALE
Expand Down
17 changes: 0 additions & 17 deletions kalite/facility/templates/facility/facility_group.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
{% extends "distributed/base_manage.html" %}
{% load i18n %}

{% block headcss %}{{ block.super }}
<style>
#id_facility {
display: none;
}
</style>
{% endblock headcss %}

{% block headjs %}{{ block.super }}
<script>
$(function () {
$('#facility_group').change(function(){
window.location.href = $("#facility_group option:selected").val();
});

{# Show facility info #}
{% if not singlefacility %}
// Show the dropdown
$("#id_facility").show();
{% else %}
// Show that there's only one facility, and it's being selected.
$(sprintf("<span>%s</span>", $("#id_facility option:selected").text())).insertAfter($("#id_facility"));
{% endif %}
});
</script>
{% endblock headjs %}
Expand Down
1 change: 0 additions & 1 deletion kalite/facility/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def group_edit(request, facility, group_id):
"form": form,
"group_id": group_id,
"facility": facility,
"singlefacility": request.session["facility_count"] == 1,
"title": _("Add a new group") if group_id == 'new' else _("Edit group"),
}