예제 #1
0
    def generate_blob_collection(self):
        """Generate a Data collection.

        Returns:

        """
        self.blob_1 = Blob(
            filename="blob1.txt",
            handle="http://127.0.0.1:8000/rest/blob/download/id1/",
            user_id="1",
        ).save()
        self.blob_2 = Blob(
            filename="blob2.txt",
            handle="http://127.0.0.1:8000/rest/blob/download/id2/",
            user_id="2",
        ).save()
        self.blob_public_workspace = Blob(
            filename="blob3.txt",
            handle="http://127.0.0.1:8000/rest/blob/download/id3/",
            user_id="1",
            workspace=self.public_workspace.id,
        ).save()
        self.blob_workspace_1 = Blob(
            filename="blob4.txt",
            handle="http://127.0.0.1:8000/rest/blob/download/id4/",
            user_id="1",
            workspace=self.workspace_1.id,
        ).save()
        self.blob_collection = [
            self.blob_1,
            self.blob_2,
            self.public_workspace,
            self.workspace_1,
        ]
예제 #2
0
def set_pid_for_blob(blob_id, blob_pid):
    """Retrieve PID matching the blob ID provided.

    Args:
        blob_id:
        blob_pid:
    """
    record_name = f"{settings.ID_PROVIDER_PREFIX_BLOB}/{blob_pid.split('/')[-1]}"

    try:
        local_id_object = get_pid_for_blob(blob_id)
    except exceptions.DoesNotExist:
        try:
            local_id_object = local_id_api.get_by_name(record_name)
        except exceptions.DoesNotExist:
            local_id_object = None

    if local_id_object:
        local_id_object.record_name = record_name
        local_id_object.record_object_class = get_api_path_from_object(Blob())
        local_id_object.record_object_id = str(blob_id)
    else:
        local_id_object = LocalId(
            record_name=record_name,
            record_object_class=get_api_path_from_object(Blob()),
            record_object_id=str(blob_id),
        )

    return local_id_api.insert(local_id_object)
    def _retrieve_data(self, request):
        """Return module display - GET method

        Args:
            request:

        Returns:

        """
        data = ""
        self.error = None
        data_xml_entities = XmlEntities()
        if request.method == "GET":
            if "data" in request.GET:
                if len(request.GET["data"]) > 0:
                    data = request.GET["data"]
        elif request.method == "POST":
            selected_option = request.POST["blob_form"]
            if selected_option == "url":
                url_form = URLForm(request.POST)
                if url_form.is_valid():
                    data = url_form.data["url"]
                else:
                    self.error = "Enter a valid URL."
            elif selected_option == "file":
                try:
                    form = BLOBHostForm(request.POST, request.FILES)
                    if not form.is_valid():
                        self.error = "No file uploaded."
                        return data

                    # get file from request
                    uploaded_file = request.FILES["file"]
                    # get filename from file
                    filename = uploaded_file.name
                    # get user id from request
                    user_id = str(request.user.id)

                    # create blob
                    blob = Blob(filename=filename, user_id=user_id)
                    # set blob file
                    blob.blob = uploaded_file
                    # save blob
                    blob_api.insert(blob)

                    # get download uri
                    data = get_blob_download_uri(blob, request)
                except:
                    self.error = "An error occurred during the upload."

        return (
            data_xml_entities.escape_xml_entities(data)
            if AUTO_ESCAPE_XML_ENTITIES
            else data
        )
예제 #4
0
 def test_blob_get_all_by_user_id_return_collection_of_blob_from_user(self):
     # Arrange
     user_id = 1
     # Act
     result = Blob.get_all_by_user_id(user_id)
     # Assert
     self.assertTrue(all(item.user_id == str(user_id) for item in result))
예제 #5
0
 def test_blob_get_all_by_workspace_return_empty_collection_of_blob_from_empty_workspace(
         self):
     # Act
     result = Blob.get_all_by_list_workspace(
         [self.fixture.workspace_without_data])
     # Assert
     self.assertTrue(result.count() == 0)
