Exemplo n.º 1
0
def test_assignment_find_for_course(db, course_strange, assignment_false, assignment_tree):
    courses = AssignmentModel.find_for_course(db, course_strange.id)
    assert len(courses.all()) == 1
    assignment_false.active = True
    courses = AssignmentModel.find_for_course(db, course_strange.id)
    assert len(courses.all()) == 2
    assignment_false.active = False
Exemplo n.º 2
0
    def get(self):

        models = []

        [course_code] = self.get_params(["course_id"])

        if not course_code:
            note = f"Assigment call requires a course id"
            self.log.info(note)
            self.finish({"success": False, "note": note, "value": []})
            return

        # Who is my user?
        this_user = self.nbex_user
        self.log.debug(f"User: {this_user.get('name')}")
        # For what course do we want to see the assignments?
        self.log.debug(f"Course: {course_code}")
        # Is our user subscribed to this course?
        if course_code not in this_user["courses"]:
            note = f"User not subscribed to course {course_code}"
            self.log.info(note)
            self.finish({"success": False, "note": note, "value": []})
            return

        # Find the course being referred to
        with scoped_session() as session:
            course = Course.find_by_code(
                db=session, code=course_code, org_id=this_user["org_id"], log=self.log
            )
            if not course:
                note = f"Course {course_code} does not exist"
                self.log.info(note)
                self.finish({"success": False, "note": note, "value": []})
                return

            assignments = AssignmentModel.find_for_course(
                db=session, course_id=course.id, log=self.log
            )

            for assignment in assignments:
                self.log.debug(f"==========")
                self.log.debug(f"Assignment: {assignment}")
                for action in assignment.actions:
                    # For every action that is not "released" checked if the user id matches
                    if (
                        action.action != AssignmentActions.released
                        and this_user.get("id") != action.user_id
                    ):
                        self.log.debug(
                            f"ormuser: {this_user.get('id')} - actionUser {action.user_id}"
                        )
                        self.log.debug("Action does not belong to user, skip action")
                        continue
                    notebooks = []

                    for notebook in assignment.notebooks:
                        feedback_available = False
                        feedback_timestamp = None
                        if action.action == AssignmentActions.submitted:
                            feedback = Feedback.find_notebook_for_student(
                                db=session,
                                notebook_id=notebook.id,
                                student_id=this_user.get("id"),
                                log=self.log,
                            )
                            if feedback:
                                feedback_available = bool(feedback)
                                feedback_timestamp = feedback.timestamp.strftime(
                                    "%Y-%m-%d %H:%M:%S.%f %Z"
                                )

                        notebooks.append(
                            {
                                "notebook_id": notebook.name,
                                "has_exchange_feedback": feedback_available,
                                "feedback_updated": False,  # TODO: needs a real value
                                "feedback_timestamp": feedback_timestamp,
                            }
                        )
                    models.append(
                        {
                            "assignment_id": assignment.assignment_code,
                            "student_id": action.user_id,
                            "course_id": assignment.course.course_code,
                            "status": action.action.value,  # currently called 'action' in our db
                            "path": action.location,
                            "notebooks": notebooks,
                            "timestamp": action.timestamp.strftime(
                                "%Y-%m-%d %H:%M:%S.%f %Z"
                            ),
                        }
                    )

        self.log.debug(f"Assignments: {models}")
        self.finish({"success": True, "value": models})
Exemplo n.º 3
0
    def get(self):

        [course_code, assignment_code,
         path] = self.get_params(["course_id", "assignment_id", "path"])

        if not (course_code and assignment_code and path):
            note = "Collection call requires a course code, an assignment code, and a path"
            self.log.info(note)
            self.finish({"success": False, "note": note})
            return

        # Who is my user?
        this_user = self.nbex_user

        self.log.debug(f"User: {this_user.get('name')}")
        # For what course do we want to see the assignments?
        self.log.debug(f"Course: {course_code}")
        # Is our user subscribed to this course?
        if course_code not in this_user["courses"]:
            note = f"User not subscribed to course {course_code}"
            self.log.info(note)
            self.finish({"success": False, "note": note})
            return
        self.log.info(f"user: {this_user}")

        if not "instructor" == this_user["current_role"].casefold(
        ):  # we may need to revisit this
            note = f"User not an instructor to course {course_code}"
            self.log.info(note)
            self.finish({"success": False, "note": note})
            return

        # Find the course being referred to
        with scoped_session() as session:
            course = Course.find_by_code(db=session,
                                         code=course_code,
                                         org_id=this_user["org_id"],
                                         log=self.log)
            if not course:
                note = f"Course {course_code} does not exist"
                self.log.info(note)
                self.finish({"success": False, "note": note})
                return

            # We need to key off the assignment, but we're actually looking
            # for the action with a action and a specific path
            assignments = AssignmentModel.find_for_course(
                db=session,
                course_id=course.id,
                log=self.log,
                action=AssignmentActions.submitted.value,
                path=path,
            )

            self.set_header("Content-Type", "application/gzip")

            # I do not want to assume there will just be one.
            for assignment in assignments:
                self.log.debug(f"Assignment: {assignment}")

                try:
                    with open(path, "r+b") as handle:
                        data = handle.read()
                except Exception as e:  # TODO: exception handling
                    self.log.warning(
                        f"Error: {e}")  # TODO: improve error message

                    # error 500??
                    raise Exception

                self.log.info(
                    f"Adding action {AssignmentActions.collected.value} for user {this_user['id']} against assignment {assignment.id}"  # noqa: E501
                )
                action = Action(
                    user_id=this_user["id"],
                    assignment_id=assignment.id,
                    action=AssignmentActions.collected,
                    location=path,
                )
                session.add(action)

                self.finish(data)
                return