def test_should_give_warning_messages_when_volume_partim_superior_to_volume_parent( self): self.charge_lecturing_1.allocation_charge = 50 self.charge_lecturing_1.save() msgs = get_charge_repartition_warning_messages( self.full_luy.learning_container_year) tutor_name = Person.get_str( self.attribution_full.tutor.person.first_name, self.attribution_full.tutor.person.middle_name, self.attribution_full.tutor.person.last_name) tutor_name_with_function = "{} ({})".format( tutor_name, _(self.attribution_full.get_function_display())) self.assertListEqual(msgs, [ _("The sum of volumes for the partims for professor %(tutor)s is superior to the " "volume of full UE for this professor") % { "tutor": tutor_name_with_function } ])
def get_charge_repartition_warning_messages(learning_container_year): total_charges_by_attribution_and_learning_subtype = AttributionChargeNew.objects \ .filter(attribution__learning_container_year=learning_container_year) \ .order_by("attribution__tutor", "attribution__function", "attribution__start_year") \ .values("attribution__tutor", "attribution__tutor__person__first_name", "attribution__tutor__person__middle_name", "attribution__tutor__person__last_name", "attribution__function", "attribution__start_year", "learning_component_year__learning_unit_year__subtype") \ .annotate(total_volume=Sum("allocation_charge")) charges_by_attribution = itertools.groupby( total_charges_by_attribution_and_learning_subtype, lambda rec: "{}_{}_{}".format(rec["attribution__tutor"], rec[ "attribution__start_year"], rec["attribution__function"])) msgs = [] for attribution_key, charges in charges_by_attribution: charges = list(charges) subtype_key = "learning_component_year__learning_unit_year__subtype" full_total_charges = next( (charge["total_volume"] for charge in charges if charge[subtype_key] == learning_unit_year_subtypes.FULL), 0) partim_total_charges = next( (charge["total_volume"] for charge in charges if charge[subtype_key] == learning_unit_year_subtypes.PARTIM), 0) partim_total_charges = partim_total_charges or 0 full_total_charges = full_total_charges or 0 if partim_total_charges > full_total_charges: tutor_name = Person.get_str( charges[0]["attribution__tutor__person__first_name"], charges[0]["attribution__tutor__person__middle_name"], charges[0]["attribution__tutor__person__last_name"]) tutor_name_with_function = "{} ({})".format( tutor_name, getattr(Functions, charges[0]["attribution__function"]).value) msg = _( "The sum of volumes for the partims for professor %(tutor)s is superior to the " "volume of parent learning unit for this professor") % { "tutor": tutor_name_with_function } msgs.append(msg) return msgs