def events_by_code(request, course_code_in): form = CourseForm({"course": course_code_in}) if form.is_valid(): course_code = form.cleaned_data["course"].upper() try: courses = Course.objects.filter(course_code=course_code) # return HttpResponse(JSONRenderer().render(EventSerializer(max([events for events in getCourseEvents(courses)])).data), content_type='application/json') course_events_list = [] for i in courses: course_events_list.append( get_course_events(i.semester, i.year, i.course_reg, course_code, i.name_en, i.name_sv) ) return HttpResponse(json.dumps(max(course_events_list)), content_type="application/json") except IndexError as e: pass return exceptions.can_find_course()
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!"})