Skip to content

Commit

Permalink
Fix some parser bugs with db object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Clue88 committed Nov 3, 2023
1 parent 85ca83e commit 4072c93
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
5 changes: 5 additions & 0 deletions backend/degree/management/commands/fetch_degreeplans.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db import transaction

from courses.util import get_current_semester
from degree.models import program_code_to_name
from degree.utils.degreeworks_client import DegreeworksClient
from degree.utils.parse_degreeworks import parse_degreeworks

Expand Down Expand Up @@ -75,5 +76,9 @@ def handle(self, *args, **kwargs):
for program in client.get_programs(year=year):
for degree_plan in client.degree_plans_of(program, year=year):
with transaction.atomic():
if degree_plan.program not in program_code_to_name:
continue
degree_plan.save()
print(f"Saving degree plan {degree_plan}...")
rules = parse_degreeworks(client.audit(degree_plan), degree_plan)
pprint(rules)
23 changes: 12 additions & 11 deletions backend/degree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

from degree.utils.model_utils import q_object_parser

program_choices = [
("EU_BSE", "Engineering BSE"),
("EU_BAS", "Engineering BAS"),
("AU_BA", "College BA"),
("WU_BS", "Wharton BS"),
]

program_code_to_name = dict(program_choices)


class DegreePlan(models.Model):
"""
Expand All @@ -15,12 +24,7 @@ class DegreePlan(models.Model):

program = models.CharField(
max_length=10,
choices=[
("EU_BSE", "Engineering BSE"),
("EU_BAS", "Engineering BAS"),
("AU_BA", "College BA"),
("WU_BS", "Wharton BS"),
],
choices=program_choices,
help_text=dedent(
"""
The program code for this degree plan, e.g., EU_BSE
Expand Down Expand Up @@ -152,14 +156,11 @@ class Meta:
]

def __str__(self) -> str:
return (
f"{self.q}, num={self.num_courses}, cus={self.credits}, degree_plan={self.degree_plan}"
)
return f"{self.title}, q={self.q}, num={self.num_courses}, cus={self.credits}, degree_plan={self.degree_plan}, parent={self.parent.title if self.parent else None}"

def evaluate(self, full_codes: Iterable[str]) -> bool:
"""
Check if this rule is fulfilled by the provided
courses.
Check if this rule is fulfilled by the provided courses.
"""
if self.q:
assert not self.children.all().exists()
Expand Down
18 changes: 15 additions & 3 deletions backend/degree/utils/degreeworks_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def audit(self, degree_plan: DegreePlan, timeout=30) -> dict:
"isKeepCurriculum": False,
"school": "UG",
"degree": degree_plan.degree,
"catalogYear": degree_plan.year,
"catalogYear": str(degree_plan.year),
"goals": [
{"code": "MAJOR", "value": degree_plan.major},
{"code": "CONC", "value": degree_plan.concentration},
Expand Down Expand Up @@ -1844,13 +1844,25 @@ def degree_plans_of(
concentrations = res.json()[1]["goals"][1]["choices"]
if len(concentrations) == 0:
degree_plans.append(
DegreePlan(program_code, degree_code, major_code, None, 2023)
DegreePlan(
program=program_code,
degree=degree_code,
major=major_code,
concentration=None,
year=year,
)
)
continue
for concentration in concentrations:
concentration_code = concentration["key"]
degree_plans.append(
DegreePlan(program_code, degree_code, major_code, concentration_code, 2023)
DegreePlan(
program=program_code,
degree=degree_code,
major=major_code,
concentration=concentration_code,
year=year,
)
)

return degree_plans
Expand Down
14 changes: 8 additions & 6 deletions backend/degree/utils/parse_degreeworks.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def parse_rulearray(
case "Group": # TODO: this is nested

parse_rulearray(rule_json["ruleArray"], degree_plan, rules, parent=this_rule)
this_rule.num_courses = (rule_req["numberOfGroups"],)
this_rule.num_courses = int(rule_req["numberOfGroups"])
case "Complete" | "Incomplete":
assert "ifElsePart" in rule_json # this is a nested requirement
continue # do nothing
Expand Down Expand Up @@ -263,8 +263,10 @@ def parse_degreeworks(json: dict, degree_plan: DegreePlan) -> list[Rule]:
# TODO: Should associate each Rule here with this Requirement
rules.append(degree_req)
parse_rulearray(requirement["ruleArray"], degree_plan, rules, parent=degree_req)
degree_plan.save()
return Rule.bulk_create(rules)

for rule in rules:
rule.save()
return rules


if __name__ == "__main__":
Expand All @@ -273,12 +275,12 @@ def parse_degreeworks(json: dict, degree_plan: DegreePlan) -> list[Rule]:
from os import getenv

pennid = getenv("PENN_ID")
assert pennid is not None
auth_token = getenv("X_AUTH_TOKEN")
assert pennid is not None
refresh_token = getenv("REFRESH_TOKEN")
assert refresh_token is not None
name = getenv("NAME")
assert pennid is not None
assert auth_token is not None
assert refresh_token is not None
assert name is not None

client = DegreeworksClient(
Expand Down

0 comments on commit 4072c93

Please sign in to comment.