Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCA Resyncing Logic #668

Merged
merged 15 commits into from
Nov 11, 2024
19 changes: 10 additions & 9 deletions backend/alert/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ def accept_webhook(request):
else:
response = JsonResponse({"message": "webhook recieved"})

u = record_update(
section,
course_term,
prev_status,
course_status,
alert_for_course_called,
request.body,
)
update_course_from_record(u)
with transaction.atomic():
u = record_update(
section,
course_term,
prev_status,
course_status,
alert_for_course_called,
request.body,
)
update_course_from_record(u)
except (ValidationError, ValueError) as e:
logger.error(e, extra={"request": request})
response = JsonResponse(
Expand Down
52 changes: 46 additions & 6 deletions backend/courses/management/commands/loadstatus.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import json
import logging

from django.core.management.base import BaseCommand
from tqdm import tqdm

from courses import registrar
from courses.models import Course, Section
from courses.util import get_course_and_section, get_current_semester
from courses.util import (
get_course_and_section,
get_current_semester,
record_update,
translate_semester_inv,
)


def set_all_status(semester=None):
def set_all_status(semester=None, add_status_update=False):
if semester is None:
semester = get_current_semester()
statuses = registrar.get_all_course_status(semester)
Expand All @@ -19,22 +25,56 @@ def set_all_status(semester=None):
if section_code is None:
continue

course_status = status.get("status")
if course_status is None:
continue

course_term = status.get("term")
if course_term is None:
continue
if any(course_term.endswith(s) for s in ["10", "20", "30"]):
course_term = translate_semester_inv(course_term)

# Ignore sections not in db
try:
_, section = get_course_and_section(section_code, semester)
except (Section.DoesNotExist, Course.DoesNotExist):
continue
section.status = status["status"]
section.save()

# Resync database (doesn't need to be atomic)
last_status_update = section.last_status_update
current_status = section.status

# Change status attribute of section model (might want to use bulk update)
if current_status != course_status:
section.status = course_status
section.save()

# Add corresponding status update object
if add_status_update and last_status_update.new_status != course_status:
shiva-menta marked this conversation as resolved.
Show resolved Hide resolved
record_update(
section,
course_term,
last_status_update.new_status,
course_status,
False,
json.dumps(status),
)


class Command(BaseCommand):
help = "Load course status for courses in the DB"
help = "Load course status for courses in the DB. Conditionally adds StatusUpdate objects."

def add_arguments(self, parser):
parser.add_argument("--semester", default=None, type=str)
parser.add_argument(
"--create-status-updates", action="store_false", help="Create status updates if set"
)

def handle(self, *args, **kwargs):
root_logger = logging.getLogger("")
root_logger.setLevel(logging.DEBUG)

set_all_status(semester=kwargs["semester"])
set_all_status(
semester=kwargs["semester"], add_status_update=kwargs["create-status-updates"]
)
4 changes: 1 addition & 3 deletions backend/tests/alert/test_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,9 +806,7 @@ def test_alert_called_wrong_sem(self, mock_alert):
"sent" in json.loads(res.content)["message"],
)
self.assertFalse(mock_alert.called)
self.assertEqual(1, StatusUpdate.objects.count())
u = StatusUpdate.objects.get()
self.assertFalse(u.alert_sent)
shiva-menta marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(0, StatusUpdate.objects.count())

def test_alert_called_alerts_off(self, mock_alert):
Option.objects.update_or_create(
Expand Down
Loading