Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
shiva-menta committed Sep 20, 2024
1 parent 4ec46b8 commit 067b53b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
47 changes: 33 additions & 14 deletions backend/courses/management/commands/precompute_pcr_views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import json
import logging
import os

from django.core.management.base import BaseCommand
from tqdm import tqdm
import json
import os

from courses.util import get_current_semester
from review.views import manual_department_reviews, manual_instructor_reviews, manual_instructor_for_course_reviews, manual_course_reviews, manual_course_plots
from courses.models import Course, Department, Instructor
from review.views import (
manual_course_plots,
manual_course_reviews,
manual_department_reviews,
manual_instructor_for_course_reviews,
manual_instructor_reviews,
)


def save_object(id, data):
"""
Common save object method to act as interface for any precomputation – either as S3 objects or storing directly in PostgreSQL.
Common save object method to act as interface for any precomputation
– either as S3 objects or storing directly in PostgreSQL.
"""
with open(os.path.expanduser(f"~/Desktop/test_data/{id}.json"), "w") as f:
json.dump(data, f, indent=4)


def precompute_instructors(verbose=False):
if verbose:
print("Precomputing instructor view data.")

for instructor in tqdm(Instructor.objects.all()):
instructor_id = instructor.id
reviews = manual_instructor_reviews(instructor_id)
Expand All @@ -29,12 +37,15 @@ def precompute_instructors(verbose=False):

review_json = {"summary": reviews, "sections": {}}
for course_code in reviews["courses"]:
section_reviews = manual_instructor_for_course_reviews(semester=None, course_code=course_code, instructor_id=instructor_id)
section_reviews = manual_instructor_for_course_reviews(
semester=None, course_code=course_code, instructor_id=instructor_id
)
if section_reviews:
review_json["sections"][course_code] = section_reviews

save_object(instructor_id, review_json)


def precompute_departments(verbose=False):
if verbose:
print("Precomputing department view data.")
Expand All @@ -45,13 +56,14 @@ def precompute_departments(verbose=False):
if reviews:
save_object(department_code, reviews)


def precompute_courses(verbose=False, semester=None):
if verbose:
print("Precomputing course view data.")

for course in tqdm(Course.objects.all()):
course_code = course.full_code

# Fetch Course Summary
reviews = manual_course_reviews(course_code, None)
if not reviews:
Expand All @@ -60,19 +72,26 @@ def precompute_courses(verbose=False, semester=None):

# Fetch Section Data
for instructor_id in reviews["instructors"]:
instructor_reviews = manual_instructor_for_course_reviews(semester=None, course_code=course_code, instructor_id=instructor_id)
instructor_reviews = manual_instructor_for_course_reviews(
semester=None, course_code=course_code, instructor_id=instructor_id
)
if instructor_reviews:
review_json["sections"][instructor_id] = instructor_reviews

# Fetch Course Plots
plots = manual_course_plots(semester, None, course_code)
review_json["plots"] = plots
if "current_add_drop_period" in review_json["plots"]:
review_json["plots"]["current_add_drop_period"]["start"] = str(review_json["plots"]["current_add_drop_period"]["start"])
review_json["plots"]["current_add_drop_period"]["end"] = str(review_json["plots"]["current_add_drop_period"]["end"])
review_json["plots"]["current_add_drop_period"]["start"] = str(
review_json["plots"]["current_add_drop_period"]["start"]
)
review_json["plots"]["current_add_drop_period"]["end"] = str(
review_json["plots"]["current_add_drop_period"]["end"]
)

save_object(course_code, review_json)


class Command(BaseCommand):
help = "Precompute all data needed for Penn Course Review and store in S3"

Expand All @@ -85,4 +104,4 @@ def handle(self, *args, **kwargs):

# precompute_instructors(verbose=True)
# precompute_departments(verbose=True)
precompute_courses(verbose=True, semester=kwargs["semester"])
precompute_courses(verbose=True, semester=kwargs["semester"])
16 changes: 12 additions & 4 deletions backend/review/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from dateutil.tz import gettz
from django.db.models import F, Max, OuterRef, Q, Subquery, Value
from django.http import Http404
from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view, permission_classes, schema
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand Down Expand Up @@ -90,6 +89,7 @@ def extra_metrics_section_filters_pcr(current_semester=None):
(~Q(course__title="") | ~Q(course__description="")) & ~Q(activity="REC") & ~Q(status="X")
)


@api_view(["GET"])
@schema(
PcxAutoSchema(
Expand Down Expand Up @@ -276,6 +276,7 @@ def manual_course_reviews(course_code, request_semester):
**get_average_and_recent_dict_single(course),
}


def manual_course_plots(semester, instructor_ids, course_code):
try:
semester = semester
Expand Down Expand Up @@ -359,6 +360,7 @@ def manual_course_plots(semester, instructor_ids, course_code):
},
}


@api_view(["GET"])
@schema(
PcxAutoSchema(
Expand Down Expand Up @@ -430,6 +432,7 @@ def check_instructor_id(instructor_id):
"difficulty",
]


def manual_instructor_reviews(instructor_id):
instructor = Instructor.objects.get(id=instructor_id)
if not instructor:
Expand Down Expand Up @@ -493,6 +496,7 @@ def manual_instructor_reviews(instructor_id):
**get_average_and_recent_dict_single(inst),
}


@api_view(["GET"])
@schema(
PcxAutoSchema(
Expand Down Expand Up @@ -581,6 +585,7 @@ def manual_department_reviews(department_code):
"courses": courses,
}


@api_view(["GET"])
@schema(
PcxAutoSchema(
Expand Down Expand Up @@ -615,13 +620,14 @@ def department_reviews(request, department_code):

return Response(results)


def manual_instructor_for_course_reviews(semester, course_code, instructor_id):
try:
course = most_recent_course_from_code(course_code, semester)
course = course.topic.most_recent
except Course.DoesNotExist:
return None

instructor = Instructor.objects.get(id=instructor_id)
reviews = review_averages(
Review.objects.filter(
Expand Down Expand Up @@ -681,7 +687,7 @@ def manual_instructor_for_course_reviews(semester, course_code, instructor_id):
for section in all_sections
],
}


@api_view(["GET"])
@schema(
Expand Down Expand Up @@ -726,7 +732,9 @@ def instructor_for_course_reviews(request, course_code, instructor_id):
Get the review history of an instructor teaching a course.
"""
check_instructor_id(instructor_id)
reviews = manual_instructor_for_course_reviews(request.GET.get("semester"), course_code, instructor_id)
reviews = manual_instructor_for_course_reviews(
request.GET.get("semester"), course_code, instructor_id
)
if not reviews:
raise Http404()

Expand Down

0 comments on commit 067b53b

Please sign in to comment.