예제 #1
0
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})
예제 #2
0
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)
예제 #3
0
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))
예제 #4
0
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})
예제 #5
0
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))
예제 #6
0
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)})
예제 #7
0
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})
예제 #8
0
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)})
예제 #9
0
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)
예제 #10
0
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})
예제 #11
0
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})
예제 #12
0
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)
예제 #13
0
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)
예제 #14
0
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))
예제 #15
0
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})
예제 #16
0
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()})
예제 #17
0
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)
예제 #18
0
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())
예제 #19
0
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
        })
예제 #20
0
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})
예제 #21
0
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})
예제 #22
0
def has_consented(request):
    user = UserService.logged_in_user(request)
    return JsonResponse(UserService.has_consented_course(user))
예제 #23
0
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})
예제 #24
0
def consented_stats(request):
    user = UserService.logged_in_user(request)
    return JsonResponse(UserService.get_consented_stats(user))
예제 #25
0
def courses(request):
    user = UserService.logged_in_user(request)
    return JsonResponse({"data": UserService.user_courses(user)})
예제 #26
0
def previous(request, qid):
    user = UserService.logged_in_user(request)
    return JsonResponse(QuestionService.previous_versions(user, qid))
예제 #27
0
def delete(request, qid):
    user = UserService.logged_in_user(request)
    return JsonResponse(QuestionService.delete_question(user, qid))
예제 #28
0
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)
예제 #29
0
def competencies(request):
    logged_in_user = UserService.logged_in_user(request)
    user_competencies = UserService.user_competencies(logged_in_user)
    return JsonResponse({"data": user_competencies})
예제 #30
0
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})