Пример #1
0
def student_id():
    github = github_username()
    if request.method == "GET":
        return render_template("onboarding/student_id.html", github=github)
    try:
        mailer_job = None
        github_job = None
        with DbCursor() as c:
            user = get_user_by_github(c, github)
            if user:
                return redirect(url_for("dashboard.index"))
            student_id = request.form.get("f_student_id")
            if not student_id:
                fail_validation("Student ID is required")
            user = get_user_by_student_id(c, student_id)
            if not user:
                fail_validation("Student not found with that student ID")
            user_id, name, _, login, old_github, email = user
            if old_github:
                fail_validation(
                    "Another GitHub account has been associated with that student "
                    "ID already.")
            if not name:
                fail_validation(
                    "There is no name associated with this account. (Contact your TA?)"
                )
            if not login:
                fail_validation(
                    "There is no login associated with this account. (Contact your "
                    "TA?)")
            if not email:
                fail_validation(
                    "There is no email associated with this account. (Contact your "
                    "TA?)")
            c.execute('''UPDATE users SET github = ? WHERE sid = ?''',
                      [github, student_id])
            if not config.github_read_only_mode:
                github_job = repomanager_queue.create(c, "assign_repo",
                                                      (login, [github]))
            if config.mailer_enabled:
                if config.inst_account_enabled:
                    attachments = [("pdf", get_inst_account_form_path(login))]
                else:
                    attachments = []
                email_payload = create_email(
                    "onboarding_confirm",
                    email,
                    "%s Autograder Registration" % config.course_number,
                    _attachments=attachments,
                    name=name,
                    login=login,
                    inst_account_enabled=config.inst_account_enabled)
                mailer_job = mailer_queue.create(c, "send", email_payload)
        if config.mailer_enabled and mailer_job:
            mailer_queue.enqueue(mailer_job)
        if not config.github_read_only_mode and github_job:
            repomanager_queue.enqueue(github_job)
        return redirect(url_for(_get_next_step("onboarding.student_id")))
    except ValidationError as e:
        return redirect_with_error(url_for("onboarding.student_id"), e)
Пример #2
0
def group_create():
    if not config.groups_enabled:
        abort(404)
    try:
        githubs = request.form.getlist("f_github")
        mailer_jobs = []
        with DbCursor() as c:
            student = _get_student(c)
            inviter_user_id, inviter_name, _, _, inviter_github, _ = student
            grouplimit = get_grouplimit(c, inviter_user_id)
            if grouplimit < 1:
                fail_validation("You are in too many groups already")
            invitees = []
            invitation_user_ids = set()
            for github in githubs:
                if not github:
                    continue
                invitee = get_user_by_github(c, github)
                if invitee is None:
                    fail_validation("GitHub username not found: %s" % github)
                invitee_id, _, _, _, _, _ = invitee
                if invitee_id == inviter_user_id:
                    continue
                if invitee_id in invitation_user_ids:
                    continue
                invitation_user_ids.add(invitee_id)
                invitees.append(invitee)
            if not config.group_min_size <= len(invitation_user_ids) + 1 <= config.group_max_size:
                fail_validation("You need between %d and %d people in your group" % (
                    config.group_min_size, config.group_max_size))
            if config.mailer_enabled:
                for _, invitee_name, _, _, _, invitee_email in invitees:
                    email_payload = create_email("group_invite", invitee_email,
                                                 "%s has invited you to a group" % inviter_name,
                                                 inviter_name=inviter_name,
                                                 inviter_github=inviter_github,
                                                 invitee_name=invitee_name,
                                                 invitees=invitees)
                    mailer_job = mailer_queue.create(c, "send", email_payload)
                    mailer_jobs.append(mailer_job)
            invitation_id = get_next_autoincrementing_value(c, "group_next_invitation_id")
            for invitation_user_id in invitation_user_ids:
                c.execute("INSERT INTO invitations (invitation_id, user, status) VALUES (?, ?, ?)",
                          [invitation_id, invitation_user_id, INVITED])
            c.execute("INSERT INTO invitations (invitation_id, user, status) VALUES (?, ?, ?)",
                      [invitation_id, inviter_user_id, ACCEPTED])
            modify_grouplimit(c, inviter_user_id, -1)
            finalize_group_if_ready(c, invitation_id)
        if config.mailer_enabled:
            for mailer_job in mailer_jobs:
                mailer_queue.enqueue(mailer_job)
        return redirect(url_for("dashboard.group"))
    except ValidationError as e:
        return redirect_with_error(url_for("dashboard.group"), e)
