Jarvis/jarvis/objective/management/commands/rebuild_closure_table_tree_...

123 lines
4.4 KiB
Python

"""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 Routine, PrerequisiteClosure
class Command(BaseCommand):
def handle(self, *args, **options):
routine_list = Routine.objects.all()
count = 0
for routine in routine_list:
count += 1
updated = False
max_level = routine.difficulty * 10
# if educative.
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(routine.long_label))
breadcrumb = routine.breadcrumb()
for path in range(0, len(breadcrumb)):
tree = set(
PrerequisiteClosure.objects.filter(descendant=educative, path=path)
)
for position, ancestor in enumerate(breadcrumb[path]):
tree_path, _ = PrerequisiteClosure.objects.get_or_create(
ancestor=ancestor,
descendant=educative,
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 routine.level != max_level:
updated = True
routine.level = max_level
if routine.rank != max_rank:
updated = True
routine.rank = max_rank
if routine.age_boy_with_help < age_boy_with_help:
updated = True
routine.age_boy_with_help = age_boy_with_help
if routine.age_boy_without_help < age_boy_without_help:
updated = True
routine.age_boy_without_help = age_boy_without_help
if routine.age_boy_chained < age_boy_chained:
updated = True
routine.age_boy_chained = age_boy_chained
if routine.age_boy_masterised < age_boy_masterised:
updated = True
routine.age_boy_masterised = age_boy_masterised
if routine.age_girl_with_help < age_girl_with_help:
updated = True
routine.age_girl_with_help = age_girl_with_help
if routine.age_girl_without_help < age_girl_without_help:
updated = True
routine.age_girl_without_help = age_girl_without_help
if routine.age_girl_chained < age_girl_chained:
updated = True
routine.age_girl_chained = age_girl_chained
if routine.age_girl_masterised < age_girl_masterised:
updated = True
routine.age_girl_masterised = age_girl_masterised
if updated:
routine.save()