예제 #6
0
 def test_blob_get_all_by_user_id_return_empty_collection_of_blob_from_user_with_no_associated_blob(
         self):
     # Arrange
     user_id = 800
     # Act
     result = Blob.get_all_by_user_id(user_id)
     # Assert
     self.assertTrue(result.count() == 0)
예제 #7
0
 def test_blob_get_all_by_workspace_return_collection_of_blob_from_user(
         self):
     # Act
     result = Blob.get_all_by_workspace(self.fixture.workspace_1)
     # Assert
     self.assertTrue(
         all(item.user_id == str(self.fixture.workspace_1.owner)
             for item in result))
예제 #8
0
    def generate_blob_collection(self):
        """ Generate a Blob collection.

        Returns:

        """
        blob_1 = Blob(filename='blob1', user_id='1', handle='handle1').save()
        blob_2 = Blob(filename='blob2', user_id='2', handle='handle2').save()
        blob_3 = Blob(filename='blob3',
                      user_id='1',
                      handle='handle3',
                      workspace=self.workspace_1.id).save()
        blob_4 = Blob(filename='blob4',
                      user_id='2',
                      handle='handle4',
                      workspace=self.workspace_2.id).save()
        self.blob_collection = [blob_1, blob_2, blob_3, blob_4]
예제 #9
0
    def create(self, validated_data):
        """ Create and return a new `Blob` instance, given the validated data.

        Args:
            validated_data:

        Returns:

        """
        # Create blob
        blob_object = Blob(filename=validated_data['blob'].name,
                           user_id=str(validated_data['user'].id))
        # Set file content
        blob_object.blob = validated_data['blob'].file

        # Save the blob
        return blob_api.insert(blob_object)
예제 #10
0
    def generate_blob_collection(self):
        """ Generate a Blob collection.

        Returns:

            user 1 -> blob1, blob2
            user 2 -> blob3

        """
        # NOTE: no real file to avoid using unsupported GridFS mock
        self.blob_1 = Blob(filename='blob1', user_id='1',
                           handle='handle1').save()
        self.blob_2 = Blob(filename='blob2', user_id='1',
                           handle='handle2').save()
        self.blob_3 = Blob(filename='blob3', user_id='2',
                           handle='handle3').save()

        self.blob_collection = [self.blob_1, self.blob_2, self.blob_3]
예제 #11
0
    def generate_blob_collection(self):
        """Generate a Blob collection.

        Returns:

            user 1 -> blob1, blob2
            user 2 -> blob3

        """
        # NOTE: no real file to avoid using unsupported GridFS mock
        self.blob_1 = Blob(filename="blob1", user_id="1",
                           handle="handle1").save()
        self.blob_2 = Blob(filename="blob2", user_id="1",
                           handle="handle2").save()
        self.blob_3 = Blob(filename="blob3", user_id="2",
                           handle="handle3").save()

        self.blob_collection = [self.blob_1, self.blob_2, self.blob_3]
예제 #12
0
def get_all_by_workspace(workspace, user):
    """ Get all data that belong to the workspace.

    Args:
        workspace:

    Returns:

    """
    return Blob.get_all_by_workspace(workspace)
예제 #13
0
    def create(self, validated_data):
        """Create and return a new `Blob` instance, given the validated data.

        Args:
            validated_data:

        Returns:

        """
        # Create blob
        blob_object = Blob(
            filename=validated_data["blob"].name,
            user_id=str(self.context["request"].user.id),
        )
        # Set file content
        blob_object.blob = validated_data["blob"].file

        # Save the blob
        return blob_api.insert(blob_object, self.context["request"].user)
예제 #14
0
def get_by_id(blob_id, user):
    """ Return blob by its id.

    Args:
        blob_id:

    Returns:

    """
    return Blob.get_by_id(blob_id)
