예제 #1
0
파일: api.py 프로젝트: chrislun16/kolibri
 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=Func(
                         Cast(OuterRef("dataset"), TextField()),
                         Value("-"),
                         Value(""),
                         function="replace",
                         output_field=TextField(),
                     )
                 )
                 .order_by("-last_activity_timestamp")
                 .values("last_activity_timestamp")[:1]
             )
         )
     )
예제 #2
0
def csv_file_generator(facility, filepath, overwrite=True, demographic=False):
    if not overwrite and os.path.exists(filepath):
        raise ValueError("{} already exists".format(filepath))
    queryset = FacilityUser.objects.filter(facility=facility)

    header_labels = tuple(label for field, label in labels.items()
                          if demographic or field not in DEMO_FIELDS)

    columns = tuple(column for column in db_columns
                    if demographic or column not in DEMO_FIELDS)

    csv_file = open_csv_for_writing(filepath)

    with csv_file as f:
        writer = csv.DictWriter(f, header_labels)
        logger.info("Creating csv file {filename}".format(filename=filepath))
        writer.writeheader()
        usernames = set()
        for item in (queryset.select_related("facility").annotate(
                classroom_count=SQCount(
                    Classroom.objects.filter(membership__user=OuterRef("id")),
                    field="id",
                )).prefetch_related("memberships__collection").filter(
                    Q(memberships__collection__kind=CLASSROOM)
                    | Q(memberships__collection__isnull=True)).values(
                        *columns)):
            if item["username"] not in usernames:
                writer.writerow(map_output(item))
                usernames.add(item["username"])
                yield
예제 #3
0
 def annotate_queryset(self, queryset):
     return queryset.annotate(
         learner_count=SQCount(
             FacilityUser.objects.filter(memberships__collection=OuterRef("id")),
             field="id",
         )
     )