Esempio n. 1
0
def register_courses():
    """Adds a course to the system.
    Returns
    -------
    dict
        Flashes, course data from the form
    """

    flashes = list()

    try:
        if Course.get_by_department_number(request.form["number"]):
            course = Course(request.form["department"], request.form["number"],
                            request.form["name"])
        else:
            return error("Course already exists"), 400
    except KeyError:
        return error("Not all fields satisfied."), 400

    if Admin.add_course(course=course):
        logger.info(f"Course {request.form['number']} added")
        flashes.append("Course added!")
        return response(flashes), 200
    else:
        flashes.append("There was a problem adding your course")
        return response(flashes), 400
Esempio n. 2
0
def add_assignment():
    """Adds new assignment for the class

    Returns
    -------
    dict
        The view response
    """
    request.form["assigned_to"].choices = current_user.get_class_names()

    try:
        file_list = get_existing_assignment_files()

        new_assignment = Assignment(
            date_assigned=datetime.utcnow(),
            assigned_by=current_user.ID,
            assigned_to=request.form["assigned_to"],
            due_by=request.form["due_by"],
            title=request.form["title"],
            content=request.form["content"],
            filenames=file_list,
            estimated_time=request.form["estimated_time"],
            # weight=request.form['weight']
        )

        Course.get_by_id(
            request.form["assigned_to"]).add_assignment(new_assignment)

        logger.info(f"Assignment {request.form['title']} added")
        return response(flashes=["Assignment sent!"])

    except KeyError:
        return error("Not all fields satisfied"), 400
Esempio n. 3
0
def view_submissions_by_assignment(course_id: str, assignment_id: str):
    """Collects all submissions for a specific assignment of a class

    Parameters
    -------
    course_id: str
        The course ID to look up in the database

    assignment_id: str
        The assignment ID to look up in the database

    Returns
    -------
    dict
        Assignment submissions
    """
    course = Course.get_by_id(course_id)
    assignments = course.get_assignments()

    assignment: Assignment = list(
        filter(lambda a: str(a.id) == assignment_id, assignments))[0]

    if assignment is None:
        return error("Could not find assignment"), 400

    else:
        return response(data={"submissions": assignment.submissions})
Esempio n. 4
0
def edit_assignment(course_id: str, assignment_id: str):
    """Edits assignment for the class

    Parameters
    -------
    course_id: str
        The course ID to look up in the database

    assignment_id: str
        The assignment ID to look up in the database

    Returns
    -------
    dict
        Edited assignment data
    """

    course = Course.get_by_id(course_id)
    assignments = course.get_assignments()

    assignment: Assignment = list(
        filter(lambda a: str(a.id) == assignment_id, assignments))[0]

    if assignment is None:
        return error("Could not find assignment"), 400

    try:
        file_list = get_existing_assignment_files()

        edited_assignment = Assignment(
            date_assigned=assignment.date_assigned,
            assigned_by=assignment.assigned_by,
            assigned_to=request.form["assigned_to"],
            due_by=request.form["due_by"],
            title=request.form["title"],
            content=request.form["content"],
            filenames=file_list,
            estimated_time=request.form["estimated_time"],
            # weight=request.form['weight']
        )
        edited_assignment.id = assignment.id
        course.edit_assignment(edited_assignment)
        # Assign to 'assignment' so form has new details
        assignment = edited_assignment

    except KeyError:
        return error("Not all fields satisfied"), 400

    # Set default values for form.
    request.form["assigned_to"].default = assignment.assigned_to
    request.form["due_by"].default = assignment.due_by
    request.form["estimated_time"].default = assignment.estimated_time
    request.form["title"].default = assignment.title
    request.form["content"].default = assignment.content
    # request.form['weight'].default = assignment.weight
    request.files.default = assignment.filenames

    return response(data={"assignment": assignment.to_json()})
Esempio n. 5
0
def manage_courses_by_id(course_id: str):
    """Provides options to edit the course.
    Returns
    -------
    dict
        Flashes, course data from the form
    """

    flashes = list()

    course = Course.get_by_id(course_id)
    if course:
        if request.form.get("file"):
            syllabus_file = request.form["file"]
            filename = syllabus_file.filename
            blob = upload_blob(
                uuid.uuid4().hex + "." +
                syllabus_file.content_type.split("/")[-1],
                syllabus_file,
            )
            syllabus = (blob.name, filename)
            course.update_syllabus(syllabus)
            logger.info(f"Course {course._id} updated")
        Course.update(
            request.form.get("department"),
            request.form.get("number"),
            request.form.get("name"),
            request.form.get("teacher"),
            request.form.get("teacher"),
            request.form.get("description"),
            request.form.get("schedule_time"),
            request.form.get("schedule_days"),
            request.form.get("syllabus"),
            request.form.get("student"),
        )
        flashes.append("Course information successfully updated!")
        return response(flashes), 200
    else:
        return error("Course does not exist"), 404