예제 #15
0
    def _retrieve_data(self, request):
        """Retrieve module's data

        Args:
            request:

        Returns:

        """
        data = ""
        self.error = None
        data_xml_entities = XmlEntities()
        if request.method == "GET":
            if "data" in request.GET:
                if len(request.GET["data"]) > 0:
                    data = request.GET["data"]
        elif request.method == "POST":
            try:
                form = BLOBHostForm(request.POST, request.FILES)
                if not form.is_valid():
                    self.error = "No file uploaded."
                    return data

                # get file from request
                uploaded_file = request.FILES["file"]
                # get filename from file
                filename = uploaded_file.name
                # get user id from request
                user_id = str(request.user.id)

                # create blob
                blob = Blob(filename=filename, user_id=user_id)
                # set blob file
                blob.blob = uploaded_file
                # save blob
                blob_api.insert(blob)
                # get download uri
                data = get_blob_download_uri(blob, request)
            except:
                self.error = "An unexpected error occurred."

        return (data_xml_entities.escape_xml_entities(data)
                if AUTO_ESCAPE_XML_ENTITIES else data)
예제 #16
0
def get_all(user):
    """ Return all blobs.

    Args:

    Returns:
        List of Blob instances.

    """
    return Blob.get_all()
예제 #17
0
def get_all_except_user_id(user_id):
    """ Return all blobs except the ones of user.

    Args:
        user_id: User id.

    Returns:
        List of Blob instances except the given user id.

    """
    return Blob.get_all_except_user_id(user_id)
예제 #18
0
def get_all_by_user(user):
    """ Return all blobs by user.

    Args:
        user: User

    Returns:
        List of Blob instances for the given user id.

    """
    return Blob.get_all_by_user_id(str(user.id))
예제 #19
0
    def _retrieve_data(self, request):
        """ Retrieve module's data

        Args:
            request:

        Returns:

        """
        data = ''
        self.error = None
        if request.method == 'GET':
            if 'data' in request.GET:
                if len(request.GET['data']) > 0:
                    data = request.GET['data']
        elif request.method == 'POST':
            try:
                form = BLOBHostForm(request.POST, request.FILES)
                if not form.is_valid():
                    self.error = 'No file uploaded.'
                    return data

                # get file from request
                uploaded_file = request.FILES['file']
                # get filename from file
                filename = uploaded_file.name
                # get user id from request
                user_id = str(request.user.id)

                # create blob
                blob = Blob(filename=filename, user_id=user_id)
                # set blob file
                blob.blob = uploaded_file
                # save blob
                blob_api.insert(blob)
                # get download uri
                data = get_blob_download_uri(blob, request)
            except:
                self.error = 'An unexpected error occurred.'

        return data
예제 #20
0
    def generate_blob_collection(self):
        """Generate a Blob collection.

        Returns:

        """
        blob_1 = Blob(filename="blob1", user_id="1", handle="handle1").save()
        blob_2 = Blob(filename="blob2", user_id="2", handle="handle2").save()
        blob_3 = Blob(
            filename="blob3",
            user_id="1",
            handle="handle3",
            workspace=self.workspace_1.id,
        ).save()
        blob_4 = Blob(
            filename="blob4",
            user_id="2",
            handle="handle4",
            workspace=self.workspace_2.id,
        ).save()
        self.blob_collection = [blob_1, blob_2, blob_3, blob_4]
예제 #21
0
def get_pid_for_blob(blob_id):
    """Retrieve PID matching the blob ID provided.

    Args:
        blob_id:

    Returns:
        str - PID of the blob object
    """
    return local_id_api.get_by_class_and_id(
        record_object_class=get_api_path_from_object(Blob()),
        record_object_id=blob_id,
    )
예제 #22
0
 def test_blob_get_all_return_collection_of_blob(self):
     # Act
     result = Blob.get_all()
     # Assert
     self.assertTrue(all(isinstance(item, Blob) for item in result))
예제 #23
0
 def test_blob_get_by_id_return_blob_if_found(self):
     # Act
     result = Blob.get_by_id(self.fixture.blob_1.id)
     # Assert
     self.assertEqual(result, self.fixture.blob_1)
예제 #24
0
 def test_blob_get_by_id_raises_does_not_exist_error_if_not_found(self):
     # Act # Assert
     with self.assertRaises(exceptions.DoesNotExist):
         Blob.get_by_id(ObjectId())
예제 #25
0
 def test_blob_get_all_return_objects_blob_in_collection(self):
     # Act
     result = Blob.get_all()
     # Assert
     self.assertTrue(len(self.fixture.blob_collection) == result.count())