Giter VIP home page Giter VIP logo

wagtail-filepreviews's Introduction

wagtail-filepreviews

image

image

Extend Wagtail's Documents with image previews and metadata from FilePreviews.io

Installing

Install with pip:

$ pip install wagtaildocs_previews

Settings

In your settings file, add wagtaildocs_previews to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'wagtaildocs_previews',
    # ...
]

You'll also need to set WAGTAILDOCS_DOCUMENT_MODEL.

WAGTAILDOCS_DOCUMENT_MODEL = 'wagtaildocs_previews.PreviewableDocument'

URL configuration

from wagtaildocs_previews import urls as wagtaildocs_urls

urlpatterns = [
    # ...
    url(r'^documents/', include(wagtaildocs_urls)),
    # ...
]

FilePreviews.io API Keys

For previews to be generated for your documents, you'll need to have a FilePreviews.io account and an application's credentials. Once you have the credentials, add them under the FilePreviews settings in your Wagtail admin.

Usage

Since we're extending via WAGTAILDOCS_DOCUMENT_MODEL you should be using get_document_model() to reference the correct Document model.

from wagtail.core.models import Page
from wagtail.documents.models import get_document_model
from wagtail.documents.edit_handlers import DocumentChooserPanel


class BookPage(Page):
    book_file = models.ForeignKey(
        get_document_model(),
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    content_panels = Page.content_panels + [
        DocumentChooserPanel('book_file'),
    ]

In your template now you'll be able to access the preview_data field.

{% extends "base.html" %}
{% load wagtailcore_tags %}

{% block body_class %}resource-page{% endblock %}

{% block content %}
    <h1>Book</h>
    <h2>{{ page.book_file.title }}</h2>
    <img src="{{ page.book_file.preview_data.preview.url|default:'http://placehold.it/300x300' }}" alt="">
{% endblock %}

Configuring thumbnail sizes and other options

By default, image previews and not resized at all. If you want to specify additional FilePreviews options like thumbnail size or metadata, specify the WAGTAILDOCS_PREVIEWS_OPTIONS_CALLBACK option in your settings.

WAGTAILDOCS_PREVIEWS_OPTIONS_CALLBACK='mysite.utils.get_preview_options'

In the mysite.utils module, create a get_preview_options method.

def get_preview_options(document):
    return {
        'sizes': [300],
        'metadata': ['ocr']
    }

Bonus: Coding video session

Here’s a coding session video building up to the released package.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.