Пример #3
0
def student_id():
    github = github_username()
    if request.method == "GET":
        return render_template("onboarding/student_id.html",
                               github=github)
    try:
        mailer_job = None
        github_job = None
        with DbCursor() as c:
            user = get_user_by_github(c, github)
            if user:
                return redirect(url_for("dashboard.index"))
            student_id = request.form.get("f_student_id")
            if not student_id:
                fail_validation("Student ID is required")
            user = get_user_by_student_id(c, student_id)
            if not user:
                fail_validation("Student not found with that student ID")
            user_id, name, _, login, old_github, email = user
            if old_github:
                fail_validation("Another GitHub account has been associated with that student "
                                "ID already.")
            if not name:
                fail_validation("There is no name associated with this account. (Contact your TA?)")
            if not login:
                fail_validation("There is no login associated with this account. (Contact your "
                                "TA?)")
            if not email:
                fail_validation("There is no email associated with this account. (Contact your "
                                "TA?)")
            c.execute('''UPDATE users SET github = ? WHERE sid = ?''',
                      [github, student_id])
            if not config.github_read_only_mode:
                github_job = repomanager_queue.create(c, "assign_repo", (login, [github]))
            if config.mailer_enabled:
                if config.inst_account_enabled:
                    attachments = [("pdf", get_inst_account_form_path(login))]
                else:
                    attachments = []
                email_payload = create_email("onboarding_confirm", email,
                                             "%s Autograder Registration" % config.course_number,
                                             _attachments=attachments, name=name, login=login,
                                             inst_account_enabled=config.inst_account_enabled)
                mailer_job = mailer_queue.create(c, "send", email_payload)
        if config.mailer_enabled and mailer_job:
            mailer_queue.enqueue(mailer_job)
        if not config.github_read_only_mode and github_job:
            repomanager_queue.enqueue(github_job)
        return redirect(url_for(_get_next_step("onboarding.student_id")))
    except ValidationError as e:
        return redirect_with_error(url_for("onboarding.student_id"), e)
Пример #4
0
def group_respond():
    if not config.groups_enabled:
        abort(404)
    try:
        github_job = None
        mailer_jobs = []
        with DbCursor() as c:
            invitation_id = request.form.get("f_group")
            response = request.form.get("f_response")
            if not invitation_id:
                fail_validation("Expected an invitation identifier (probably a programming error)")
            if response not in ("accept", "reject"):
                fail_validation("Expected a response (probably a programming error)")
            student = _get_student(c)
            user_id, _, _, _, _, _ = student
            c.execute('''SELECT status FROM invitations WHERE invitation_id = ? AND user = ?''',
                      [invitation_id, user_id])
            statuses = c.fetchall()
            if len(statuses) != 1:
                fail_validation("Invitation has already been responded to")
            status = statuses[0][0]
            if response == "accept":
                if status != INVITED:
                    fail_validation("Invitation has already been responded to")
                grouplimit = get_grouplimit(c, user_id)
                if grouplimit < 1:
                    fail_validation("You have joined too many groups already")
                c.execute('''UPDATE invitations SET status = ?
                             WHERE invitation_id = ? AND user = ?''',
                          [ACCEPTED, invitation_id, user_id])
                modify_grouplimit(c, user_id, -1)
                group_name, group_members = finalize_group_if_ready(c, invitation_id)
                if group_name:
                    if not config.github_read_only_mode:
                        group_githubs = []
                        for _, _, _, github in group_members:
                            assert github, "GitHub handle is empty"
                            group_githubs.append(github)
                        github_job = repomanager_queue.create(c, "assign_repo",
                                                              (group_name, group_githubs))
                    if config.mailer_enabled:
                        for _, name, email, github in group_members:
                            email_payload = create_email("group_confirm", email,
                                                         "%s has been created" % group_name,
                                                         group_name=group_name,
                                                         name=name, group_members=group_members)
                            mailer_job = mailer_queue.create(c, "send", email_payload)
                            mailer_jobs.append(mailer_job)
            elif response == "reject":
                if status not in (ACCEPTED, INVITED):
                    fail_validation("Invitation has already been rejected")
                c.execute('''UPDATE invitations SET status = ?
                             WHERE invitation_id = ? AND user = ?''',
                          [REJECTED, invitation_id, user_id])
                if status == ACCEPTED:
                    # Give them back +1 to their group limit
                    modify_grouplimit(c, user_id, +1)
        if config.mailer_enabled:
            for mailer_job in mailer_jobs:
                mailer_queue.enqueue(mailer_job)
        if github_job and not config.github_read_only_mode:
            repomanager_queue.enqueue(github_job)
        return redirect(url_for("dashboard.group"))
    except ValidationError as e:
        return redirect_with_error(url_for("dashboard.group"), e)
