Ultron/ultron/objective/management/commands/rebuild_closure_table_tree.py

101 lines
3.9 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 ultron.objective.models import Educative, PrerequisiteClosure
class Command(BaseCommand):
def handle(self, *args, **options):
educative_list = Educative.objects.all()
for educative in educative_list:
updated = False
max_level = 0
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(educative.long_label))
breadcrumb = educative.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, 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 educative.level != max_level:
updated = True
educative.level = max_level
if educative.rank != max_rank:
updated = True
educative.rank = max_rank
if educative.age_boy_with_help < age_boy_with_help:
updated = True
educative.age_boy_with_help = age_boy_with_help
if educative.age_boy_without_help < age_boy_without_help:
updated = True
educative.age_boy_without_help = age_boy_without_help
if educative.age_boy_chained < age_boy_chained:
updated = True
educative.age_boy_chained = age_boy_chained
if educative.age_boy_masterised < age_boy_masterised:
updated = True
educative.age_boy_masterised = age_boy_masterised
if educative.age_girl_with_help < age_girl_with_help:
updated = True
educative.age_girl_with_help = age_girl_with_help
if educative.age_girl_without_help < age_girl_without_help:
updated = True
educative.age_girl_without_help = age_girl_without_help
if educative.age_girl_chained < age_girl_chained:
updated = True
educative.age_girl_chained = age_girl_chained
if educative.age_girl_masterised < age_girl_masterised:
updated = True
educative.age_girl_masterised = age_girl_masterised
if updated:
educative.save()