예제 #1
0
def test_user_file_path(data_fixture):
    handler = UserFileHandler()
    assert handler.user_file_path("test.jpg") == "user_files/test.jpg"
    assert handler.user_file_path("another_file.png") == "user_files/another_file.png"

    user_file = data_fixture.create_user_file()
    assert handler.user_file_path(user_file) == f"user_files/{user_file.name}"
예제 #2
0
def test_user_file_path(data_fixture):
    handler = UserFileHandler()
    assert handler.user_file_path('test.jpg') == 'user_files/test.jpg'
    assert handler.user_file_path('another_file.png') == 'user_files/another_file.png'

    user_file = data_fixture.create_user_file()
    assert handler.user_file_path(user_file) == f'user_files/{user_file.name}'
예제 #3
0
    def get_export_serialized_value(self, row, field_name, cache, files_zip,
                                    storage):
        file_names = []
        user_file_handler = UserFileHandler()

        for file in getattr(row, field_name):
            # Check if the user file object is already in the cache and if not,
            # it must be fetched and added to to it.
            cache_entry = f"user_file_{file['name']}"
            if cache_entry not in cache:
                try:
                    user_file = UserFile.objects.all().name(file["name"]).get()
                except UserFile.DoesNotExist:
                    continue

                if file["name"] not in files_zip.namelist():
                    # Load the user file from the content and write it to the zip file
                    # because it might not exist in the environment that it is going
                    # to be imported in.
                    file_path = user_file_handler.user_file_path(
                        user_file.name)
                    with storage.open(file_path, mode="rb") as storage_file:
                        files_zip.writestr(file["name"], storage_file.read())

                cache[cache_entry] = user_file

            file_names.append({
                "name":
                file["name"],
                "visible_name":
                file["visible_name"],
                "original_name":
                cache[cache_entry].original_name,
            })
        return file_names
    def handle(self, *args, **options):
        """
        Regenerates the thumbnails of all image user files. If the USER_THUMBNAILS
        setting ever changes then this file can be used to fix all the thumbnails.
        """

        i = 0
        handler = UserFileHandler()
        buffer_size = 100
        queryset = UserFile.objects.filter(is_image=True)
        count = queryset.count()

        while i < count:
            user_files = queryset[i:min(count, i + buffer_size)]
            for user_file in user_files:
                i += 1

                full_path = handler.user_file_path(user_file)
                stream = default_storage.open(full_path)

                try:
                    image = Image.open(stream)
                    handler.generate_and_save_image_thumbnails(
                        image, user_file, storage=default_storage)
                    image.close()
                except IOError:
                    pass

                stream.close()

        self.stdout.write(
            self.style.SUCCESS(f"{i} thumbnails have been regenerated."))