def serialize_exams(queryset): if connection.vendor == "postgresql" and ArrayAgg is not None: queryset = queryset.annotate( exam_assignments=ArrayAgg("assignments__collection")) else: queryset = queryset.values("id").annotate(exam_assignments=GroupConcat( "assignments__collection", output_field=CharField())) return list( map( _map_exam, queryset.values( "id", "title", "active", "question_sources", "data_model_version", "question_count", "learners_see_fixed_order", "seed", "date_created", "date_archived", "date_activated", "archive", "exam_assignments", ), ))
def serialize_groups(queryset): if connection.vendor == "postgresql" and ArrayAgg is not None: queryset = queryset.annotate( member_ids=ArrayAgg("membership__user__id")) else: queryset = queryset.values("id").annotate(member_ids=GroupConcat( "membership__user__id", output_field=CharField())) return list(map(_map_group, queryset.values("id", "name", "member_ids")))
def consolidate(self, items): assignment_ids = [] for item in items: assignment_ids.extend(item["assignment_ids"]) assignments = LessonAssignment.objects.filter(id__in=assignment_ids) if connection.vendor == "postgresql" and ArrayAgg is not None: assignments = assignments.annotate( learner_ids=ArrayAgg("collection__membership__user__id")) def _process_item(item): item["learner_ids"] = map( lambda x: x.hex, filter(lambda x: x, item["learner_ids"])) return item else: assignments = assignments.values("id").annotate( learner_ids=GroupConcat("collection__membership__user__id", output_field=CharField())) def _process_item(item): item["learner_ids"] = (item["learner_ids"].split(",") if item["learner_ids"] else []) return item assignments = { a["id"]: _process_item(a) for a in assignments.values( "id", "collection", "learner_ids", "assigned_by", collection_kind=F("collection__kind"), ) } for item in items: item_ids = item.pop("assignment_ids") item["lesson_assignments"] = [assignments[a] for a in item_ids] item["learner_ids"] = list( set( chain.from_iterable( a.pop("learner_ids") for a in item["lesson_assignments"]))) return items
def serialize_lessons(queryset): if connection.vendor == "postgresql" and ArrayAgg is not None: queryset = queryset.annotate( assignments=ArrayAgg("lesson_assignments__collection")) else: queryset = queryset.values("id").annotate(assignments=GroupConcat( "lesson_assignments__collection", output_field=CharField())) return list( map( _map_lesson, queryset.values( "id", "title", "resources", "assignments", "description", "date_created", active=F("is_active"), ), ))
def annotate_queryset(self, queryset): if connection.vendor == "postgresql" and ArrayAgg is not None: return queryset.annotate(user_ids=ArrayAgg("membership__user__id")) return queryset.values("id").annotate(user_ids=GroupConcat( "membership__user__id", output_field=CharField()))
def annotate_queryset(self, queryset): if connection.vendor == "postgresql" and ArrayAgg is not None: return queryset.annotate( assignment_ids=ArrayAgg("lesson_assignments__id")) return queryset.values("id").annotate(assignment_ids=GroupConcat( "lesson_assignments__id", output_field=CharField()))