from assignments.models import Assignment
from django.db.models import Q, Count

"""
Get all assignments, evaluate all submissions and final (=shared) submissions and sort them by Topic and Module

todo:  make this a Django command
"""
num_final = Count('submissions', filter=Q(submissions__final=True))
num_submissions = Count('submissions')
assignments=Assignment.objects.annotate(num_submissions=num_submissions).annotate(num_final=num_final).select_related('module').order_by('module')

topics = {}
for a in assignments:
    topic = a.module.get_parent()
    topic_title = topic.title
    if topic_title not in topics:
        topics[topic_title] = []
    topics[topic_title].append(f'{a.module},{a.title},{a.num_final},{a.num_submissions}')

for t, value in topics.items():
    for e in value:
        print(f'{t},{e}')

