Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_facility(self):
"""
Gets the ``Classroom``'s parent ``Facility``.
:return: A ``Facility`` instance.
"""
return Facility.objects.get(id=self.parent_id)
def _user_is_admin_for_own_facility(self, user, obj=None):
# import here to avoid circular imports
from ..models import Facility
if not hasattr(user, "dataset"):
return False
# if we've been given an object, make sure it too is from the same dataset (facility)
if obj:
if not hasattr(obj, "dataset") or not user.dataset == obj.dataset:
return False
facility = Facility.objects.get(dataset=user.dataset)
return user.has_role_for_collection(role_kinds.ADMIN, 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(
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 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 _user_is_admin_for_own_facility(user, obj=None):
# import here to avoid circular imports
from ..models import Facility
if not hasattr(user, "dataset"):
return False
# if we've been given an object, make sure it too is from the same dataset (facility)
if obj:
if not hasattr(obj, "dataset") or not user.dataset == obj.dataset:
return False
facility = Facility.objects.get(dataset=user.dataset)
return user.has_role_for_collection(role_kinds.ADMIN, 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
)
)
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