2022-01-12 21:03:43 +01:00
|
|
|
"""This command manages Closure Tables implementation
|
2022-01-12 21:29:17 +01:00
|
|
|
|
|
|
|
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):
|
2022-01-13 18:43:43 +01:00
|
|
|
educative_list = Educative.objects.all()
|
2022-01-14 20:06:29 +01:00
|
|
|
count = 0
|
2022-01-12 21:29:17 +01:00
|
|
|
for educative in educative_list:
|
2022-01-19 13:44:22 +01:00
|
|
|
updated = False
|
|
|
|
max_level = 0
|
|
|
|
max_rank = 0
|
2022-01-20 13:46:03 +01:00
|
|
|
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
|
2022-01-14 20:06:29 +01:00
|
|
|
count += 1
|
|
|
|
print(str(count) + ' - Traitement de ' + str(educative.long_label))
|
2022-01-12 21:29:17 +01:00
|
|
|
breadcrumb = educative.breadcrumb()
|
|
|
|
|
|
|
|
for path in range(0, len(breadcrumb)):
|
|
|
|
tree = set(PrerequisiteClosure.objects.filter(descendant=educative, path=path))
|
2022-01-14 20:06:29 +01:00
|
|
|
|
2022-01-12 21:29:17 +01:00
|
|
|
for position, ancestor in enumerate(breadcrumb[path]):
|
|
|
|
tree_path, _ = PrerequisiteClosure.objects.get_or_create(
|
|
|
|
ancestor=ancestor, descendant=educative, level=position, path=path
|
|
|
|
)
|
2022-01-19 13:44:22 +01:00
|
|
|
max_level = max(max_level, position)
|
|
|
|
max_rank = max(max_rank, ancestor.rank + 1)
|
2022-01-20 13:46:03 +01:00
|
|
|
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)
|
2022-01-12 21:29:17 +01:00
|
|
|
|
|
|
|
if tree_path in tree:
|
|
|
|
tree.remove(tree_path)
|
|
|
|
|
|
|
|
for tree_path in tree:
|
|
|
|
tree_path.delete()
|
2022-01-19 13:44:22 +01:00
|
|
|
|
|
|
|
# Dans une condition pour espérer accélérer le traitement
|
|
|
|
if educative.level != max_level:
|
|
|
|
updated = True
|
|
|
|
educative.level = max_level
|
|
|
|
|
|
|
|
if educative.rank != max_rank:
|
|
|
|
updated = True
|
|
|
|
educative.rank = max_rank
|
|
|
|
|
2022-01-20 13:46:03 +01:00
|
|
|
# Age boy defining
|
|
|
|
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
|
|
|
|
|
|
|
|
# Age girl defined
|
|
|
|
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
|
|
|
|
|
2022-01-19 13:44:22 +01:00
|
|
|
if updated:
|
|
|
|
educative.save()
|