Пример #5
0
def group_create():
    if not config.groups_enabled:
        abort(404)
    try:
        githubs = request.form.getlist("f_github")
        github_job = None
        mailer_jobs = []
        with DbCursor() as c:
            student = _get_student(c)
            inviter_user_id, inviter_name, _, _, inviter_github, _ = student
            grouplimit = get_grouplimit(c, inviter_user_id)
            if grouplimit < 1:
                fail_validation("You are in too many groups already")
            invitees = []
            invitation_user_ids = set()
            for github in githubs:
                if not github:
                    continue
                invitee = get_user_by_github(c, github)
                if invitee is None:
                    fail_validation("GitHub username not found: %s" % github)
                invitee_id, _, _, _, _, _ = invitee
                if invitee_id == inviter_user_id:
                    continue
                if invitee_id in invitation_user_ids:
                    continue
                invitation_user_ids.add(invitee_id)
                invitees.append(invitee)
            if not config.group_min_size <= len(
                    invitation_user_ids) + 1 <= config.group_max_size:
                fail_validation(
                    "You need between %d and %d people in your group" %
                    (config.group_min_size, config.group_max_size))
            if config.mailer_enabled:
                for _, invitee_name, _, _, _, invitee_email in invitees:
                    email_payload = create_email(
                        "group_invite",
                        invitee_email,
                        "%s has invited you to a group" % inviter_name,
                        inviter_name=inviter_name,
                        inviter_github=inviter_github,
                        invitee_name=invitee_name,
                        invitees=invitees)
                    mailer_job = mailer_queue.create(c, "send", email_payload)
                    mailer_jobs.append(mailer_job)
            invitation_id = get_next_autoincrementing_value(
                c, "group_next_invitation_id")
            for invitation_user_id in invitation_user_ids:
                c.execute(
                    "INSERT INTO invitations (invitation_id, user, status) VALUES (?, ?, ?)",
                    [invitation_id, invitation_user_id, INVITED])
            c.execute(
                "INSERT INTO invitations (invitation_id, user, status) VALUES (?, ?, ?)",
                [invitation_id, inviter_user_id, ACCEPTED])
            modify_grouplimit(c, inviter_user_id, -1)
            group_name, group_members = finalize_group_if_ready(
                c, invitation_id)
            if group_name:
                if not config.github_read_only_mode:
                    group_githubs = []
                    for _, _, _, github in group_members:
                        assert github, "GitHub handle is empty"
                        group_githubs.append(github)
                    github_job = repomanager_queue.create(
                        c, "assign_repo", (group_name, group_githubs))
                if config.mailer_enabled:
                    for _, name, email, github in group_members:
                        email_payload = create_email(
                            "group_confirm",
                            email,
                            "%s has been created" % group_name,
                            group_name=group_name,
                            name=name,
                            group_members=group_members)
                        mailer_job = mailer_queue.create(
                            c, "send", email_payload)
                        mailer_jobs.append(mailer_job)
        if config.mailer_enabled:
            for mailer_job in mailer_jobs:
                mailer_queue.enqueue(mailer_job)
        if github_job and not config.github_read_only_mode:
            repomanager_queue.enqueue(github_job)
        return redirect(url_for("dashboard.group"))
    except ValidationError as e:
        return redirect_with_error(url_for("dashboard.group"), e)
Пример #6
0
def group_respond():
    if not config.groups_enabled:
        abort(404)
    try:
        github_job = None
        mailer_jobs = []
        with DbCursor() as c:
            invitation_id = request.form.get("f_group")
            response = request.form.get("f_response")
            if not invitation_id:
                fail_validation(
                    "Expected an invitation identifier (probably a programming error)"
                )
            if response not in ("accept", "reject"):
                fail_validation(
                    "Expected a response (probably a programming error)")
            student = _get_student(c)
            user_id, _, _, _, _, _ = student
            c.execute(
                '''SELECT status FROM invitations WHERE invitation_id = ? AND user = ?''',
                [invitation_id, user_id])
            statuses = c.fetchall()
            if len(statuses) != 1:
                fail_validation("Invitation has already been responded to")
            status = statuses[0][0]
            if response == "accept":
                if status != INVITED:
                    fail_validation("Invitation has already been responded to")
                grouplimit = get_grouplimit(c, user_id)
                if grouplimit < 1:
                    fail_validation("You have joined too many groups already")
                c.execute(
                    '''UPDATE invitations SET status = ?
                             WHERE invitation_id = ? AND user = ?''',
                    [ACCEPTED, invitation_id, user_id])
                modify_grouplimit(c, user_id, -1)
                group_name, group_members = finalize_group_if_ready(
                    c, invitation_id)
                if group_name:
                    if not config.github_read_only_mode:
                        group_githubs = []
                        for _, _, _, github in group_members:
                            assert github, "GitHub handle is empty"
                            group_githubs.append(github)
                        github_job = repomanager_queue.create(
                            c, "assign_repo", (group_name, group_githubs))
                    if config.mailer_enabled:
                        for _, name, email, github in group_members:
                            email_payload = create_email(
                                "group_confirm",
                                email,
                                "%s has been created" % group_name,
                                group_name=group_name,
                                name=name,
                                group_members=group_members)
                            mailer_job = mailer_queue.create(
                                c, "send", email_payload)
                            mailer_jobs.append(mailer_job)
            elif response == "reject":
                if status not in (ACCEPTED, INVITED):
                    fail_validation("Invitation has already been rejected")
                c.execute(
                    '''UPDATE invitations SET status = ?
                             WHERE invitation_id = ? AND user = ?''',
                    [REJECTED, invitation_id, user_id])
                if status == ACCEPTED:
                    # Give them back +1 to their group limit
                    modify_grouplimit(c, user_id, +1)
        if config.mailer_enabled:
            for mailer_job in mailer_jobs:
                mailer_queue.enqueue(mailer_job)
        if github_job and not config.github_read_only_mode:
            repomanager_queue.enqueue(github_job)
        return redirect(url_for("dashboard.group"))
    except ValidationError as e:
        return redirect_with_error(url_for("dashboard.group"), e)