"""This command manages Closure Tables implementation It adds new levels and cleans links between Educatives. This way, it's relatively easy to fetch an entire tree with just one tiny request. """ from django.core.management.base import BaseCommand from jarvis.objective.models import Skill, PrerequisiteClosure class Command(BaseCommand): def handle(self, *args, **options): skill_list = Skill.objects.all() count = 0 for skill in skill_list: count += 1 updated = False max_level = skill.difficulty * 10 if skill.position == "/": max_level += 1 max_rank = 0 age_boy_with_help = 0 age_boy_without_help = 0 age_boy_chained = 0 age_boy_masterised = 0 age_girl_with_help = 0 age_girl_without_help = 0 age_girl_chained = 0 age_girl_masterised = 0 print(str(count) + " - Traitement de " + str(skill.long_label)) breadcrumb = skill.breadcrumb() for path in range(0, len(breadcrumb)): tree = set( PrerequisiteClosure.objects.filter(descendant=skill, path=path) ) for position, ancestor in enumerate(breadcrumb[path]): tree_path, _ = PrerequisiteClosure.objects.get_or_create( ancestor=ancestor, descendant=skill, level=position, path=path, ) max_level = max(max_level, position) max_rank = max(max_rank, max_level, ancestor.rank + 1) age_boy_with_help = max( age_boy_with_help, ancestor.age_boy_with_help ) age_boy_without_help = max( age_boy_without_help, ancestor.age_boy_without_help ) age_boy_chained = max(age_boy_chained, ancestor.age_boy_chained) age_boy_masterised = max( age_boy_masterised, ancestor.age_boy_masterised ) age_girl_with_help = max( age_girl_with_help, ancestor.age_girl_with_help ) age_girl_without_help = max( age_girl_without_help, ancestor.age_girl_without_help ) age_girl_chained = max(age_girl_chained, ancestor.age_girl_chained) age_girl_masterised = max( age_girl_masterised, ancestor.age_girl_masterised ) if tree_path in tree: tree.remove(tree_path) for tree_path in tree: tree_path.delete() if skill.level != max_level: updated = True skill.level = max_level if skill.rank != max_rank: updated = True skill.rank = max_rank if skill.age_boy_with_help < age_boy_with_help: updated = True skill.age_boy_with_help = age_boy_with_help if skill.age_boy_without_help < age_boy_without_help: updated = True skill.age_boy_without_help = age_boy_without_help if skill.age_boy_chained < age_boy_chained: updated = True skill.age_boy_chained = age_boy_chained if skill.age_boy_masterised < age_boy_masterised: updated = True skill.age_boy_masterised = age_boy_masterised if skill.age_girl_with_help < age_girl_with_help: updated = True skill.age_girl_with_help = age_girl_with_help if skill.age_girl_without_help < age_girl_without_help: updated = True skill.age_girl_without_help = age_girl_without_help if skill.age_girl_chained < age_girl_chained: updated = True skill.age_girl_chained = age_girl_chained if skill.age_girl_masterised < age_girl_masterised: updated = True skill.age_girl_masterised = age_girl_masterised if updated: skill.save()