Skip to content

Commit

Permalink
add endpoint to list courses that fulfill a rule
Browse files Browse the repository at this point in the history
  • Loading branch information
AaDalal committed Dec 14, 2023
1 parent 3606b77 commit c724e4f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion backend/degree/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path, include
from rest_framework import routers
from degree.views import DegreeDetail, DegreeList, UserDegreePlanViewset
from degree.views import DegreeDetail, DegreeList, UserDegreePlanViewset, rule_courses

router = routers.DefaultRouter()

Expand All @@ -13,5 +13,6 @@
DegreeDetail.as_view(),
name="degree-detail",
),
path("courses/<rule_id>", rule_courses, name="rule-courses"),
path("", include(router.urls)),
]
25 changes: 18 additions & 7 deletions backend/degree/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes
from courses.serializers import CourseListSerializer

from courses.models import Course
from degree.models import Degree, Rule, DegreePlan
from degree.utils.model_utils import q_object_parser
from degree.serializers import (
DegreePlanDetailSerializer,
DegreeListSerializer,
Expand Down Expand Up @@ -77,15 +79,24 @@ def get_serializer_context(self):
return context


@api_view(["POST"])
@permission_classes([IsAuthenticated])
def check_degree_plan(request, **kwargs):
@api_view(["GET"])
def rule_courses(request, rule_id: int):
"""
Search for courses that fulfill a given rule.
"""
try:
degree_plan = Degree.objects.get(id=kwargs["degree_plan_id"])
rule = Rule.objects.get(id=rule_id)
except ObjectDoesNotExist:
return Response(
{"error": "Degree plan does not exist."},
data={"error": f"Rule with id {rule_id} does not exist."},
status=status.HTTP_404_NOT_FOUND,
)

return Response(degree_plan.check_degree(), 200)

q = rule.get_q_object()
if q is None:
return Response(
data={"error": f"Rule with id {rule_id} has no query object."},
status=status.HTTP_400_BAD_REQUEST,
)
courses = Course.objects.filter(q)
return Response(CourseListSerializer(courses, many=True).data)
3 changes: 3 additions & 0 deletions backend/tests/degree/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ def test_unparseable_value(self):
def test_idempotency(self):
self.assertParsedEqual(q_object_parser.parse(repr(Q(key="\"'value"))))

def test_empty_string(self):
with self.assertRaises(LarkError):
q_object_parser.parse("")

class RuleEvaluationTest(TestCase):
def setUp(self):
Expand Down

0 comments on commit c724e4f

Please sign in to comment.