示例#1
0
    def test_upload_fail(self, m):
        requires = {"uploader": ["upload_fail", "upload_response_fail"]}
        register_uris(requires, m)

        uploader = Uploader(self.requester, "upload_response_fail", self.file)
        result = uploader.start()

        self.assertFalse(result[0])
        self.assertIsInstance(result[1], dict)
        self.assertNotIn("url", result[1])
示例#2
0
    def test_start_path(self, m):
        requires = {"uploader": ["upload_response", "upload_response_upload_url"]}
        register_uris(requires, m)

        uploader = Uploader(self.requester, "upload_response", self.filename)
        result = uploader.start()

        self.assertTrue(result[0])
        self.assertIsInstance(result[1], dict)
        self.assertIn("url", result[1])
示例#3
0
    def test_upload_fail(self, m):
        requires = {'uploader': ['upload_fail', 'upload_response_fail']}
        register_uris(requires, m)

        uploader = Uploader(self.requester, 'upload_response_fail', self.file)
        result = uploader.start()

        self.assertFalse(result[0])
        self.assertIsInstance(result[1], dict)
        self.assertNotIn('url', result[1])
示例#4
0
    def test_enrollment_created(self, prepare_canvas):
        # create file
        self.file = open(self.filename, "w+")
        uploader = Uploader(self.requester, "upload_response", self.file)
        result = uploader.start()

        assert (result[0])
        assert isinstance(result[1], dict)
        assert "url" in result[1]

        # close file(s)
        self.file.close()
示例#5
0
    def test_start_path(self, m):
        requires = {
            'uploader': ['upload_response', 'upload_response_upload_url']
        }
        register_uris(requires, m)

        uploader = Uploader(self.requester, 'upload_response', self.filename)
        result = uploader.start()

        self.assertTrue(result[0])
        self.assertIsInstance(result[1], dict)
        self.assertIn('url', result[1])
示例#6
0
    def upload_comment(self, file, **kwargs):
        """
        Upload a file to attach to this submission as a comment.

        :calls: `POST \
        /api/v1/courses/:course_id/assignments/:assignment_id/submissions/:user_id/comments/files \
        <https://canvas.instructure.com/doc/api/submission_comments.html#method.submission_comments_api.create_file>`_

        :param file: The file or path of the file to upload.
        :type file: file or str
        :returns: True if the file uploaded successfully, False otherwise, \
            and the JSON response from the API.
        :rtype: tuple
        """
        response = Uploader(
            self._requester,
            'courses/{}/assignments/{}/submissions/{}/comments/files'.format(
                self.course_id,
                self.assignment_id,
                self.user_id
            ),
            file,
            **kwargs
        ).start()

        if response[0]:
            self.edit(
                comment={
                    'file_ids': [response[1]['id']]
                }
            )
        return response
示例#7
0
    def upload(self, file, **kwargs):
        """
        Upload a file to the group.
        Only those with the 'Manage Files' permission on a group can upload files to the group.
        By default, this is anybody participating in the group, or any admin over the group.

        :calls: `POST /api/v1/groups/:group_id/files \
        <https://canvas.instructure.com/doc/api/groups.html#method.groups.create_file>`_

        :param path: The path of the file to upload.
        :type path: str
        :param file: The file or path of the file to upload.
        :type file: file or str
        :returns: True if the file uploaded successfully, False otherwise, \
                    and the JSON response from the API.
        :rtype: tuple
        """
        from canvasapi.upload import Uploader

        return Uploader(
            self._requester,
            'groups/{}/files'.format(self.id),
            file,
            **kwargs
        ).start()
示例#8
0
    def upload_to_submission(self, file, user='******', **kwargs):
        """
        Upload a file to a submission.

        :calls: `POST /api/v1/courses/:course_id/assignments/:assignment_id/ \
            submissions/:user_id/files \
        <https://canvas.instructure.com/doc/api/submissions.html#method.submissions_api.create_file>`_

        :param file: The file or path of the file to upload.
        :type file: file or str
        :param user: The object or ID of the related user, or 'self' for the
            current user. Defaults to 'self'.
        :type user: :class:`canvasapi.user.User`, int, or str

        :returns: True if the file uploaded successfully, False otherwise, \
                    and the JSON response from the API.
        :rtype: tuple
        """
        user_id = obj_or_id(user, "user", (User,))

        return Uploader(
            self._requester,
            'courses/{}/assignments/{}/submissions/{}/files'.format(
                self.course_id,
                self.id,
                user_id
            ),
            file,
            **kwargs
        ).start()
示例#9
0
    def test_upload_no_upload_params(self, m):
        register_uris({"uploader": ["upload_response_no_upload_params"]}, m)

        with self.assertRaises(ValueError):
            Uploader(
                self.requester, "upload_response_no_upload_params", self.filename
            ).start()
