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

Feature/transcript parsing #701

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM --platform=linux/amd64 ubuntu:22.04
FROM --platform=linux/arm64 ubuntu:22.04
ARG IMAGE_NAME=pennlabs/courses-devcontainer
RUN apt-get update && apt-get install -y wget curl gcc python3-dev libpq-dev postgresql-client
RUN apt-get update && apt-get install -y wget curl gcc python3-dev libpq-dev postgresql-client
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.11"
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/alert/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def accept_webhook(request):

class RegistrationViewSet(AutoPrefetchViewSetMixin, viewsets.ModelViewSet):
"""
retrieve: Get one of the logged-in user's PCA registrations for the current semester, using
retrieve: Get one of the logged-in user's PCA registrations for the , using
the registration's ID. Note that if a registration with the specified ID exists, but that
registration is not at the head of its resubscribe chain (i.e. there is a more recent
registration which was created by resubscribing to the specified registration), the
Expand Down
57 changes: 55 additions & 2 deletions backend/degree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ class Rule(models.Model):
)

def __str__(self) -> str:
rules_str = ", ".join([str(rule) for rule in self.children.all()])
return (
f"{self.title}, q={self.q}, num={self.num}, cus={self.credits}, "
"parent={self.parent.title if self.parent else None}"
f"child rules: {rules_str}"
f"parent={self.parent.title if self.parent else None}"
)

@property
Expand Down Expand Up @@ -240,6 +242,48 @@ def evaluate(self, full_codes: Iterable[str]) -> bool:
return False
return True

# This is scuffed. Only difference with evaluate() is that this is on line 269,
# where we check if total_credits = 0 rather than total_credits < self.credits.
# Why do we do this? It makes it work, I guess. Please make something better.
def check_belongs(self, full_codes: Iterable[str]) -> bool:
"""
Check if provided courses all contribute to fulfilling a rule.
"""
if self.q:
assert not self.children.all().exists()
total_courses, total_credits = (
Course.objects.filter(self.get_q_object() or Q(), full_code__in=full_codes)
.aggregate(
total_courses=Count("id"),
total_credits=Coalesce(
Sum("credits"),
0,
output_field=DecimalField(max_digits=4, decimal_places=2),
),
)
.values()
)

assert self.num is not None or self.credits is not None
if self.num is not None and total_courses < self.num:
return False

if self.credits is not None and total_credits == 0:
return False
return True
else:
# assert self.children.all().exists()
count = 0
for child in self.children.all():
if not child.evaluate(full_codes):
if self.num is None:
return False
else:
count += 1
if self.num is not None and count < self.num:
return False
return True

def get_q_object(self) -> Q | None:
if not self.q:
return None
Expand Down Expand Up @@ -330,9 +374,18 @@ def evaluate_rules(self, rules: list[Rule]) -> tuple[set[Rule], set[DoubleCountR
full_codes = [fulfillment.full_code for fulfillment in rule_fulfillments]
if rule.evaluate(full_codes):
satisfied_rules.add(rule)

return (satisfied_rules, violated_dcrs)

def check_rules_already_satisfied(self, rules: set[Rule]) -> set[Rule]:
satisfied_rules = set()

for rule in rules:
f = Fulfillment.objects.all().filter(degree_plan=self).filter(rules=rule)
if (rule.num and len(f) >= rule.num) or (rule.credits and len(f) >= rule.credits):
satisfied_rules.add(rule)

return satisfied_rules

def copy(self, new_name: str) -> DegreePlan:
"""
Returns a new DegreePlan that is a copy of this DegreePlan.
Expand Down
31 changes: 31 additions & 0 deletions backend/degree/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import deque
from textwrap import dedent

from django.db.models import Q
Expand Down Expand Up @@ -139,6 +140,36 @@ def validate(self, data):
if degree_plan is None:
degree_plan = self.instance.degree_plan

# Find rules in degree plan with requirements identical to currently fulfilled ones
bfs_queue = deque()
for degree in degree_plan.degrees.all():
for rule_in_degree in degree.rules.all():
bfs_queue.append(rule_in_degree)

identical_rules = []
existing_fulfillment = Fulfillment.objects.filter(
degree_plan=degree_plan, full_code=full_code
).first()
existing_rules = (
existing_fulfillment.rules.all() if existing_fulfillment is not None else []
)
while len(bfs_queue):
curr_rule = bfs_queue.pop()
# this is a leaf rule
if curr_rule.q:
if (
any(rule.q == curr_rule.q and rule.id != curr_rule.id for rule in rules)
and curr_rule not in existing_rules
):
identical_rules.append(curr_rule)
else: # parent rule
bfs_queue.extend(curr_rule.children.all())

if hasattr(rules, 'extend'):
rules.extend(identical_rules)

data["rules"] = rules

# TODO: check that rules belong to this degree plan
for rule in rules:
# NOTE: we don't do any validation if the course doesn't exist in DB. In future,
Expand Down
19 changes: 18 additions & 1 deletion backend/degree/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from rest_framework.routers import DefaultRouter
from rest_framework_nested.routers import NestedDefaultRouter

from degree.views import DegreePlanViewset, DegreeViewset, DockedCourseViewset, FulfillmentViewSet
from degree.views import (
DegreePlanViewset,
DegreeViewset,
DockedCourseViewset,
FulfillmentViewSet,
SatisfiedRuleList,
OnboardFromTranscript
)


router = DefaultRouter(trailing_slash=False)
Expand All @@ -17,4 +24,14 @@
urlpatterns = [
path("", include(router.urls)),
path("", include(fulfillments_router.urls)),
path(
"onboard-from-transcript/<slug:degree_plan_id>/<path:all_codes>",
OnboardFromTranscript.as_view(),
name="onboard-from-transcript"
),
path(
"satisfied-rule-list/<slug:degree_plan_id>/<slug:full_code>/<slug:rule_id>",
SatisfiedRuleList.as_view(),
name="satisfied-rule-list",
),
]
Loading
Loading