Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def perform_statistics(self, server, pingback_id):
url = urljoin(server, "/api/v1/statistics")
channels = [extract_channel_statistics(c) for c in ChannelMetadata.objects.all()]
facilities = [extract_facility_statistics(f) for f in Facility.objects.all()]
data = {
"pi": pingback_id,
"c": channels,
"f": facilities,
}
logger.debug("Statistics data: {}".format(data))
jsondata = dump_zipped_json(data)
response = requests.post(url, data=jsondata, timeout=60)
response.raise_for_status()
return json.loads(response.content.decode() or "{}")
def get_facility(facility_id=None, noninteractive=False):
# try to get a valid facility from id
if facility_id:
try:
facility = Facility.objects.get(id=facility_id)
except Facility.DoesNotExist:
raise CommandError("Facility with ID {} does not exist".format(facility_id))
# if no id passed in, assume only one facility on device
else:
try:
facility = Facility.objects.get()
except Facility.DoesNotExist:
raise CommandError(
(
"There are no facilities on this device. "
"Please initialize your Kolibri installation by starting the server, loading Kolibri in the browser, "
"and completing the setup instructions. "
)
)
except Facility.MultipleObjectsReturned:
if noninteractive:
raise CommandError(
(
"There are multiple facilities on this device. "
"Please pass in a facility ID by passing in --facility {ID} after the command."
)
)
else:
def get_facility(self, options):
if options["facility"]:
default_facility = Facility.objects.get(pk=options["facility"])
else:
default_facility = Facility.get_default_facility()
if not default_facility:
self.overall_error.append(MESSAGES[NO_FACILITY])
raise CommandError(self.overall_error[-1])
return default_facility
def handle(self, *args, **options):
if options["facility"]:
default_facility = Facility.objects.get(pk=options["facility"])
else:
default_facility = Facility.get_default_facility()
if not default_facility:
raise CommandError(
"No default facility exists, please make sure to provision this device before running this command"
)
fieldnames = ["full_name", "username", "password", "facility", "class"]
# open using default OS encoding
with open(options["filepath"]) as f:
header = next(csv.reader(f, strict=True))
if all(col in fieldnames for col in header):
# Every item in the first row matches an item in the fieldnames, it is a header row
if "username" not in header:
raise CommandError(
"No usernames specified, this is required for user creation"
)
from .serializers import TotalContentProgressSerializer
from .serializers import UserSessionLogSerializer
from kolibri.core.auth.api import KolibriAuthPermissions
from kolibri.core.auth.api import KolibriAuthPermissionsFilter
from kolibri.core.auth.filters import HierarchyRelationsFilter
from kolibri.core.auth.models import Classroom
from kolibri.core.auth.models import Collection
from kolibri.core.auth.models import Facility
from kolibri.core.auth.models import FacilityUser
from kolibri.core.auth.models import LearnerGroup
from kolibri.core.content.api import OptionalPageNumberPagination
from kolibri.core.exams.models import Exam
class BaseLogFilter(FilterSet):
facility = ModelChoiceFilter(method="filter_facility", queryset=Facility.objects.all())
classroom = ModelChoiceFilter(method="filter_classroom", queryset=Classroom.objects.all())
learner_group = ModelChoiceFilter(method="filter_learner_group", queryset=LearnerGroup.objects.all())
# Only a superuser can filter by facilities
def filter_facility(self, queryset, name, value):
return queryset.filter(user__facility=value)
def filter_classroom(self, queryset, name, value):
return HierarchyRelationsFilter(queryset).filter_by_hierarchy(
ancestor_collection=value,
target_user=F("user"),
)
def filter_learner_group(self, queryset, name, value):
return HierarchyRelationsFilter(queryset).filter_by_hierarchy(
ancestor_collection=value,
def infer_facility(user, default_facility):
if "facility" in user and user["facility"]:
try:
# Try lookup by id first, then name
return Facility.objects.get(pk=user["facility"])
except (Facility.DoesNotExist, ValueError):
try:
return Facility.objects.get(name=user["facility"])
except Facility.DoesNotExist:
raise CommandError(
"Facility matching identifier {facility} was not found".format(
facility=user["facility"]
)
)
else:
return default_facility
def infer_facility(facility_id, facility=None):
if facility_id:
try:
# Try lookup by id first, then name
facility = Facility.objects.get(pk=facility_id)
except (Facility.DoesNotExist, ValueError):
try:
facility = Facility.objects.get(name=facility_id)
except Facility.DoesNotExist:
raise ValueError(
"Facility matching identifier {facility} was not found".format(
facility=facility_id
)
)
elif facility is not None:
return facility
else:
facility = Facility.get_default_facility()
if facility:
logger.info(
"No facility specified, using the default facility {}".format(
facility.name
)
)
else:
def infer_facility(facility_id, facility=None):
if facility_id:
try:
# Try lookup by id first, then name
facility = Facility.objects.get(pk=facility_id)
except (Facility.DoesNotExist, ValueError):
try:
facility = Facility.objects.get(name=facility_id)
except Facility.DoesNotExist:
raise ValueError(
"Facility matching identifier {facility} was not found".format(
facility=facility_id
)
)
elif facility is not None:
return facility
else:
facility = Facility.get_default_facility()
if facility:
logger.info(
"No facility specified, using the default facility {}".format(