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_members(self):
if self.kind == collection_kinds.FACILITY:
return FacilityUser.objects.filter(
dataset=self.dataset
) # FacilityUser is always a member of her own facility
return HierarchyRelationsFilter(FacilityUser).filter_by_hierarchy(
target_user=F("id"), ancestor_collection=self
)
def annotate_queryset(self, queryset):
return queryset.annotate(
learner_count=SQCount(
FacilityUser.objects.filter(memberships__collection=OuterRef("id")),
field="id",
)
def list(self, request):
# The filter is very loose, since we are assuming that the only
# users are from the new facility
queryset = FacilityUser.objects.filter(roles__kind__contains="admin")
response_data = [
{"username": user.username, "id": user.id} for user in queryset
]
return Response(response_data)
# if resource does not exist, check if another resource with same content_id exists
nodes = ContentNode.objects.filter(
content_id=content_id_map[node_id]
)
if nodes:
node_ids.append(nodes[0].id)
# point to new list of node ids
lesson["node_ids"] = node_ids
learners_data = serialize_users(query_learners)
output = {
"id": pk,
"name": classroom.name,
"coaches": serialize_users(
FacilityUser.objects.filter(
roles__collection=classroom, roles__kind=role_kinds.COACH
)
),
"learners": learners_data,
"groups": serialize_groups(classroom.get_learner_groups()),
"adhoclearners": serialize_groups(
classroom.get_individual_learners_group()
),
"exams": exam_data,
"exam_learner_status": serialize_exam_status(query_exam_logs),
"content": query_content.values(
"content_id", "title", "kind", "channel_id", node_id=F("id")
),
"content_learner_status": content_status_serializer(
lesson_data, learners_data, classroom
),
def get_members_or_user(collection_kind, collection_id):
if "user" == collection_kind:
return FacilityUser.objects.filter(pk=collection_id)
else: # if not user, then it must be a collection
return (
Collection.objects.filter(kind=collection_kind)
.get(pk=collection_id)
.get_members()
def extract_facility_statistics(facility):
dataset_id = facility.dataset_id
settings = {
name: getattr(facility.dataset, name)
for name in facility_settings
if hasattr(facility.dataset, name)
}
settings.update(allow_guest_access=allow_guest_access())
learners = FacilityUser.objects.filter(dataset_id=dataset_id).exclude(
roles__kind__in=[role_kinds.ADMIN, role_kinds.COACH]
)
coaches = FacilityUser.objects.filter(
dataset_id=dataset_id, roles__kind__in=[role_kinds.ADMIN, role_kinds.COACH]
)
usersessions = UserSessionLog.objects.filter(dataset_id=dataset_id)
contsessions = ContentSessionLog.objects.filter(
dataset_id=dataset_id, time_spent__lt=3600 * 2
)
# the aggregates below are used to calculate the first and most recent times this device was used
usersess_agg = usersessions.filter(
start_timestamp__gt=datetime.datetime(2016, 1, 1)
).aggregate(first=Min("start_timestamp"), last=Max("last_interaction_timestamp"))
contsess_agg = contsessions.filter(
def annotate_queryset(self, queryset):
return (
queryset.annotate(
num_users=SQCount(
FacilityUser.objects.filter(facility=OuterRef("id")), field="id"
)
)
.annotate(
num_classrooms=SQCount(
Classroom.objects.filter(parent=OuterRef("id")), field="id"
)
)
.annotate(
last_synced=Subquery(
TransferSession.objects.filter(
filter=Cast(OuterRef("dataset"), TextField())
)
.order_by("-last_activity_timestamp")
.values("last_activity_timestamp")
)
superuser = FacilityUser.objects.create(
username="superuser", facility=facility
)
superuser.set_password("password")
superuser.save()
DevicePermissions.objects.create(
user=superuser, is_superuser=True, can_manage_content=True
)
print(
"Temporary superuser with username: `superuser` and password: `password` created"
)
return
username = input(
"Please enter username of account that will become the superuser on this device: "
)
if not FacilityUser.objects.filter(username=username).exists():
print(
"User with username `{}` does not exist on this device".format(username)
)
username = None
continue
# make the user with the given credentials, a superuser for this device
user = FacilityUser.objects.get(username=username, dataset_id=dataset_id)
# create permissions for the authorized user
DevicePermissions.objects.update_or_create(
user=user, defaults={"is_superuser": True, "can_manage_content": True}
)