Ejemplo n.º 1
0
def admin():
    """List of all students and groups. Group, student, and instructor management forms"""
    check_admin()
    db_ses = db.session
    # Queries for the tables of students and groups
    students = db_ses.query(User.id, User.username, User.email, User.is_static).filter(User.is_instructor == False)
    instructors = db_ses.query(User.id, User.username, User.email).filter(User.is_instructor == True)
    groups = StudentGroups.query.all()
    groupNames = []
    users_per_group = {}

    for g in groups:
        groupNames.append(g.name)

    for name in groupNames:
        users_per_group[name] = db_ses.query(User.id, User.username, User.email, User.is_static).filter(
            StudentGroups.name == name,
            StudentGroups.id == GroupUsers.group_id,
            GroupUsers.user_id == User.id,
        )

    if request.method == "GET":
        groupMaker = GroupForm()
        userAdder = addUsersForm()
        instructorManager = manageInstructorForm()
        groupEraser = deleteGroupForm()

        return render_template(
            "dashboard/admin.html",
            groupMaker=groupMaker,
            userAdder=userAdder,
            instructorManager=instructorManager,
            groups=groups,
            students=students,
            instructors=instructors,
            usersPGroup=users_per_group,
            groupEraser=groupEraser
        )

    elif request.method == "POST":
        ajax = process_request(request.form)
        if ajax:
            temp = ajax[0]
            if temp == 'utils/create_group_response.html':
                if len(ajax) == 1:
                    return render_template(temp)
                elif len(ajax) < 4:
                    return render_template(temp, group=ajax[1], users=ajax[2])
                else:
                    return render_template(temp, group=ajax[1], users=ajax[2], pairs=ajax[3])
            elif temp == 'utils/manage_student_response.html':
                if len(ajax) == 1:
                    return render_template(temp)
                else:
                    return render_template(temp, group=ajax[1], users=ajax[2])
        else:
            return redirect(url_for("dashboard.admin"))
Ejemplo n.º 2
0
def instructor():
    """List of an instructors groups"""
    check_instructor()
    # Queries for the owned groups table
    curId = session.get("_user_id")
    db_ses = db.session

    students = db_ses.query(User.id, User.username, User.email,
                            User.is_static).filter(User.is_instructor == False)
    groups = db_ses.query(
        StudentGroups.id, StudentGroups.name,
        StudentGroups.code).filter(StudentGroups.owner_id == curId)
    users_per_group = {}

    for g in groups:
        users_per_group[g.name] = db_ses.query(
            User.id, User.username, User.email, User.is_static).filter(
                StudentGroups.name == g.name,
                StudentGroups.id == GroupUsers.group_id,
                GroupUsers.user_id == User.id,
            )

    if request.method == "GET":
        groupMaker = GroupForm()
        userAdder = addUsersForm()
        return render_template(
            "dashboard/instructor.html",
            groupMaker=groupMaker,
            userAdder=userAdder,
            students=students,
            groups=groups,
            usersPGroup=users_per_group,
        )

    elif request.method == "POST":
        ajax = process_request(request.form)
        if ajax:
            temp = ajax[0]
            if temp == 'utils/create_group_response.html':
                if len(ajax) == 1:
                    return render_template(temp)
                elif len(ajax) < 4:
                    return render_template(temp, group=ajax[1], users=ajax[2])
                else:
                    return render_template(temp,
                                           group=ajax[1],
                                           users=ajax[2],
                                           pairs=ajax[3])
            elif temp == 'utils/manage_student_response.html':
                if len(ajax) == 1:
                    return render_template(temp)
                else:
                    return render_template(temp, group=ajax[1], users=ajax[2])
        else:
            return redirect(url_for("dashboard.instructor"))
Ejemplo n.º 3
0
def process_groupMaker():  # Form to create a new group |  # GroupForm
    gM = GroupForm(request.form)  # type1Form(request.form)  #
    if gM.validate_on_submit():
        code = grc()
        name = gM.name.data
        group = StudentGroups.create(name=name,
                                     owner_id=session.get('_user_id'),
                                     code=code)
        users = []
        size = gM.size.data
        if size == 0:
            flash('Created group {0}'.format(name), 'success')
            return 'utils/create_group_response.html', group, users
        else:
            pairs = []
            gid = group.get_id()
            fName = name  # formatted group name
            name = name.replace(" ", "")  # group name with no spaces
            j = 0
            for i in range(1, size + 1):
                username = "******".format(name, i)
                password = grc()
                user = User.create(username=username,
                                   email=username + "@edurange.org".format(i),
                                   password=password,
                                   active=True,
                                   is_static=True)
                uid = user.get_id()
                GroupUsers.create(user_id=uid, group_id=gid)
                j += 1
                pairs.append((username, password))
                users.append(user)
            flash(
                'Created group {0} and populated it with {1} temporary accounts'
                .format(fName, j), 'success')
            return 'utils/create_group_response.html', group, users, pairs
    else:
        flash_errors(gM)
        return 'utils/create_group_response.html',
