algoo / preview-generator

generates previews of files with cache management
https://pypi.org/project/preview-generator/
MIT License
228 stars 51 forks source link

Question about basic usage #143

Closed vbroskas closed 4 years ago

vbroskas commented 4 years ago

Apologies since I know this is simple, but I'm not just understanding correctly. I have a simple stream block for letting a user upload multiple files which are then all looped through and displayed in a page template:

class DocumentBlock(blocks.StructBlock):
   # block allowing user to upload multiple images for single page
    title = blocks.CharBlock(required=True, help_text='add your title')

    # create list block
    docs = blocks.ListBlock(
        blocks.StructBlock(
            [

                ('related_document', DocumentChooserBlock(required=True)),
                ('title', blocks.CharBlock(required=True, max_length=100)),
                ('description', blocks.TextBlock(required=True, max_length=200)),

            ]
        )
    )

    class Meta:
        template = 'streams/document_block.html'
        icon = 'placeholder'
        label = 'Document Entries'

In order to implement preview-generator I need the path of the file I want a thumbnail for. However, I only have access to the file url in the template after a file has actually been uploaded. I'm confused about where to put this code:

cache_path = '/tmp/preview_cache'
file_to_preview_path = '/tmp/an_image.png'

manager = PreviewManager(cache_path, create_folder= True)
path_to_preview_image = manager.get_jpeg_preview(file_to_preview_path, width=1000, height=500)

It seem like I need to put it in the template since that is where I have {{doc.related_document.url}}, which can be put here: file_to_preview_path = '/tmp/an_image.png'

Thanks for any guidance you can give!

vbroskas commented 4 years ago

Right now I'm thinking I need to use Django custom template tags to make a function with the preview-generator code inside it. The function will take in the file name which I then use in: file_to_preview_path = f"/tmp/{file_name}.pdf"

vbroskas commented 4 years ago

Django custom template tags worked

lebouquetin commented 4 years ago

Hi @vbroskas could you give a code snippet? It would be interesting for other django developers.

vbroskas commented 4 years ago

The template tag function is working but I'm having an issue with getting the preview image to work.

Preview-generator returns a path to the image, but I need the URL for the image so I can display it in a template. Once I figure out how to do this I'll post some snippets.