def update_consent_form(user, consent_form_data, host):
    if not is_administrator(user):
        return {"error": "User does not have permission in this context"}

    payload = consent_form_data.get("payload", None)
    if payload is None:
        return {"error": "No consent form provided"}

    consent_text = payload.get("content", None)
    if not verify_content(consent_text):
        return {"error": "Invalid consent text provided"}
    payloads = payload.get("payloads", None)

    root_path = generate_static_path(host)
    try:
        with transaction.atomic():
            consent_form = ConsentForm.objects.create(content=consent_text,
                                                      author=user)
        if payloads:
            extract_image_from_html(user.course.id, consent_form, payloads,
                                    "c", root_path)

        return {"data": "Consent form saved"}
    except IntegrityError as e:
        return {"state": "Error", "error": str(e)}
    except Exception as e:
        return {"state": "Error", "error": str(e)}
def get_all_stats(user):
    if not util.is_administrator(user):
        return {"error": "User is not authorized"}

    leaderboard = QuestionService.create_leaderboard(user, False,
                                                     ["lastName", "firstName"],
                                                     "ASC")
    for person in leaderboard:
        person.pop("rank", None)
    return {"data": leaderboard}
Beispiel #3
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})
def has_consented_course(user):
    if util.is_administrator(user):
        return {"data": True}
    form = get_form(user)
    if form:
        consent = Consent.objects.filter(form=form, user=user)
        if consent:
            return {"data": True}
        else:
            return {"data": False}
    else:
        return {"error": "No consent form for this course"}
def get_question_by_id(user, id):
    try:
        question = Question.objects.get(pk=id)
        if question.author.course != user.course:
            return None
        q_JSON = question.toJSON()
        if util.is_administrator(user) or question.author == user:
            q_JSON["canEdit"] = True
        else:
            q_JSON["canEdit"] = False
        return q_JSON
    except Question.DoesNotExist:
        return None
def get_user_consent(user):
    if is_administrator(user):
        return {"data": True}

    form = get_form(user)
    if form:
        user_consent = Consent.objects.filter(
            user=user, form=form).order_by("-created_at").first()
        return {
            "data": user_consent.response if user_consent is not None else None
        }
    else:
        return {"error": "No consent form for course"}
def update_question(post_request, root_path, user, qid):
    try:
        question = Question.objects.get(id=qid)
        if (util.is_administrator(user) or user == question.author):
            with transaction.atomic():
                deleted = make_deleted_question(question)
                deleted.topics.set(question.topics.all())
                return AuthorService.add_question(post_request, root_path,
                                                  user, question)
        else:
            return {"state": "Error", "error": "Permission Denied"}
    except Question.DoesNotExist:
        return {"state": "Error", "error": "Question does not exist"}
    except IntegrityError as e:
        return {"state": "Error", "error": str(e)}
def delete_question(user, qid):
    try:
        question = Question.objects.get(id=qid)
        if (util.is_administrator(user) or user == question.author):
            with transaction.atomic():
                deleted = make_deleted_question(question)
                deleted.topics.set(question.topics.all())
                question.delete()
            return {"data": {}}
        else:
            return {"error": "Permission Denied"}
    except Question.DoesNotExist:
        return {"error": "Question does not exist"}
    except IntegrityError as e:
        return {"error": str(e)}
def get_course_leaders(user, has_consented, sort_field, sort_order, limit=25):
    if not type(sort_field) == list:
        sort_field = [sort_field]
    leaderboard_users = create_leaderboard(user, has_consented, sort_field,
                                           sort_order)

    found_user = [x for x in leaderboard_users if x["id"] == user.id]
    if found_user:
        found_user = found_user[0]

    for l_user in leaderboard_users:
        if found_user is not l_user:
            l_user.pop("lastName", None)
            l_user.pop("id", None)

    if limit == -1:
        return leaderboard_users

    leaderboard = leaderboard_users[0:limit]
    if not util.is_administrator(user) and found_user not in leaderboard:
        leaderboard.append(found_user)

    return leaderboard
def update_course(course_user, new_data):
    if not course_user or not new_data:
        return {"error": "Course user and update data must be provided"}
    course_information = new_data.get("course", {})
    topics = new_data.get("topics", None)
    if not topics:
        return {"error": "Course must have topics"}
    for t in topics:
        if not t.get("name", None):
            return {
                "error":
                "Topics must be JSON representations of Topics with, at minimum, attribute 'name'"
            }

    course_id = course_information.get("courseID", None)
    if course_id is None:
        return {"error": "Missing course code"}

    if course_user.course.course_id != course_id:
        return {"error": "Course not found"}

    if not util.is_administrator(course_user):
        return {
            "error":
            "User does not have administrative permission for current context"
        }

    code = course_information.get("courseCode", None)
    name = course_information.get("courseName", None)
    sem = course_information.get("courseSem", None)
    start = course_information.get("start", None)
    end = course_information.get("end", None)
    available = course_information.get("available", None)

    course = course_user.course
    if code is not None:
        course.course_code = code
    if name is not None:
        course.course_name = name
    if sem is not None:
        course.course_sem = sem
    if start is not None:
        if str(start).isdigit():
            course.start = datetime.fromtimestamp(int(start), timezone.utc)
        else:
            return {
                "error": "Given start timestamp is not valid: " + str(start)
            }
    if end is not None:
        if str(end).isdigit():
            course.end = datetime.fromtimestamp(int(end), timezone.utc)
        else:
            return {"error": "Given end timestamp is not valid: " + str(end)}
    # Important to check for None, since available is boolean
    if available is not None:
        course.available = available

    course.save()
    new_topics = []
    # Insert topics
    for topic in topics:
        topic_id = topic.get("id", None)
        topic_name = topic.get("name", None)
        existing_topic = None
        try:
            existing_topic = Topic.objects.get(name=topic_name,
                                               id__in=course.topic_set.all())
        except Topic.DoesNotExist:
            pass

        if not existing_topic:
            (new_topic, created) = Topic.objects.get_or_create(name=topic_name)
            new_topic.course.add(course)
            new_topics.append(new_topic)
        else:
            new_topics.append(existing_topic)
    course.topic_set.clear()
    course.topic_set.set(new_topics)

    return {"data": course_user.toJSON()}