Skip to content
18 changes: 18 additions & 0 deletions tcf_website/management/commands/load_semester.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ def load_course(
attribute.save()
attrs.append(attribute)
course.disciplines.set(attrs)
if not pd.isnull(description): #prerequisites
pre_req_format = "Pre-requisite" if "Pre-requisite" in description else "Prerequisite"
if pre_req_format in description:
# Get pre_req from beginning to end
from_pre_req_to_end = description[description.find(pre_req_format) :]
# Get rid of title of "Prerequisite" or "Pre-requisite"
pre_req_no_title = from_pre_req_to_end[from_pre_req_to_end.find(":") + 1 :]

# Check if in-line or not for pre_req
if pre_req_no_title.find(".") > 0:
pre_req_text = pre_req_no_title[: pre_req_no_title.find(".")]
else:
pre_req_text = pre_req_no_title

# Match only on course mnemonic and code
matches = re.findall(r'([A-Z]{2,4}\s?\d{4})', pre_req_text)
prereq_codes = [m.strip().upper() for m in matches]
course.prerequisites = prereq_codes
if not course.description and not pd.isnull(description):
course.description = description
if not course.title and not pd.isnull(title):
Expand Down
18 changes: 18 additions & 0 deletions tcf_website/migrations/0024_course_prerequisites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated manually for prerequisites field

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("tcf_website", "0023_remove_sectionenrollment_section_and_more"),
]

operations = [
migrations.AddField(
model_name="course",
name="prerequisites",
field=models.JSONField(blank=True, default=list),
),
]
2 changes: 2 additions & 0 deletions tcf_website/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ class Course(models.Model):
description = models.TextField(blank=True)
# Course disciplines. Optional.
disciplines = models.ManyToManyField(Discipline, blank=True)
# Course prerequisites. Optional.
prerequisites = models.JSONField(default=list, blank=True)

# Course number. Required.
number = models.IntegerField(
Expand Down
13 changes: 13 additions & 0 deletions tcf_website/tests/test_course.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,16 @@ def test_student_eval_link(self):
# need to break into 2 lines because otherwise pylint gets mad
# this link doesn't actually work because CS 420 is not a real class
self.assertEqual(eval_link, self.course.eval_link())

def test_prerequisites_default(self):
"""Test that prerequisites field defaults to empty list."""
self.assertEqual(self.course.prerequisites, [])

def test_prerequisites_set_and_get(self):
"""Test that prerequisites can be set and retrieved."""
prerequisites = ["CS 1110", "CS 2100"]
self.course.prerequisites = prerequisites
self.course.save()
# Refresh from database
self.course.refresh_from_db()
self.assertEqual(self.course.prerequisites, prerequisites)