Skip to content

Commit

Permalink
Standardized errors with company 1 (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik-McKelvey authored Aug 4, 2023
1 parent a135246 commit d8e6ab7
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions prediction_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,62 @@
import json
import pandas as pd

loc_missing_template = {
"loc": [],
"msg": "field required",
"type": "value_error.missing"
}

loc_term_template = {
"loc": ["body", "term"],
"msg":"value is not a valid enumeration member; permitted: 'spring', 'summer', 'fall'",
"type":"type_error.enum",
"ctx":{"enum_values":["spring","summer","fall"]}
}

def predict(request):
# Check that request is a POST request
if request.method != 'POST':
return HttpResponse("This is a POST endpoint, silly", status=405)

# Check that year and term are correctly provided
body = request.body.decode('utf-8')
data = json.loads(body)
try:
data = json.loads(body)
except:
return HttpResponse("Error with JSON body", status=422)

error_template = {
"detail": []
}

year = str(data.get('year'))
year = data.get('year')
term = utils.reformat_term(data.get('term'))
if not year:
return HttpResponse("year is required", status=400)
loc = loc_missing_template.copy()
loc["loc"] = ["body", "year"]
error_template["detail"].append(loc)
year = str(year)
if not term:
return HttpResponse("term is required", status=400)
if not term in ["fall", "spring", "summer"]:
return HttpResponse("term must be fall, spring, or summer", status=400)

"""
# Get historic schedules from backend
historic_schedules = api.request_historic_schedules()
"""
loc = loc_missing_template.copy()
loc["loc"] = ["body", "term"]
error_template["detail"].append(loc)
elif not term in ["fall", "spring", "summer"]:
error_template["detail"].append(loc_term_template)

with open('data/client_data/schedules2.json', 'r', encoding='utf-8') as fh:
historic_schedules = json.load(fh)

# Get courses from request
courses = data.get('courses')
if not courses:
return HttpResponse("courses to predict are required", status=400)
## Get courses from backend
## courses = api.request_courses()

loc = loc_missing_template.copy()
loc["loc"] = ["body", "courses"]
error_template["detail"].append(loc)

if (len(error_template["detail"]) > 0):
return JsonResponse(error_template, status=422)

# Reformat courses for prediction
for course in courses:
course["terms_offered"] = [utils.reformat_term(term) for term in course["terms_offered"]]
Expand All @@ -50,21 +73,11 @@ def predict(request):
course_names = [course["Course"] for course in formatted_historic_schedules]
courses = utils.filter_courses_by_name(courses, course_names)

"""# Perform prediction
predictions = enrollment_predictions(historic_schedules, courses)
"""

#try:
historic_schedules = pd.DataFrame(historic_schedules)
courses_df = pd.DataFrame(courses)
predictions = enrollment_predictions(historic_schedules, courses_df)
# Reformate predictions

predictions = utils.reformat_predictions(courses, predictions)

"""
except Exception as e:
return HttpResponse(f"Error calculating course predictions {e}", status=400)
"""

try:
return JsonResponse(predictions, status=200, safe=False)
Expand Down

0 comments on commit d8e6ab7

Please sign in to comment.