Ejemplo n.º 4
0
 def test_validate_create_group(self, admin):
     form1 = GroupForm(name="Test 1", size=0)
     assert form1.validate() is True
     form2 = GroupForm(name="Test 2", size=5)
     assert form2.validate() is True
Ejemplo n.º 5
0
def admin():
    check_admin()
    students = User.query.all()
    stuTable = StudentTable(students)
    groups = StudentGroups.query.all()
    groTable = GroupTable(groups)
    groupNames = []
    for g in groups:
        groupNames.append(g.name)
    if request.method == 'GET':
        form = EmailForm()
        form1 = GroupForm()
        form2 = GroupFinderForm()
        return render_template('dashboard/admin.html',
                               stuTable=stuTable,
                               groTable=groTable,
                               form=form,
                               form1=form1,
                               form2=form2,
                               groups=groups,
                               students=students)

    elif request.form.get('to') is not None:
        form = EmailForm(request.form)
        if form.validate_on_submit():
            email_data = {
                'subject': form.subject.data,
                'to': form.to.data,
                'body': form.body.data
            }
            email = form.to.data
            if request.form['submit'] == 'Send':
                send_async_email.delay(email_data)
                flash('Sending email to {0}'.format(email))
            else:
                send_async_email.apply_async(args=[email_data], countdown=60)
                flash('An email will be sent to {0} in one minute.'.format(
                    email))
            return redirect(url_for('dashboard.admin'))

    elif request.form.get('name') is not None:
        form = GroupForm(request.form)
        if form.validate_on_submit():
            code = grc()
            name = form.name.data
            StudentGroups.create(name=name,
                                 owner_id=session.get('_user_id'),
                                 code=code)
            flash('Created group {0}'.format(name))

            return redirect(url_for('dashboard.admin'))

    elif request.form.get('group') is not None:
        form = GroupFinderForm(request.form)
        if form.validate_on_submit():
            db_ses = db.session
            name = form.group.data
            groupUsers = db_ses.query(
                User.id, User.username, User.email, StudentGroups,
                GroupUsers).filter(StudentGroups.name == name).filter(
                    StudentGroups.id == GroupUsers.group_id).filter(
                        GroupUsers.user_id == User.id)
            groUTable = GroupUserTable(groupUsers)
            return render_template('dashboard/admin.html',
                                   students=students,
                                   groTable=groTable,
                                   groUTable=groUTable,
                                   form=form,
                                   groups=groupNames)
        else:
            flash_errors(form)
        return redirect(url_for('dashboard.admin'))

    elif request.form.get('groups') is not None:
        form = addUsersForm(request.form)
        if form.validate_on_submit():
            db_ses = db.session

            if len(form.groups.data) < 1:
                flash('A group must be selected')
                return redirect(url_for('dashboard.admin'))

            group = form.groups.data

            gid = db_ses.query(
                StudentGroups.id).filter(StudentGroups.name == group)
            uids = form.uids.data  # string form
            if uids[-1] == ',':
                uids = uids[:
                            -1]  # slice last comma to avoid empty string after string split
            uids = uids.split(',')
            for i, uid in enumerate(uids):
                check = db_ses.query(
                    GroupUsers.id).filter(GroupUsers.user_id == uid)
                if any(check):
                    flash('User already in group.')
                    uids.pop(i - 1)
                    pass
                else:
                    GroupUsers.create(user_id=uid, group_id=gid)

            flash('Added {0} users to group {1}. DEBUG: {2}'.format(
                len(uids), group, uids))
            return redirect(url_for('dashboard.admin'))
        else:
            flash_errors(form)
        return redirect(url_for('dashboard.admin'))

    elif request.form.get('uName') is not None:
        form = makeInstructorForm(request.form)
        if form.validate_on_submit():
            uName = form.uName.data
            user = User.query.filter_by(username=uName).first()
            user.update(is_instructor=True)

            flash('Made {0} an Instructor.'.format(uName))
            return redirect(url_for('dashboard.admin'))
        else:
            flash_errors(form)
        return redirect(url_for('dashboard.admin'))