Skip to content

Commit

Permalink
Merge Logic into Load Status
Browse files Browse the repository at this point in the history
  • Loading branch information
shiva-menta committed Apr 24, 2024
1 parent 525ece7 commit 60e69c8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 92 deletions.
10 changes: 3 additions & 7 deletions backend/alert/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,12 @@ def accept_webhook(request):

# Ignore duplicate updates
last_status_update = section.last_status_update
current_status = section.status
if current_status == course_status:
if last_status_update and last_status_update.new_status == course_status:
raise ValidationError(
f"Status update received changing section {section} from "
f"{prev_status} to {course_status}, "
f"with a current section status of {current_status},"
"after previous status update from "
f"{last_status_update.old_status if last_status_update else 'None'} to"
f"{last_status_update.new_status if last_status_update else 'None'}"
" (duplicate or erroneous).",
f"after previous status update from {last_status_update.old_status} "
f"to {last_status_update.new_status} (duplicate or erroneous).",
)

alert_for_course_called = False
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:
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"]
)
76 changes: 0 additions & 76 deletions backend/courses/management/sync_section_status.py

This file was deleted.

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)
self.assertEqual(0, StatusUpdate.objects.count())

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

0 comments on commit 60e69c8

Please sign in to comment.