Skip to content

Commit 60e69c8

Browse files
committed
Merge Logic into Load Status
1 parent 525ece7 commit 60e69c8

File tree

4 files changed

+50
-92
lines changed

4 files changed

+50
-92
lines changed

backend/alert/views.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,12 @@ def accept_webhook(request):
110110

111111
# Ignore duplicate updates
112112
last_status_update = section.last_status_update
113-
current_status = section.status
114-
if current_status == course_status:
113+
if last_status_update and last_status_update.new_status == course_status:
115114
raise ValidationError(
116115
f"Status update received changing section {section} from "
117116
f"{prev_status} to {course_status}, "
118-
f"with a current section status of {current_status},"
119-
"after previous status update from "
120-
f"{last_status_update.old_status if last_status_update else 'None'} to"
121-
f"{last_status_update.new_status if last_status_update else 'None'}"
122-
" (duplicate or erroneous).",
117+
f"after previous status update from {last_status_update.old_status} "
118+
f"to {last_status_update.new_status} (duplicate or erroneous).",
123119
)
124120

125121
alert_for_course_called = False
Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import json
12
import logging
23

34
from django.core.management.base import BaseCommand
45
from tqdm import tqdm
56

67
from courses import registrar
78
from courses.models import Course, Section
8-
from courses.util import get_course_and_section, get_current_semester
9+
from courses.util import (
10+
get_course_and_section,
11+
get_current_semester,
12+
record_update,
13+
translate_semester_inv,
14+
)
915

1016

11-
def set_all_status(semester=None):
17+
def set_all_status(semester=None, add_status_update=False):
1218
if semester is None:
1319
semester = get_current_semester()
1420
statuses = registrar.get_all_course_status(semester)
@@ -19,22 +25,56 @@ def set_all_status(semester=None):
1925
if section_code is None:
2026
continue
2127

28+
course_status = status.get("status")
29+
if course_status is None:
30+
continue
31+
32+
course_term = status.get("term")
33+
if course_term is None:
34+
continue
35+
if any(course_term.endswith(s) for s in ["10", "20", "30"]):
36+
course_term = translate_semester_inv(course_term)
37+
38+
# Ignore sections not in db
2239
try:
2340
_, section = get_course_and_section(section_code, semester)
2441
except (Section.DoesNotExist, Course.DoesNotExist):
2542
continue
26-
section.status = status["status"]
27-
section.save()
43+
44+
# Resync database (doesn't need to be atomic)
45+
last_status_update = section.last_status_update
46+
current_status = section.status
47+
48+
# Change status attribute of section model (might want to use bulk update)
49+
if current_status != course_status:
50+
section.status = course_status
51+
section.save()
52+
53+
# Add corresponding status update object
54+
if add_status_update and last_status_update.new_status != course_status:
55+
record_update(
56+
section,
57+
course_term,
58+
last_status_update.new_status,
59+
course_status,
60+
False,
61+
json.dumps(status),
62+
)
2863

2964

3065
class Command(BaseCommand):
31-
help = "Load course status for courses in the DB"
66+
help = "Load course status for courses in the DB. Conditionally adds StatusUpdate objects."
3267

3368
def add_arguments(self, parser):
3469
parser.add_argument("--semester", default=None, type=str)
70+
parser.add_argument(
71+
"--create-status-updates", action="store_false", help="Create status updates if set"
72+
)
3573

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

40-
set_all_status(semester=kwargs["semester"])
78+
set_all_status(
79+
semester=kwargs["semester"], add_status_update=kwargs["create-status-updates"]
80+
)

backend/courses/management/sync_section_status.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

backend/tests/alert/test_alert.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,7 @@ def test_alert_called_wrong_sem(self, mock_alert):
806806
"sent" in json.loads(res.content)["message"],
807807
)
808808
self.assertFalse(mock_alert.called)
809-
self.assertEqual(1, StatusUpdate.objects.count())
810-
u = StatusUpdate.objects.get()
811-
self.assertFalse(u.alert_sent)
809+
self.assertEqual(0, StatusUpdate.objects.count())
812810

813811
def test_alert_called_alerts_off(self, mock_alert):
814812
Option.objects.update_or_create(

0 commit comments

Comments
 (0)