def next_recommended_question(request): logged_in_user = UserService.logged_in_user(request) question = QuestionService.next_recommended_question(logged_in_user) if question is None: return JsonResponse({"error": "You are not enrolled in the course for this question"}, status=403) return JsonResponse({"data": question.id})
def rate(request): if request.method != 'POST': return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) post_request = loads(request.body.decode("utf-8")) distractor_id = post_request.get("distractorID", None) difficulty = post_request.get("difficulty", None) quality = post_request.get("quality", None) if difficulty is None and quality is None: return JsonResponse({"error": "At least response.rating or response.quality must be specified"}, status=422) if difficulty is not None: if is_number(difficulty) is False or not 0 <= int(difficulty) <= 5: return JsonResponse({"error": "response.difficulty must be between 0 and 5 inclusive"}, status=422) difficulty = int(difficulty) if quality is not None: if is_number(quality) is False or not 0 <= int(quality) <= 5: return JsonResponse({"error": "response.quality must be between 0 and 5 inclusive"}, status=422) quality = int(quality) if distractor_id is None: return JsonResponse({"error": "Missing integer distractorID in request"}, status=422) user_ratings = {"difficulty": difficulty, "quality": quality} if QuestionService.rate_question(distractor_id, user_ratings, UserService.logged_in_user(request)) is False: return JsonResponse({"error": "Invalid distractorID"}, status=422) else: return HttpResponse(status=204)
def consent(request): user = UserService.logged_in_user(request) if request.method == 'POST': post_request = loads(request.body.decode("utf-8")) return JsonResponse(UserService.consent_service(user, post_request)) else: return JsonResponse(UserService.get_user_consent(user))
def id(request, id): logged_in_user = UserService.logged_in_user(request) question = QuestionService.get_question_by_id(logged_in_user, id) if question is None: return JsonResponse({"error": "You are not enrolled in the course for this question"}, status=403) return JsonResponse({"data": question})
def update(request): if request.method != 'POST': return JsonResponse({"error": "Must use POST to this endpoint"}, status=405) post_request = loads(request.body.decode("utf-8")) user = UserService.logged_in_user(request) return JsonResponse(UserService.update_course(user, post_request))
def get_reasons(request): if request.method != 'POST': return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) user = UserService.logged_in_user(request) return JsonResponse({"data": QuestionService.get_reason_list(user)})
def all_reports(request): user = UserService.logged_in_user(request) result = QuestionService.all_reports(user) if type(result) != list and result.get("error", None) is not None: return JsonResponse(result) else: return JsonResponse({"data": result})
def report(request): if request.method != 'POST': return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) post_request = loads(request.body.decode("utf-8")) user = UserService.logged_in_user(request) return JsonResponse({"data": QuestionService.report_question(user, post_request)})
def update(request, qid): val = validate_request(request) if not val[0]: return val[1] root_path, post_request = val response = QuestionService.update_question( post_request, root_path, UserService.logged_in_user(request), qid) return validate_response(response)
def get_all_notifications(request): user = UserService.logged_in_user(request) notifications = Notification.objects.filter(user=user).order_by("-id") data = [] for n in notifications: n.sent = True data.append(n.toJSON()) return JsonResponse({"data": data})
def leaderboard(request, sort_field, sort_order): logged_in_user = UserService.logged_in_user(request) user_roles = (str(x) for x in logged_in_user.roles.all()) limit = -1 if is_administrator(logged_in_user) else 20 if sort_order != "DESC" and sort_order != "ASC": sort_order = "DESC" leaderboard_scores = QuestionService.get_course_leaders(logged_in_user, False, sort_field, sort_order, limit) return JsonResponse({"data": leaderboard_scores})
def add(request): v = validate_request(request) if not v[0]: return v[1] root_path, post_request = v response = AuthorService.add_question( post_request, root_path, UserService.logged_in_user(request)) return validate_response(response)
def respond(request): if request.method != 'POST': return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) post_request = loads(request.body) distractor_id = post_request.get("distractorID", None) if distractor_id is None: return JsonResponse({"error": "Missing integer distractorID in request"}, status=422) if QuestionService.respond_to_question(distractor_id, UserService.logged_in_user(request)) is None: return JsonResponse({"error": "Invalid distractorID"}, status=422) else: return HttpResponse(status=204)
def image_update(request): if request.method != 'POST': return JsonResponse({"error": "Must use POST to this endpoint"}, status=405) post_request = loads(request.body.decode("utf-8")) new_image = post_request.get("image", None) if new_image is None: return JsonResponse({"error": "Missing image payload"}, status=405) course_user = UserService.logged_in_user(request) root_path = util.generate_static_path(request.get_host()) return JsonResponse( UserService.update_user_image(course_user.user, root_path, new_image))
def get_all_user_achievements(request): user = UserService.logged_in_user(request) achievements = Achievement.objects.all() data = [] for ach in achievements: result = engine.check_achievement(user=user, key=ach.key) data.append(result) if result["new"]: n = Notification(name=result["name"] + " Earned", description=result["description"], icon=result["icon"], user=user) n.save() return JsonResponse({"data": data})
def update_availability(request): # HTTP.POST is required for this. if request.method != "POST": return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) logged_in_user = UserService.logged_in_user(request) post_request = loads(request.body.decode("utf-8")) day = post_request.get("day", None) time = post_request.get("time", None) updated_availability = AvailabilityService.update_availability(logged_in_user, day, time) if updated_availability is None: return JsonResponse({"error": "Invalid day/time/availability combination"}, status=422) else: return JsonResponse({"data": updated_availability.toJSON()})
def respond(request): if request.method != 'POST': return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) post_request = loads(request.body.decode("utf-8")) distractor_id = post_request.get("distractorID", None) try: if distractor_id is None: return JsonResponse({"error": "Missing integer distractorID in request"}, status=422) if QuestionService.respond_to_question(distractor_id, UserService.logged_in_user(request)) is False: return JsonResponse({"error": "Invalid distractorID"}, status=422) else: return JsonResponse({}) except ValueError: return JsonResponse({"error": "You are not enrolled in the course for this course"}, status=403)
def update_role(request): # HTTP.POST is required for this. if request.method != "POST": return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) logged_in_user = UserService.logged_in_user(request) post_request = loads(request.body.decode("utf-8")) topic = post_request.get("topic", None) study_role = post_request.get("studyRole", None) updated_role = AvailabilityService.update_role(logged_in_user, topic, study_role) if updated_role is None: return JsonResponse({"error": "Invalid topic/studyRole/availableRole combination"}, status=422) else: return JsonResponse(updated_role.toJSON())
def search(request): if request.method != 'POST': return JsonResponse({ "error": "Must use POST to this endpoint" }, status=405) logged_in_user = UserService.logged_in_user(request) search_query = SearchService.SearchService(logged_in_user.course) post_request = loads(request.body.decode("utf-8")) sort_field = post_request.get("sortField", None) filter_field = post_request.get("filterField", None) filter_topics = post_request.get("filterTopics", None) query = post_request.get("query", None) sort_order = post_request.get("sortOrder", None) page_index = post_request.get("page", None) page_size = post_request.get("pageSize", 25) if sort_field is None and filter_field is None and query is None: search_result = search_query.execute() return page_response(search_result, page_index, page_size) if sort_field is not None: search_query.add_sort(sort_field, sort_order, logged_in_user) if filter_field is not None: search_query.add_filter(filter_field, logged_in_user) if filter_topics is not None: search_query.add_topic_filter(filter_topics) if query is not None: search_query.text_search(query) try: search_result = search_query.execute() return page_response(search_result, page_index, page_size) except TypeError as e: return JsonResponse({ "error": e })
def topics(request): logged_in_user = UserService.logged_in_user(request) course_topics = QuestionService.get_course_topics(logged_in_user.course) unique_topics = [x.toJSON() for x in course_topics] return JsonResponse({"data": unique_topics})
def user_roles(request): logged_in_user = UserService.logged_in_user(request) available_roles = [x.toJSON() for x in AvailabilityService.get_user_available_roles(logged_in_user)] return JsonResponse({"data": available_roles})
def has_consented(request): user = UserService.logged_in_user(request) return JsonResponse(UserService.has_consented_course(user))
def course_availability(request): course = UserService.logged_in_user(request).course counts = AvailabilityService.get_course_availability(course) # Get the count of each user in the course return JsonResponse({"data": counts})
def consented_stats(request): user = UserService.logged_in_user(request) return JsonResponse(UserService.get_consented_stats(user))
def courses(request): user = UserService.logged_in_user(request) return JsonResponse({"data": UserService.user_courses(user)})
def previous(request, qid): user = UserService.logged_in_user(request) return JsonResponse(QuestionService.previous_versions(user, qid))
def delete(request, qid): user = UserService.logged_in_user(request) return JsonResponse(QuestionService.delete_question(user, qid))
def competencies(request): logged_in_user = UserService.logged_in_user(request) user_competencies = UserService.user_competencies(logged_in_user) return JsonResponse(user_competencies, safe=False)
def competencies(request): logged_in_user = UserService.logged_in_user(request) user_competencies = UserService.user_competencies(logged_in_user) return JsonResponse({"data": user_competencies})
def aggregate(request, compare_type): logged_in_user = UserService.logged_in_user(request) aggregate_competencies = UserService.aggregate_competencies(logged_in_user, compare_type) return JsonResponse({"data": aggregate_competencies})