def benchmark_calculate_course_aggregate(student, course, marking_period, items=None, recalculate_all_categories=False): # doesn't recalculate component aggregates by default if items is None: # QUICK HACK to use new Aggregate calculation method # TODO: Subclass Aggregate and override mark_set for one-off sets of Items agg, created = benchmark_get_create_or_flush( Aggregate, student=student, course=course, marking_period=marking_period, category=None ) agg.calculate(recalculate_all_categories) return agg, created # /HACK (haha, right.) # just leave items alone--we don't actually consider it here; we only pass it to benchmark_calculate_course_category_aggregate # setting items here will prevent benchmark_calculate_course_category_aggregate from saving anything save = True items_categories = () else: # don't store aggregates for every one-off combination of items save = False # we'll have to miss cache and recaculate any category to which an item belongs items_categories = Category.objects.filter(item__in=items).distinct() calculation_rule = benchmark_find_calculation_rule(course.marking_period.all()[0].school_year) # initialize attributes criteria = {"course": course, "category": None, "marking_period": marking_period} # silly name is silly, and should not be part of the criteria silly_name = "G! {} - Course Average ({}, {})".format(student, course, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None course_numer = course_denom = Decimal(0) for rule_category in calculation_rule.per_course_category_set.filter(apply_to_departments=course.department): criteria["category"] = rule_category.category cat_agg, cat_created = benchmark_get_create_or_flush(student.aggregate_set, **criteria) if cat_created or recalculate_all_categories or rule_category.category in items_categories: cat_agg, cat_created = benchmark_calculate_course_category_aggregate( student, course, rule_category.category, marking_period, items ) if cat_agg.cached_value is not None: course_numer += rule_category.weight * cat_agg.cached_value course_denom += rule_category.weight # yes, agg will just end up with the last substitution, but tough if cat_agg.cached_substitution is not None: agg.cached_substitution = cat_agg.cached_substitution if course_denom: agg.cached_value = course_numer / course_denom else: agg.cached_value = None if save: agg.save() return agg, created
def benchmark_calculate_category_as_course_aggregate(student, category, marking_period): agg, created = benchmark_get_create_or_flush(student.aggregate_set, course=None, category=category, marking_period=marking_period) agg.name = 'G! {} - {} (All Courses, {})'.format(student, category, marking_period) agg.cached_substitution = None calculation_rule = benchmark_find_calculation_rule(marking_period.school_year) category_as_course = calculation_rule.category_as_course_set.get(category=category) category_numer = category_denom = Decimal(0) for course in Course.objects.filter(award_credits=True, courseenrollment__user__username=student.username, marking_period=marking_period, department__in=category_as_course.include_departments.all()).distinct(): credits = Decimal(course.credits) / course.marking_period.count() try: category_aggregate = benchmark_get_or_flush(Aggregate, student=student, marking_period=marking_period, category=category, course=course) except Aggregate.DoesNotExist: category_aggregate = benchmark_calculate_course_category_aggregate(student, course, category, marking_period)[0] if category_aggregate is not None and category_aggregate.cached_value is not None: calculate_as, display_as = calculation_rule.substitute(category_aggregate, category_aggregate.cached_value) category_numer += credits * calculate_as category_denom += credits # yes, agg will just end up with the last substitution, but tough if display_as is not None and len(display_as): agg.cached_substitution = display_as if category_denom: agg.cached_value = category_numer / category_denom else: agg.cached_value = None agg.save() return agg, created
def benchmark_calculate_category_as_course_aggregate(student, category, marking_period): agg, created = benchmark_get_create_or_flush(student.aggregate_set, course_section=None, category=category, marking_period=marking_period) agg.name = 'G! {} - {} (All Courses, {})'.format(student, category, marking_period) agg.cached_substitution = None calculation_rule = benchmark_find_calculation_rule( marking_period.school_year) category_as_course = calculation_rule.category_as_course_set.get( category=category) category_numer = category_denom = Decimal(0) for course_section in CourseSection.objects.filter( course__course_type__award_credits=True, courseenrollment__user__username=student.username, marking_period=marking_period, department__in=category_as_course.include_departments.all( )).distinct(): credits = Decimal( course_section.credits) / course_section.marking_period.count() try: category_aggregate = benchmark_get_or_flush( Aggregate, student=student, marking_period=marking_period, category=category, course_section=course_section) except Aggregate.DoesNotExist: category_aggregate = benchmark_calculate_course_category_aggregate( student, course_section, category, marking_period)[0] if category_aggregate is not None and category_aggregate.cached_value is not None: calculate_as, display_as = calculation_rule.substitute( category_aggregate, category_aggregate.cached_value) category_numer += credits * calculate_as category_denom += credits # yes, agg will just end up with the last substitution, but tough if display_as is not None and len(display_as): agg.cached_substitution = display_as if category_denom: agg.cached_value = category_numer / category_denom else: agg.cached_value = None agg.save() return agg, created
def benchmark_calculate_course_aggregate(student, course_section, marking_period, items=None, recalculate_all_categories=False): # doesn't recalculate component aggregates by default if items is None: # QUICK HACK to use new Aggregate calculation method # TODO: Subclass Aggregate and override mark_set for one-off sets of Items agg, created = benchmark_get_create_or_flush( Aggregate, student=student, course_section=course_section, marking_period=marking_period, category=None) agg.calculate(recalculate_all_categories) return agg, created # /HACK (haha, right.) # just leave items alone--we don't actually consider it here; we only pass it to benchmark_calculate_course_category_aggregate # setting items here will prevent benchmark_calculate_course_category_aggregate from saving anything save = True items_categories = () else: # don't store aggregates for every one-off combination of items save = False # we'll have to miss cache and recaculate any category to which an item belongs items_categories = Category.objects.filter(item__in=items).distinct() calculation_rule = benchmark_find_calculation_rule( course_section.marking_period.all()[0].school_year) # initialize attributes criteria = { 'course_section': course_section, 'category': None, 'marking_period': marking_period } # silly name is silly, and should not be part of the criteria silly_name = 'G! {} - Course Average ({}, {})'.format( student, course_section, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None course_section_numer = course_section_denom = Decimal(0) for rule_category in calculation_rule.per_course_category_set.filter( apply_to_departments=course_section.department): criteria['category'] = rule_category.category cat_agg, cat_created = benchmark_get_create_or_flush( student.aggregate_set, **criteria) if cat_created or recalculate_all_categories or rule_category.category in items_categories: cat_agg, cat_created = benchmark_calculate_course_category_aggregate( student, course_section, rule_category.category, marking_period, items) if cat_agg.cached_value is not None: course_section_numer += rule_category.weight * cat_agg.cached_value course_section_denom += rule_category.weight # yes, agg will just end up with the last substitution, but tough if cat_agg.cached_substitution is not None: agg.cached_substitution = cat_agg.cached_substitution if course_section_denom: agg.cached_value = course_section_numer / course_section_denom else: agg.cached_value = None if save: agg.save() return agg, created