示例#10
0
def upload_file(file_path):
    f = open(file_path, "rb")
    tmp_file = None
    tmp_dir = None

    if args.strip is not None and file_path.endswith(".java"):
        f = open(file_path, "r", encoding="utf-8")
        lines = f.readlines()

        i = -1
        for line in lines:
            i += 1

            if line.strip().startswith("package "):
                lines[i] = ""
                continue

            if line.strip().startswith("import " + args.strip):
                lines[i] = ""
                continue

        file_name = ntpath.basename(file_path)
        tmp_dir = tempfile.mkdtemp()
        tmp_file = tmp_dir + "/" + file_name

        f = open(tmp_file, "w", encoding="utf-8")
        f.writelines(lines)
        f.close()

        f = open(tmp_file, "r", encoding="utf-8")

    uploader = Uploader(
        requester, "/api/v1/courses/" + str(args.course) + "/assignments/" +
        str(args.assignment) + "/submissions/self/files", f)
    response = uploader.start()

    f.close()

    if tmp_file is not None and tmp_dir is not None:
        unlink(tmp_file)
        rmdir(tmp_dir)

    if not response[0]:
        eprint("😥 One of the files (" + file_path + ") could not be uploaded.")
        exit(1)

    return response[1]
示例#11
0
    def upload(self, file, **kwargs):
        """
        Upload a file to this folder.

        :calls: `POST /api/v1/folders/:folder_id/files \
        <https://canvas.instructure.com/doc/api/files.html#method.folders.create_file>`_

        :param file: The file or path of the file to upload.
        :type file: file or str
        :returns: True if the file uploaded successfully, False otherwise, \
                    and the JSON response from the API.
        :rtype: tuple
        """
        my_path = 'folders/{}/files'.format(self.id)
        return Uploader(self._requester, my_path, file, **kwargs).start()
示例#12
0
    def upload(self, file, **kwargs):
        """
        Upload a file to this course.

        :calls: `POST /api/v1/courses/:course_id/files \
        <https://canvas.instructure.com/doc/api/courses.html#method.courses.create_file>`_

        :param path: The path of the file to upload.
        :type path: str
        :param file: The file or path of the file to upload.
        :type file: file or str
        :returns: True if the file uploaded successfully, False otherwise, \
                    and the JSON response from the API.
        :rtype: tuple
        """
        return Uploader(self._requester, 'courses/%s/files' % (self.id), file,
                        **kwargs).start()
def upload_file_to_assignment(requester, course_id, assn_id, user_id, path):
    """
    Uploads the file found at path to the assignment indicated.
    """

    if not os.path.isfile(path):
        raise ValueError("'path' must be a valid path to a file.")

    unformatted_str = ("/api/v1/courses/{course_id}"
                       + "/assignments/{assignment_id}"
                       + "/submissions/{user_id}/files")
    upload_url = unformatted_str.format(course_id = course_id,
                                        assignment_id = assn_id,
                                        user_id = user_id)
    print(upload_url)
    print(path)
    return Uploader(requester, upload_url, path).start()
示例#14
0
    def upload(self, file, **kwargs):
        """
        Upload a file for a user.

        NOTE: You *must* have authenticated with this user's API key to
        upload on their behalf no matter what permissions the issuer of the
        request has.

        :calls: `POST /api/v1/users/:user_id/files \
        <https://canvas.instructure.com/doc/api/users.html#method.users.create_file>`_

        :param file: The file or path of the file to upload.
        :type file: file or str
        :returns: True if the file uploaded successfully, False otherwise, \
                    and the JSON response from the API.
        :rtype: tuple
        """
        return Uploader(self._requester, 'users/{}/files'.format(self.id),
                        file, **kwargs).start()
示例#15
0
    def upload_comment(self, file, **kwargs):
        """
        Upload a file to attach to this submission comment.

        :calls: `POST \
        /api/v1/courses/:course_id/assignments/:assignment_id/submissions/:user_id/comments/files \
        <https://canvas.instructure.com/doc/api/submission_comments.html#method.submission_comments_api.create_file>`_

        :param file: The file or path of the file to upload.
        :type file: file or str
        :returns: True if the file uploaded successfully, False otherwise, \
            and the JSON response from the API.
        :rtype: tuple
        """
        if not hasattr(self, 'course_id'):
            raise ValueError('Must use a course to upload file comments.')

        return Uploader(
            self._requester,
            'courses/{}/assignments/{}/submissions/{}/comments/files'.format(
                self.course_id, self.assignment_id,
                self.user_id), file, **kwargs).start()
示例#16
0
    def test_upload_no_upload_url(self, m):
        register_uris({'uploader': ['upload_response_no_upload_url']}, m)

        with self.assertRaises(ValueError):
            Uploader(self.requester, 'upload_response_no_upload_url', self.filename).start()
示例#17
0
 def test_start_file_does_not_exist(self, m):
     with self.assertRaises(IOError):
         Uploader(self.requester, 'upload_response', 'test_file_not_real.xyz')