Example #1
0
    def upload_url(self, data, suffix=''):
        """
        Request a URL to be used for uploading content related to this
        submission.

        Returns:
            A URL to be used to upload content associated with this submission.

        """
        if "contentType" not in data:
            return {
                'success': False,
                'msg': self._(u"Must specify contentType.")
            }
        content_type = data['contentType']

        if not content_type.startswith('image/'):
            return {
                'success': False,
                'msg': self._(u"contentType must be an image.")
            }

        try:
            key = self._get_student_item_key()
            url = file_upload_api.get_upload_url(key, content_type)
            return {'success': True, 'url': url}
        except FileUploadError:
            logger.exception("Error retrieving upload URL.")
            return {
                'success': False,
                'msg': self._(u"Error retrieving upload URL.")
            }
Example #2
0
    def upload_url(self, data, suffix=''):  # pylint: disable=unused-argument
        """
        Request a URL to be used for uploading content related to this
        submission.

        Returns:
            A URL to be used to upload content associated with this submission.

        """
        if 'contentType' not in data or 'filename' not in data:
            return {
                'success': False,
                'msg': self._(u"There was an error uploading your file.")
            }
        content_type = data['contentType']
        file_name = data['filename']
        file_name_parts = file_name.split('.')

        file_num = int(data.get('filenum', 0))

        file_ext = file_name_parts[-1] if len(file_name_parts) > 1 else None
        if self.file_upload_type == 'image' and content_type not in self.ALLOWED_IMAGE_MIME_TYPES:
            return {
                'success': False,
                'msg': self._(u"Content type must be GIF, PNG or JPG.")
            }

        if self.file_upload_type == 'pdf-and-image' and content_type not in self.ALLOWED_FILE_MIME_TYPES:
            return {
                'success': False,
                'msg': self._(u"Content type must be PDF, GIF, PNG or JPG.")
            }

        if self.file_upload_type == 'custom' and file_ext.lower(
        ) not in self.white_listed_file_types:
            return {
                'success':
                False,
                'msg':
                self._(u"File type must be one of the following types: {}").
                format(', '.join(self.white_listed_file_types))
            }

        if file_ext in self.FILE_EXT_BLACK_LIST:
            return {
                'success': False,
                'msg': self._(u"File type is not allowed.")
            }
        try:
            key = self._get_student_item_key(file_num)
            url = file_upload_api.get_upload_url(key, content_type)
            return {'success': True, 'url': url}
        except FileUploadError:
            logger.exception(
                u"FileUploadError:Error retrieving upload URL for the data:{data}."
                .format(data=data))
            return {
                'success': False,
                'msg': self._(u"Error retrieving upload URL.")
            }
 def test_get_upload_url(self):
     conn = boto.connect_s3()
     conn.create_bucket('mybucket')
     uploadUrl = api.get_upload_url("foo", "bar")
     self.assertIn(
         "https://mybucket.s3.amazonaws.com/submissions_attachments/foo",
         uploadUrl)
