Exemple #1
0
    def set_import_serialized_value(self, row, field_name, value, id_mapping,
                                    files_zip, storage):
        user_file_handler = UserFileHandler()
        files = []

        for file in value:
            with files_zip.open(file["name"]) as stream:
                # Try to upload the user file with the original name to make sure
                # that if the was already uploaded, it will not be uploaded again.
                user_file = user_file_handler.upload_user_file(
                    None, file["original_name"], stream, storage=storage)

            value = user_file.serialize()
            value["visible_name"] = file["visible_name"]
            files.append(value)

        setattr(row, field_name, files)
Exemple #2
0
def test_upload_user_file(data_fixture, tmpdir):
    user = data_fixture.create_user()

    storage = FileSystemStorage(location=str(tmpdir), base_url="http://localhost")
    handler = UserFileHandler()

    with pytest.raises(InvalidFileStreamError):
        handler.upload_user_file(user, "test.txt", "NOT A STREAM!", storage=storage)

    with pytest.raises(InvalidFileStreamError):
        handler.upload_user_file(user, "test.txt", None, storage=storage)

    old_limit = settings.USER_FILE_SIZE_LIMIT
    settings.USER_FILE_SIZE_LIMIT = 6
    with pytest.raises(FileSizeTooLargeError):
        handler.upload_user_file(user, "test.txt", ContentFile(b"Hello World"))
    settings.USER_FILE_SIZE_LIMIT = old_limit

    with freeze_time("2020-01-01 12:00"):
        user_file = handler.upload_user_file(
            user, "test.txt", ContentFile(b"Hello World"), storage=storage
        )

    assert user_file.original_name == "test.txt"
    assert user_file.original_extension == "txt"
    assert len(user_file.unique) == 32
    assert user_file.size == 11
    assert user_file.mime_type == "text/plain"
    assert user_file.uploaded_by_id == user.id
    assert user_file.uploaded_at.year == 2020
    assert user_file.uploaded_at.month == 1
    assert user_file.uploaded_at.day == 1
    assert user_file.is_image is False
    assert user_file.image_width is None
    assert user_file.image_height is None
    assert user_file.sha256_hash == (
        "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"
    )
    file_path = tmpdir.join("user_files", user_file.name)
    assert file_path.isfile()
    assert file_path.open().read() == "Hello World"

    user_file = handler.upload_user_file(
        user, "another.txt", BytesIO(b"Hello"), storage=storage
    )
    assert user_file.original_name == "another.txt"
    assert user_file.original_extension == "txt"
    assert user_file.mime_type == "text/plain"
    assert user_file.size == 5
    assert user_file.sha256_hash == (
        "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"
    )
    file_path = tmpdir.join("user_files", user_file.name)
    assert file_path.isfile()
    assert file_path.open().read() == "Hello"

    assert (
        handler.upload_user_file(
            user, "another.txt", ContentFile(b"Hello"), storage=storage
        ).id
        == user_file.id
    )
    assert (
        handler.upload_user_file(
            user, "another_name.txt", ContentFile(b"Hello"), storage=storage
        ).id
        != user_file.id
    )

    image = Image.new("RGB", (100, 140), color="red")
    image_bytes = BytesIO()
    image.save(image_bytes, format="PNG")

    user_file = handler.upload_user_file(
        user, "some image.png", image_bytes, storage=storage
    )
    assert user_file.mime_type == "image/png"
    assert user_file.is_image is True
    assert user_file.image_width == 100
    assert user_file.image_height == 140
    file_path = tmpdir.join("user_files", user_file.name)
    assert file_path.isfile()
    file_path = tmpdir.join("thumbnails", "tiny", user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open("rb"))
    assert thumbnail.height == 21
    assert thumbnail.width == 21

    old_thumbnail_settings = settings.USER_THUMBNAILS
    settings.USER_THUMBNAILS = {"tiny": [None, 100]}
    image = Image.new("RGB", (1920, 1080), color="red")
    image_bytes = BytesIO()
    image.save(image_bytes, format="PNG")
    user_file = handler.upload_user_file(user, "red.png", image_bytes, storage=storage)
    file_path = tmpdir.join("thumbnails", "tiny", user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open("rb"))
    assert thumbnail.width == 178
    assert thumbnail.height == 100

    image = Image.new("RGB", (400, 400), color="red")
    image_bytes = BytesIO()
    image.save(image_bytes, format="PNG")
    user_file = handler.upload_user_file(user, "red2.png", image_bytes, storage=storage)
    file_path = tmpdir.join("thumbnails", "tiny", user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open("rb"))
    assert thumbnail.width == 100
    assert thumbnail.height == 100

    settings.USER_THUMBNAILS = {"tiny": [21, None]}
    image = Image.new("RGB", (1400, 1000), color="red")
    image_bytes = BytesIO()
    image.save(image_bytes, format="PNG")
    user_file = handler.upload_user_file(user, "red3.png", image_bytes, storage=storage)
    file_path = tmpdir.join("thumbnails", "tiny", user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open("rb"))
    assert thumbnail.width == 21
    assert thumbnail.height == 15
    settings.USER_THUMBNAILS = old_thumbnail_settings

    assert UserFile.objects.all().count() == 7

    image = Image.new("RGB", (1, 1), color="red")
    image_bytes = BytesIO()
    image.save(image_bytes, format="PNG")
    user_file = handler.upload_user_file(
        user,
        "this_file_has_an_extreme_long_file_name_that_should_not_make_the_system_"
        "fail_hard_when_trying_to_upload.png",
        image_bytes,
        storage=storage,
    )

    assert (
        user_file.original_name == "this_file_has_an_extreme_long_f...hard_when_"
        "trying_to_upload.png"
    )
