Exemplo n.º 1
0
def signup_through_link(request, group_hash):

    # Call logout to ensure a clean session
    logout(request)

    group = StudentGroup.get(group_hash)

    if group is None:
        return response_404(
            request,
            msg=_("The group couldn't be found. Bear in mind that the URL "
                  "is case-sensitive."),
        )

    if request.method == "POST":

        form = EmailForm(request.POST)
        if not form.is_valid():
            return response_400(
                request, msg=_("There was a problem with the values sent."))

        student, created = Student.get_or_create(form.cleaned_data["email"])

        if student is None:
            return response_400(
                request,
                msg=_("There already exists a user with this username. Try a "
                      "different email address."),
            )

        if created:
            student.join_group(group, mail_type="confirmation")
        else:
            student.join_group(group, mail_type="new_group")

        return TemplateResponse(
            request,
            "registration/sign_up_student_done.html",
            context={
                "student": student,
                "group": group
            },
        )

    form = EmailForm()
    tos = Tos.objects.filter(role="student").latest("created").text

    return TemplateResponse(
        request,
        "registration/sign_up_student.html",
        context={
            "form": form,
            "group": group,
            "tos": tos
        },
    )
Exemplo n.º 2
0
def test_response_404__default_message(rf, caplog):
    logger = logging.getLogger("test")
    req = rf.get("/test")
    req.user = AnonymousUser()

    resp = response_404(req,
                        msg="test1",
                        log=logger.warning,
                        use_template=True)

    assert isinstance(resp, TemplateResponse)
    assert resp.status_code == 404
    assert len(caplog.records) == 1
    assert (caplog.records[0].message ==
            "404 error for user AnonymousUser on path /test.")
Exemplo n.º 3
0
def test_response_404(rf, caplog):
    logger = logging.getLogger("test")
    req = rf.get("/test")

    resp = response_404(
        req,
        msg="test1",
        logger_msg="test2",
        log=logger.warning,
        use_template=True,
    )

    assert isinstance(resp, TemplateResponse)
    assert resp.status_code == 404
    assert len(caplog.records) == 1
    assert caplog.records[0].message == "test2"
Exemplo n.º 4
0
def live(request, token, assignment_hash):

    # Call logout to ensure a clean session
    logout(request)

    # Login through token
    user, __ = authenticate_student(request, token)
    if isinstance(user, HttpResponse):
        return user
    login(request, user)

    # Register access type
    request.session["LTI"] = False

    # Get assignment for this token and current question
    group_assignment = StudentGroupAssignment.get(assignment_hash)
    if group_assignment is None:
        return response_404(
            request,
            msg=_(
                "This url doesn't correspond to any assignment. "
                "It may have been deleted by your teacher."
            ),
            logger_msg=(
                "Access to live was tried for unknown assignment with hash "
                "{} by user {}.".format(assignment_hash, user.pk)
            ),
            log=logger.warning,
        )

    group = group_assignment.group
    if group.student_id_needed:
        group_membership = StudentGroupMembership.objects.get(
            student=user.student, group=group
        )
        if not group_membership.student_school_id:
            return HttpResponseRedirect(
                reverse("student-page")
                + "?group-student-id-needed="
                + group.name
            )

    student_assignment = StudentAssignment.objects.get(
        student=user.student, group_assignment=group_assignment
    )

    # Register assignment
    request.session["assignment"] = assignment_hash

    # Register quality
    if group_assignment.quality:
        request.session["quality"] = group_assignment.quality.pk
    elif group_assignment.group.quality:
        request.session["quality"] = group_assignment.group.quality.pk
    elif (
        group_assignment.group.teacher.exists()
        and group_assignment.group.teacher.first().quality
    ):
        request.session[
            "quality"
        ] = group_assignment.group.teacher.first().quality.pk

    assignment = student_assignment.group_assignment
    current_question = student_assignment.get_current_question()
    has_expired = student_assignment.group_assignment.expired

    if has_expired or current_question is None:
        return HttpResponseRedirect(reverse("finish-assignment"))

    questions = assignment.questions
    idx = questions.index(current_question)

    request.session["assignment_first"] = idx == 0
    request.session["assignment_last"] = idx == len(questions) - 1
    request.session["assignment_expired"] = has_expired

    # Redirect to view
    return HttpResponseRedirect(
        reverse(
            "question",
            kwargs={
                "assignment_id": assignment.assignment.pk,
                "question_id": current_question.id,
            },
        )
    )