def course_by_code(request, course_code_in): form = CourseForm({"course": course_code_in}) if form.is_valid(): course_code = form.cleaned_data["course"].upper() CourseCodes.objects.get_or_create(code=course_code) # Save course code for later try: courses = Course.objects.filter(course_code=course_code) return HttpResponse( JSONRenderer().render(CourseSerializer(courses[0]).data), content_type="application/json" ) except IndexError as e: pass courses_list_id, course_id_status = get_course_id(course_code) if course_id_status: # If timeedit gives us some objects courses = [Course.objects.update_or_create(**get_course_info(course_id)) for course_id in courses_list_id] return HttpResponse( JSONRenderer().render(CourseSerializer(courses[0][0]).data), content_type="application/json" ) else: return exceptions.can_find_course(course_code_in) else: return exceptions.invalid_search_format()
def post(self, request, *args, **kwargs): # Create new form and pass in post info. form = CourseForm(request.POST) # If form is valid if form.is_valid(): # variable with cleaned data from the form course_post = form.cleaned_data["course"].upper() # Set seesion if "course" in request.session.keys(): if course_post not in request.session["course"]: request.session["course"].append(course_post) else: request.session["course"] = [] # Try to get if course from database try: # If there is only one course for the course code in database course = Course.objects.get(course_code=course_post) course_events = get_course_events(course.semester, course.year, course.course_reg) return render(request, "timeedit/index.html", {"course": course, "events": course_events, "form": form}) # If there is multiple course objects in the database except MultipleObjectsReturned as e: courses = Course.objects.filter(course_code=course_post) course_events_list = [] for i in courses: course_events_list.append(get_course_events(i.semester, i.year, i.course_reg)) return render( request, "timeedit/index.html", {"course": courses[0], "events": max(course_events_list), "form": form}, ) # If course dont exist in the database, request it except Course.DoesNotExist as e: try: courses_list_id = get_course_id(course_post) # The stupid id thats is assigned to every course events_list = [] courses_list = [] for i in courses_list_id: course = Course(**get_course_info(i)) course.save() courses_list.append(course) first_course = courses_list[0] for i in courses_list: events_list.append(get_course_events(i.semester, i.year, i.course_reg)) return render( request, "timeedit/index.html", {"course": first_course, "events": max(events_list), "form": form}, ) except IndexError as e: print(e) return render( request, "timeedit/index.html", {"form": form, "message": "Course does not exist, or is not active!"}, ) # If form in not valid else: # Logs the post if it does not pass the form validation invalid_post = form.data["course"] print(invalid_post) # Renders an error response return render(request, "timeedit/index.html", {"form": form, "message": "Invalid search format!"})