Example #4
0
    def upload_url(self, data, suffix=''):
        """
        Request a URL to be used for uploading content related to this
        submission.

        Returns:
            A URL to be used to upload content associated with this submission.

        """
        if 'contentType' not in data or 'filename' not in data:
            return {'success': False, 'msg': self._(u"There was an error uploading your file.")}
        content_type = data['contentType']
        file_name = data['filename']
        file_name_parts = file_name.split('.')
        file_ext = file_name_parts[-1] if len(file_name_parts) > 1 else None

        if self.file_upload_type == 'image' and content_type not in self.ALLOWED_IMAGE_MIME_TYPES:
            return {'success': False, 'msg': self._(u"Content type must be GIF, PNG or JPG.")}

        if self.file_upload_type == 'pdf-and-image' and content_type not in self.ALLOWED_FILE_MIME_TYPES:
            return {'success': False, 'msg': self._(u"Content type must be PDF, GIF, PNG or JPG.")}

        if self.file_upload_type == 'custom' and file_ext not in self.white_listed_file_types:
            return {'success': False, 'msg': self._(u"File type must be one of the following types: {}").format(
                ', '.join(self.white_listed_file_types))}

        if file_ext in self.FILE_EXT_BLACK_LIST:
            return {'success': False, 'msg': self._(u"File type is not allowed.")}
        try:
            key = self._get_student_item_key()
            url = file_upload_api.get_upload_url(key, content_type)
            return {'success': True, 'url': url}
        except FileUploadError:
            logger.exception("Error retrieving upload URL.")
            return {'success': False, 'msg': self._(u"Error retrieving upload URL.")}
    def upload_url(self, data, suffix=''):  # pylint: disable=unused-argument
        """
        Request a URL to be used for uploading content related to this
        submission.

        Returns:
            A URL to be used to upload content associated with this submission.

        """
        if 'contentType' not in data or 'filename' not in data:
            return {'success': False, 'msg': self._("There was an error uploading your file.")}

        if not self.allow_multiple_files:

            # Here we check if there are existing file uploads by checking for
            # an existing download url for any of the upload slots.
            # Note that we can't use self.saved_files_descriptions because that
            # is populated before files are uploaded
            for i in range(self.MAX_FILES_COUNT):
                file_url = self._get_download_url(i)
                if file_url:
                    return {'success': False,
                            'msg': self._("Only a single file upload is allowed for this assessment.")}

        file_num = int(data.get('filenum', 0))

        _, file_ext = os.path.splitext(data['filename'])
        file_ext = file_ext.strip('.') if file_ext else None
        content_type = data['contentType']

        # Validate that there are no data issues and file type is allowed
        if not self.is_supported_upload_type(file_ext, content_type):
            return {'success': False, 'msg': self._(
                "File upload failed: unsupported file type."
                "Only the supported file types can be uploaded."
                "If you have questions, please reach out to the course team."
            )}

        # Attempt to upload
        file_num = int(data.get('filenum', 0))
        try:
            key = self._get_student_item_key(file_num)
            url = file_upload_api.get_upload_url(key, content_type)
            return {'success': True, 'url': url}
        except FileUploadError:
            logger.exception("FileUploadError:Error retrieving upload URL for the data: %s.", data)
            return {'success': False, 'msg': self._("Error retrieving upload URL.")}
Example #6
0
    def upload_url(self, data, suffix=''):
        """
        Request a URL to be used for uploading content related to this
        submission.

        Returns:
            A URL to be used to upload content associated with this submission.

        """
        if "contentType" not in data:
            return {'success': False, 'msg': self._(u"Must specify contentType.")}
        content_type = data['contentType']

        if not content_type.startswith('image/'):
            return {'success': False, 'msg': self._(u"contentType must be an image.")}

        try:
            key = self._get_student_item_key()
            url = file_upload_api.get_upload_url(key, content_type)
            return {'success': True, 'url': url}
        except FileUploadError:
            logger.exception("Error retrieving upload URL.")
            return {'success': False, 'msg': self._(u"Error retrieving upload URL.")}
Example #7
0
 def test_get_upload_url_error(self, mock_s3):
     with raises(exceptions.FileUploadInternalError):
         mock_s3.side_effect = Exception("Oh noes")
         api.get_upload_url("foo", "bar")
Example #8
0
 def test_get_upload_url_no_key(self):
     with raises(exceptions.FileUploadRequestError):
         api.get_upload_url("", "bar")
Example #9
0
 def test_get_upload_url_no_bucket(self):
     with raises(exceptions.FileUploadInternalError):
         api.get_upload_url("foo", "bar")
 def test_get_upload_url_error(self, mock_s3):
     mock_s3.side_effect = Exception("Oh noes")
     api.get_upload_url("foo", "bar")
 def test_get_upload_url_no_key(self):
     api.get_upload_url("", "bar")
 def test_get_upload_url_no_bucket(self):
     api.get_upload_url("foo", "bar")
Example #13
0
 def test_get_upload_url_error(self, mock_s3):
     mock_s3.side_effect = Exception("Oh noes")
     api.get_upload_url("foo", "bar")
Example #14
0
 def test_get_upload_url_no_key(self):
     api.get_upload_url("", "bar")
Example #15
0
 def test_get_upload_url_no_bucket(self):
     api.get_upload_url("foo", "bar")
Example #16
0
 def test_get_upload_url(self):
     conn = boto.connect_s3()
     conn.create_bucket('mybucket')
     uploadUrl = api.get_upload_url("foo", "bar")
     self.assertIn("https://mybucket.s3.amazonaws.com/submissions_attachments/foo", uploadUrl)
Example #17
0
 def test_get_upload_url(self):
     conn = boto3.client("s3")
     conn.create_bucket(Bucket="mybucket")
     uploadUrl = api.get_upload_url("foo", "bar")
     self.assertIn("/submissions_attachments/foo", uploadUrl)