From 067b53b7689e76fa16f2f2d22bacc0fee9af4564 Mon Sep 17 00:00:00 2001 From: Shiva Menta Date: Thu, 19 Sep 2024 21:33:15 -0400 Subject: [PATCH] lint --- .../commands/precompute_pcr_views.py | 47 +++++++++++++------ backend/review/views.py | 16 +++++-- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/backend/courses/management/commands/precompute_pcr_views.py b/backend/courses/management/commands/precompute_pcr_views.py index a9547ed2..7d43f30a 100644 --- a/backend/courses/management/commands/precompute_pcr_views.py +++ b/backend/courses/management/commands/precompute_pcr_views.py @@ -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) @@ -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.") @@ -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: @@ -60,7 +72,9 @@ 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 @@ -68,11 +82,16 @@ def precompute_courses(verbose=False, semester=None): 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" @@ -85,4 +104,4 @@ def handle(self, *args, **kwargs): # precompute_instructors(verbose=True) # precompute_departments(verbose=True) - precompute_courses(verbose=True, semester=kwargs["semester"]) \ No newline at end of file + precompute_courses(verbose=True, semester=kwargs["semester"]) diff --git a/backend/review/views.py b/backend/review/views.py index c8ffb14e..d121b79a 100644 --- a/backend/review/views.py +++ b/backend/review/views.py @@ -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 @@ -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( @@ -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 @@ -359,6 +360,7 @@ def manual_course_plots(semester, instructor_ids, course_code): }, } + @api_view(["GET"]) @schema( PcxAutoSchema( @@ -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: @@ -493,6 +496,7 @@ def manual_instructor_reviews(instructor_id): **get_average_and_recent_dict_single(inst), } + @api_view(["GET"]) @schema( PcxAutoSchema( @@ -581,6 +585,7 @@ def manual_department_reviews(department_code): "courses": courses, } + @api_view(["GET"]) @schema( PcxAutoSchema( @@ -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( @@ -681,7 +687,7 @@ def manual_instructor_for_course_reviews(semester, course_code, instructor_id): for section in all_sections ], } - + @api_view(["GET"]) @schema( @@ -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()