Exemple #3
0
def test_upload_user_file(data_fixture, tmpdir):
    user = data_fixture.create_user()

    storage = FileSystemStorage(location=str(tmpdir), base_url='http://localhost')
    handler = UserFileHandler()

    with pytest.raises(InvalidFileStreamError):
        handler.upload_user_file(
            user,
            'test.txt',
            'NOT A STREAM!',
            storage=storage
        )

    with pytest.raises(InvalidFileStreamError):
        handler.upload_user_file(
            user,
            'test.txt',
            None,
            storage=storage
        )

    old_limit = settings.USER_FILE_SIZE_LIMIT
    settings.USER_FILE_SIZE_LIMIT = 6
    with pytest.raises(FileSizeTooLargeError):
        handler.upload_user_file(
            user,
            'test.txt',
            ContentFile(b'Hello World')
        )
    settings.USER_FILE_SIZE_LIMIT = old_limit

    with freeze_time('2020-01-01 12:00'):
        user_file = handler.upload_user_file(
            user,
            'test.txt',
            ContentFile(b'Hello World'),
            storage=storage
        )

    assert user_file.original_name == 'test.txt'
    assert user_file.original_extension == 'txt'
    assert len(user_file.unique) == 32
    assert user_file.size == 11
    assert user_file.mime_type == 'text/plain'
    assert user_file.uploaded_by_id == user.id
    assert user_file.uploaded_at.year == 2020
    assert user_file.uploaded_at.month == 1
    assert user_file.uploaded_at.day == 1
    assert user_file.is_image is False
    assert user_file.image_width is None
    assert user_file.image_height is None
    assert user_file.sha256_hash == (
        'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e'
    )
    file_path = tmpdir.join('user_files', user_file.name)
    assert file_path.isfile()
    assert file_path.open().read() == 'Hello World'

    user_file = handler.upload_user_file(
        user,
        'another.txt',
        BytesIO(b'Hello'),
        storage=storage
    )
    assert user_file.original_name == 'another.txt'
    assert user_file.original_extension == 'txt'
    assert user_file.mime_type == 'text/plain'
    assert user_file.size == 5
    assert user_file.sha256_hash == (
        '185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969'
    )
    file_path = tmpdir.join('user_files', user_file.name)
    assert file_path.isfile()
    assert file_path.open().read() == 'Hello'

    assert (
        handler.upload_user_file(
            user,
            'another.txt',
            ContentFile(b'Hello'),
            storage=storage
        ).id == user_file.id
    )
    assert handler.upload_user_file(
        user,
        'another_name.txt',
        ContentFile(b'Hello'),
        storage=storage
    ).id != user_file.id

    image = Image.new('RGB', (100, 140), color='red')
    image_bytes = BytesIO()
    image.save(image_bytes, format='PNG')

    user_file = handler.upload_user_file(
        user,
        'some image.png',
        image_bytes,
        storage=storage
    )
    assert user_file.mime_type == 'image/png'
    assert user_file.is_image is True
    assert user_file.image_width == 100
    assert user_file.image_height == 140
    file_path = tmpdir.join('user_files', user_file.name)
    assert file_path.isfile()
    file_path = tmpdir.join('thumbnails', 'tiny', user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open('rb'))
    assert thumbnail.height == 21
    assert thumbnail.width == 21

    old_thumbnail_settings = settings.USER_THUMBNAILS
    settings.USER_THUMBNAILS = {'tiny': [None, 100]}
    image = Image.new('RGB', (1920, 1080), color='red')
    image_bytes = BytesIO()
    image.save(image_bytes, format='PNG')
    user_file = handler.upload_user_file(
        user,
        'red.png',
        image_bytes,
        storage=storage
    )
    file_path = tmpdir.join('thumbnails', 'tiny', user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open('rb'))
    assert thumbnail.width == 178
    assert thumbnail.height == 100

    image = Image.new('RGB', (400, 400), color='red')
    image_bytes = BytesIO()
    image.save(image_bytes, format='PNG')
    user_file = handler.upload_user_file(
        user,
        'red2.png',
        image_bytes,
        storage=storage
    )
    file_path = tmpdir.join('thumbnails', 'tiny', user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open('rb'))
    assert thumbnail.width == 100
    assert thumbnail.height == 100

    settings.USER_THUMBNAILS = {'tiny': [21, None]}
    image = Image.new('RGB', (1400, 1000), color='red')
    image_bytes = BytesIO()
    image.save(image_bytes, format='PNG')
    user_file = handler.upload_user_file(
        user,
        'red3.png',
        image_bytes,
        storage=storage
    )
    file_path = tmpdir.join('thumbnails', 'tiny', user_file.name)
    assert file_path.isfile()
    thumbnail = Image.open(file_path.open('rb'))
    assert thumbnail.width == 21
    assert thumbnail.height == 15
    settings.USER_THUMBNAILS = old_thumbnail_settings

    assert UserFile.objects.all().count() == 7

    image = Image.new('RGB', (1, 1), color='red')
    image_bytes = BytesIO()
    image.save(image_bytes, format='PNG')
    user_file = handler.upload_user_file(
        user,
        'this_file_has_an_extreme_long_file_name_that_should_not_make_the_system_'
        'fail_hard_when_trying_to_upload.png',
        image_bytes,
        storage=storage
    )

    assert user_file.original_name == 'this_file_has_an_extreme_long_f...hard_when_' \
                                      'trying_to_upload.png'
