diff --git a/backend/courses/management/commands/recompute_topics.py b/backend/courses/management/commands/recompute_topics.py index c56b3094..6a5a3e4e 100644 --- a/backend/courses/management/commands/recompute_topics.py +++ b/backend/courses/management/commands/recompute_topics.py @@ -32,6 +32,8 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_ - Any single child of a parent inherits the parent's topic. - Any child with siblings that exactly matches their parent in full_code and title inherits the parent's topic. + - Any child with siblings that is the only child with a manually set + parent_course_id inherits the parent topic. - Any child with siblings that does not exactly match their parent gets its own topic. These rules are applied sequentially in increasing order of semester. @@ -53,11 +55,16 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_ Course.objects.filter(semester=semester) .select_related("parent_course", "parent_course__topic") .prefetch_related("parent_course__children") + .order_by("-manually_set_parent_course") ) topics_to_create = [] topics_to_update = [] # .most_recent courses_to_update = [] # .topic + visited_courses = set() + for course in courses: + if course.id in visited_courses: + continue parent = course.parent_course if not parent: topics_to_create.append(Topic(most_recent=course)) @@ -74,6 +81,11 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_ parent.full_code == course.full_code and parent.title == course.title or parent.children.count() == 1 + or ( + sum([child.manually_set_parent_course for child in parent.children.all()]) + == 1 + and course.manually_set_parent_course + ) ): # If a parent has multiple children and none are an exact match, we consider # all the child courses as "branched from" the old. But if one child is an exact @@ -86,6 +98,7 @@ def recompute_topics(min_semester: str = None, verbose=False, allow_null_parent_ topics_to_update.append(course.topic) courses_to_update.append(course) for child in parent.children.all(): + visited_courses.add(child.id) if child.id == course.id and not branched_from: continue topics_to_create.append(Topic(most_recent=child, branched_from=parent.topic)) diff --git a/backend/review/management/commands/precompute_pcr_views.py b/backend/review/management/commands/precompute_pcr_views.py index f318b6db..8ff4f085 100644 --- a/backend/review/management/commands/precompute_pcr_views.py +++ b/backend/review/management/commands/precompute_pcr_views.py @@ -91,8 +91,10 @@ def precompute_pcr_views(verbose=False, is_new_data=False): # Bulk create / update objects. if verbose: print("Creating and updating objects.") - CachedReviewResponse.objects.bulk_create(objs_to_insert) - CachedReviewResponse.objects.bulk_update(objs_to_update, ["response", "expired"]) + CachedReviewResponse.objects.bulk_create(objs_to_insert, batch_size=4000) + CachedReviewResponse.objects.bulk_update( + objs_to_update, ["response", "expired"], batch_size=4000 + ) # Bulk delete objects. if verbose: