Skip to content

Commit

Permalink
Reorganize registration_update async task, rename to section_demand_c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
mureytasroc committed May 2, 2022
1 parent 8d4b3eb commit 89106df
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
11 changes: 7 additions & 4 deletions backend/alert/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def save(self, load_script=False, *args, **kwargs):
and `current_demand_distribution_estimate` cache are asynchronously updated
(via a celery task) to reflect the resulting section demand change.
"""
from alert.tasks import registration_update
from alert.tasks import section_demand_change
from courses.util import get_set_id, is_fk_set

# ^ imported here to avoid circular imports
Expand Down Expand Up @@ -534,9 +534,12 @@ def save(self, load_script=False, *args, **kwargs):
and self.section.semester == get_current_semester()
and was_active != self.is_active
):
registration_update.delay(
self.section.id, was_active, self.is_active, self.updated_at
)
section = self.section
volume_change = int(self.is_active) - int(was_active)
if volume_change > 0 or section.registration_volume >= 1:
section.registration_volume += volume_change
section.save()
section_demand_change.delay(section.id, self.updated_at)

def alert(self, forced=False, sent_by="", close_notification=False):
"""
Expand Down
26 changes: 9 additions & 17 deletions backend/alert/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,20 @@ def recompute_percent_open_async(semester):


@shared_task(name="pca.tasks.registration_update")
def registration_update(section_id, was_active, is_now_active, updated_at):
def section_demand_change(section_id, updated_at):
"""
This method should only be called on sections from the current semester. It updates the
registration_volume of the registration's section, it updates the PcaDemandDistributionEstimate
models and current_demand_distribution_estimate cache to reflect the demand change.
This function should be called when a section's demand changes (i.e. the number of
active registrations changes, or the section's status is updated). It updates the
`PcaDemandDistributionEstimate` model and `current_demand_distribution_estimate`
cache to reflect the demand change.
:param: section_id: the id of the section involved in the demand change
:param: updated_at: the datetime at which the demand change occurred
"""
section = Section.objects.get(id=section_id)
semester = section.semester
assert (
semester == get_current_semester()
), "Error: PCA registration from past semester cannot be updated."
if was_active == is_now_active:
# No change to registration volume
if semester != get_current_semester():
return
volume_change = int(is_now_active) - int(was_active)
if volume_change < 0:
if section.registration_volume >= 1:
section.registration_volume += volume_change
section.save()
else: # volume_change > 0
section.registration_volume += volume_change
section.save()

with transaction.atomic():
create_new_distribution_estimate = False
Expand Down
2 changes: 1 addition & 1 deletion backend/courses/management/commands/merge_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def prompt_for_link(course1, course2):

def same_course(course_a, course_b):
return any(
course_bc.full_code == course_a.full_code
course_bc.full_code == course_a.primary_listing.full_code
for course_bc in course_b.primary_listing.listing_set.all()
)

Expand Down
38 changes: 21 additions & 17 deletions backend/courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models, transaction
from django.db.models import OuterRef, Q, Subquery
from django.db.models import Case, OuterRef, Q, Subquery, Value, When
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
Expand Down Expand Up @@ -326,26 +326,27 @@ def save(self, *args, **kwargs):
if not self.topic:
with transaction.atomic():
primary = self.primary_listing
topics = list(
Topic.objects.select_related("most_recent")
.filter(
Q(most_recent__full_code=primary.full_code)
| Q(most_recent__full_code__in=primary.listing_set.values("full_code")),
)
.select_related("most_recent")
)
if topics:
topic = max(
topics,
key=lambda t: (
int(t.most_recent.full_code == primary.full_code),
t.most_recent.semester,
),
try:
topic = (
Topic.objects.filter(
Q(most_recent__full_code=primary.full_code)
| Q(most_recent__full_code__in=primary.listing_set.values("full_code")),
)
.annotate(
most_recent_match=Case(
When(most_recent__full_code=primary.full_code, then=Value(1)),
default=Value(0),
output_field=models.IntegerField(),
)
)
.order_by("-most_recent_match", "-most_recent__semester")
.select_related("most_recent")[:1]
.get()
)
if topic.most_recent.semester < primary.semester:
topic.most_recent = primary
topic.save()
else:
except Topic.DoesNotExist:
topic = Topic(most_recent=primary)
topic.save()

Expand Down Expand Up @@ -818,6 +819,7 @@ def save(self, *args, **kwargs):
it sets the percent_through_add_drop_period field.
"""
from alert.models import validate_add_drop_semester
from alert.tasks import section_demand_change
from courses.util import get_or_create_add_drop_period

# ^ imported here to avoid circular imports
Expand Down Expand Up @@ -855,6 +857,8 @@ def save(self, *args, **kwargs):
self.section.has_status_updates = True
self.section.save()

section_demand_change.delay(self.section.id, self.created_at)


"""
Schedule Models
Expand Down

0 comments on commit 89106df

Please sign in to comment.