Exemple #4
0
def test_import_export_file_field(data_fixture, tmpdir,
                                  user_tables_in_separate_db):
    user = data_fixture.create_user()
    imported_group = data_fixture.create_group(user=user)
    database = data_fixture.create_database_application(user=user)
    table = data_fixture.create_database_table(database=database)
    field = data_fixture.create_file_field(table=table, name="File")

    storage = FileSystemStorage(location=str(tmpdir),
                                base_url="http://localhost")
    handler = UserFileHandler()
    user_file = handler.upload_user_file(user,
                                         "test.txt",
                                         ContentFile(b"Hello World"),
                                         storage=storage)

    core_handler = CoreHandler()
    row_handler = RowHandler()
    model = table.get_model()

    row_1 = row_handler.create_row(
        user=user,
        table=table,
        values={
            f"field_{field.id}": [{
                "name": user_file.name,
                "visible_name": "a.txt"
            }]
        },
        model=model,
    )
    row_2 = row_handler.create_row(
        user=user,
        table=table,
        values={},
        model=model,
    )
    row_3 = row_handler.create_row(
        user=user,
        table=table,
        values={f"field_{field.id}": [{
            "name": user_file.name
        }]},
        model=model,
    )

    files_buffer = BytesIO()
    exported_applications = core_handler.export_group_applications(
        database.group, files_buffer=files_buffer, storage=storage)

    # We expect that the exported zip file contains the user file used in the created
    # rows.
    with ZipFile(files_buffer, "r", ZIP_DEFLATED, False) as zip_file:
        assert zip_file.read(user_file.name) == b"Hello World"

    assert (exported_applications[0]["tables"][0]["rows"][0]
            [f"field_{field.id}"][0]["name"] == user_file.name)
    assert (
        exported_applications[0]["tables"][0]["rows"][0][f"field_{field.id}"]
        [0]["original_name"] == user_file.original_name)
    assert exported_applications[0]["tables"][0]["rows"][1][
        f"field_{field.id}"] == []
    assert (exported_applications[0]["tables"][0]["rows"][2]
            [f"field_{field.id}"][0]["name"] == user_file.name)

    # Change the original name for enforce that the file is re-uploaded when saved.
    exported_applications[0]["tables"][0]["rows"][0][f"field_{field.id}"][0][
        "original_name"] = "test2.txt"
    exported_applications[0]["tables"][0]["rows"][2][f"field_{field.id}"][0][
        "original_name"] = "test2.txt"

    imported_applications, id_mapping = core_handler.import_applications_to_group(
        imported_group, exported_applications, files_buffer, storage)
    imported_database = imported_applications[0]
    imported_tables = imported_database.table_set.all()
    imported_table = imported_tables[0]
    imported_field = imported_table.field_set.all().first().specific
    imported_user_file = UserFile.objects.all()[1]

    import_row_1 = row_handler.get_row(user=user,
                                       table=imported_table,
                                       row_id=row_1.id)
    import_row_2 = row_handler.get_row(user=user,
                                       table=imported_table,
                                       row_id=row_2.id)
    import_row_3 = row_handler.get_row(user=user,
                                       table=imported_table,
                                       row_id=row_3.id)

    assert len(getattr(import_row_1, f"field_{imported_field.id}")) == 1
    assert (getattr(
        import_row_1,
        f"field_{imported_field.id}")[0]["name"] == imported_user_file.name)
    assert (getattr(
        import_row_1,
        f"field_{imported_field.id}")[0]["visible_name"] == "a.txt")
    assert len(getattr(import_row_2, f"field_{imported_field.id}")) == 0
    assert len(getattr(import_row_3, f"field_{imported_field.id}")) == 1
    assert (getattr(
        import_row_3,
        f"field_{imported_field.id}")[0]["name"] == imported_user_file.name)
    assert (getattr(
        import_row_3,
        f"field_{imported_field.id}")[0]["visible_name"] == "test.txt")

    assert UserFile.objects.all().count() == 2
    assert user_file.name != imported_user_file.name
    file_path = tmpdir.join("user_files", imported_user_file.name)
    assert file_path.isfile()
    assert file_path.open().read() == "Hello World"