Esempio n. 6
0
def assignments_by_class(course_id: str):
    """Get assignments for a specific class
    Parameters
    ----------
    course_id : str
        The ID of the class
    Returns
    -------
    dict
        The view response
    """

    course_assignments = Course.get_by_id(course_id).get_assignments()
    logger.info(f"All assignments from {course_id}.")
    return response(data={"assignments": course_assignments})
Esempio n. 7
0
def mark_submission(course_id: str, assignment_id: str, submission_id: str):
    flashes = []

    course = Course.get_by_id(course_id)
    assignments = course.get_assignments()
    assignment: Assignment = get(assignments, id=assignment_id)
    submission: Submission = get(assignment.submissions, id=submission_id)

    min_, max_ = course.grade_range
    if min_ < request.form["grade"] < max_:
        submission.update_grade(request.form["grade"])
        flashes.append("Grade updated!")
        return response(flashes), 200

    return error("Grade outside course grade boundary")
Esempio n. 8
0
def view_assignment_by_class_id(course_id: str):
    """Collects assignments from a specific class

    Parameters
    -------
    class_id: str
        The class ID to look up in the database

    Returns
    -------
    dict
        The specified class and its respective data (id, name, and assignments)
    """
    course_assignments = Course.get_by_id(course_id).get_assignments()

    return response(
        data={
            "assignments": list(map(lambda a: a.to_dict(), course_assignments))
        })
Esempio n. 9
0
def get_schedule_classes():
    """Gets name, dates, and times for classes

    Returns
    -------
    dict
        The view response
    """

    student_course_ids = current_user.get_course_ids()
    class_schedule = list()
    for student_course in student_course_ids:
        data = Course.get_by_id(student_course)
        course_data = {
            "name": data.name,
            "daysOfWeek": data.schedule_days,
            "startTime": data.schedule_time,
        }
        class_schedule.append(course_data)

    return response(data={"class_schedule": class_schedule})
Esempio n. 10
0
def add_teacher_to_course():
    """Adds a teacher to a course.
    Returns
    -------
    dict
        Flashes, teacher data from the form
    """

    flashes = list()

    try:
        if Teacher.get_by_email(request.form["email"]):
            Admin.add_teacher(
                Course.get_by_department_number(request.form["number"])._id,
                request.form["email"],
            )
        else:
            flashes.append("Account doesn't exist!")
            return response(flashes), 400

    except KeyError:
        return response(flashes), 400
Esempio n. 11
0
def view_assignments():
    """Collects all courses for a specific teatcher.
    Returns
    -------
    dict
        All the courses and their respective data (id, name, and assignments)
    """

    courses = []
    for course_id in current_user.courses:
        course = Course.get_by_id(course_id)
        course_assignments = course.get_assignments()
        course_data = {
            "id":
            str(course_id),
            "name":
            course.name,
            "assignments":
            list(map(lambda a: a.to_dict(), course_assignments)),
            "students":
            list(map(lambda s: Student.get_by_id(s).to_dict(),
                     course.students)),
            "description":
            course.description,
            "schedule_time":
            course.schedule_time,
            "schedule_days":
            course.schedule_days,
            "_syllabus":
            course._syllabus,
            "course_analytics":
            course._course_analytics,
        }
        courses.append(course_data)

    return response(data={"courses": courses})
Esempio n. 12
0
def manage_classes_by_id(course_id: str):
    """Updates a specified course's information

    Parameters
    -------
    course_id: str
        The course ID to look up in the database

    Returns
    -------
    dict
        Class data (id and name)
        Current class description
    """
    course = Course.get_by_id(course_id)
    syllabus_name = course.get_syllabus_name()

    try:
        syllabus = []
        if request.form and request.files:
            syllabus_file = request.files["syllabus_file"]
            syllabus_name = request.form.get("syllabus_name")
            description = request.form.get("description")

            if syllabus_file is not None:

                blob = upload_blob(
                    uuid.uuid4().hex + "." +
                    syllabus_file.content_type.split("/")[-1],
                    syllabus_file,
                )
                syllabus = [blob.name, syllabus_name]

                course.update_description(description)
                course.update_syllabus(syllabus)

                logger.info(f"Syllabus updated")
                return response(
                    flashes=["Course information successfully updated!"])

            else:
                print("Specify syllabus information")
                return error("Please specify the syllabus information"), 400

    except KeyError:
        print("Not all fields satisfied")
        return error("Not all fields satisfied"), 400

    courses = []
    for course_id in current_user.courses:
        course_data = {
            "id": str(course_id),
            "name": Course.get_by_id(course_id).name,
        }
        courses.append(course_data)

    return response(
        flashes=["Course information successfully updated!"],
        data={
            "courses": courses,
            "current_description": course.description
        },
    )