"""Return ordered groups of lessons."""

from collections import OrderedDict
from topics.models import (
    AgeGroup,
    LessonNumber,
)


def group_lessons_by_age(lessons):
    """Return ordered groups of lessons.

    Lessons are grouped by the lesson minimum age and maximum ages,
    and then order by number.

    Args:
        lessons: QuerySet of Lesson objects (QuerySet).

    Returns:
        A ordered dictionary of grouped lessons.
        The key is a tuple of the minimum age and maximum ages for
        the lessons.
        The value for a key is a sorted list of lessons (ordered by number).
    """
    grouped_lessons = OrderedDict()
    for age_group in AgeGroup.objects.distinct():
        for lesson in age_group.lessons.filter(id__in=lessons).order_by("lessonnumber"):
            lesson.number = LessonNumber.objects.get(lesson=lesson, age_group=age_group).number
            if age_group in grouped_lessons.keys():
                grouped_lessons[age_group].append(lesson)
            else:
                grouped_lessons[age_group] = [lesson]
    return grouped_lessons

@@ -974,21 +974,13 @@
 %5Bage
-_group%5D.appen
+s%5D.ad
 d(le
@@ -1042,26 +1042,25 @@
 %5Bage
-_group%5D = 
+s%5D = set(
 %5Blesson%5D
 %0A   
@@ -1055,16 +1055,17 @@
 %5Blesson%5D
+)
 %0A    ret

Fix bug where lessons are duplicated across age groups.