feat: update the index of orphan exercises

When an exercise is removed from a lesson, the link to the lesson is
removed from that exercise and the index is reset. This will make sure
the removed exercises won't show up in places like progress.
This commit is contained in:
Anand Chitipothu
2021-06-01 05:59:01 +05:30
parent a12a52747e
commit 400e706be1

View File

@@ -16,6 +16,21 @@ class Lesson(Document):
e = s.get_exercise()
e.lesson = self.name
e.save()
self.update_orphan_exercises()
def update_orphan_exercises(self):
"""Updates the exercises that were previously part of this lesson,
but not any more.
"""
linked_exercises = {row['name'] for row in frappe.get_all('Exercise', {"lesson": self.name})}
active_exercises = {s.id for s in self.get("sections") if s.type=="exercise"}
orphan_exercises = linked_exercises - active_exercises
for name in orphan_exercises:
ex = frappe.get_doc("Exercise", name)
ex.lesson = None
ex.index_ = 0
ex.index_label = ""
ex.save()
def get_sections(self):
return sorted(self.get('sections'), key=lambda s: s.index)