Skip to main content

A module for Wagtail that provides functionality similar to wagtail.documents module, but for audio and video files.

Project description

wagtailmedia PyPI Build Status

A module for Wagtail that provides functionality similar to wagtail.documents module, but for audio and video files.

Compatibility

wagtailmedia is compatible with Wagtail 2.2 and above. For compatibility with Wagtail 2.1 and 2.0, use the v0.2.0 release.

How to install

Install using pip:

pip install wagtailmedia

Settings

In your settings file, add wagtailmedia to INSTALLED_APPS:

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

URL configuration

Your project needs to be set up to serve user-uploaded files from MEDIA_ROOT. Your Django project may already have this in place, but if not, add the following snippet to urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Note that this only works in development mode (DEBUG = True); in production, you will need to configure your web server to serve files from MEDIA_ROOT. For further details, see the Django documentation: Serving files uploaded by a user during development and Deploying static files.

With this configuration in place, you are ready to run ./manage.py migrate to create the database tables used by wagtailmedia.

Custom Media model

The Media model can be customised. To do this, you need to add a new model to your project that inherits from wagtailmedia.models.AbstractMedia.

Then set the WAGTAILMEDIA_MEDIA_MODEL setting to point to it:

WAGTAILMEDIA_MEDIA_MODEL = 'mymedia.CustomMedia'

### Hooks

construct_media_chooser_queryset

Called when rendering the media chooser view, to allow the media listing QuerySet to be customised. The callable passed into the hook will receive the current media QuerySet and the request object, and must return a Media QuerySet (either the original one, or a new one).

from wagtail.core import hooks

@hooks.register('construct_media_chooser_queryset')
def show_my_uploaded_media_only(media, request):
    # Only show uploaded media
    media = media.filter(uploaded_by_user=request.user)

    return media

How to use

As a regular Django field

You can use Media as a regular Django field. Here’s an example:

from __future__ import unicode_literals

from django.db import models

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel

from wagtailmedia.edit_handlers import MediaChooserPanel


class BlogPageWithMedia(Page):
    author = models.CharField(max_length=255)
    date = models.DateField("Post date")
    body = RichTextField(blank=False)
    featured_media = models.ForeignKey(
        'wagtailmedia.Media',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    content_panels = Page.content_panels + [
        FieldPanel('author'),
        FieldPanel('date'),
        FieldPanel('body'),
        MediaChooserPanel('featured_media'),
    ]

Name clash with Wagtail

Do not name the field media. When rendering the admin UI, Wagtail uses a media property for its fields’ CSS & JS assets loading. Using media as a field name breaks the admin UI (#54).

In StreamField

You can use Media in StreamField. To do this, you need to add a new block class that inherits from wagtailmedia.blocks.AbstractMediaChooserBlock and implement your own render_basic method.

Here is an example:

from __future__ import unicode_literals

from django.db import models
from django.forms.utils import flatatt
from django.utils.html import format_html, format_html_join

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore import blocks
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel

from wagtailmedia.blocks import AbstractMediaChooserBlock


class TestMediaBlock(AbstractMediaChooserBlock):
    def render_basic(self, value, context=None):
        if not value:
            return ''

        if value.type == 'video':
            player_code = '''
            <div>
                <video width="320" height="240" controls>
                    {0}
                    Your browser does not support the video tag.
                </video>
            </div>
            '''
        else:
            player_code = '''
            <div>
                <audio controls>
                    {0}
                    Your browser does not support the audio element.
                </audio>
            </div>
            '''

        return format_html(player_code, format_html_join(
            '\n', "<source{0}>",
            [[flatatt(s)] for s in value.sources]
        ))


class BlogPage(Page):
    author = models.CharField(max_length=255)
    date = models.DateField("Post date")
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title", icon='title')),
        ('paragraph', blocks.RichTextBlock(icon='pilcrow')),
        ('media', TestMediaBlock(icon='media')),
    ])

    content_panels = Page.content_panels + [
        FieldPanel('author'),
        FieldPanel('date'),
        StreamFieldPanel('body'),
    ]

Contributing

Install

To make changes to this project, first clone this repository:

git clone git@github.com:torchbox/wagtailmedia.git
cd wagtailmedia

With your preferred virtualenv activated, install testing dependencies:

pip install -e .[testing] -U

How to run tests

Now you can run tests as shown below:

python runtests.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtailmedia-0.4.0.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtailmedia-0.4.0-py2.py3-none-any.whl (43.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file wagtailmedia-0.4.0.tar.gz.

File metadata

  • Download URL: wagtailmedia-0.4.0.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.3

File hashes

Hashes for wagtailmedia-0.4.0.tar.gz
Algorithm Hash digest
SHA256 b631af0e67c339e213b273fba53cc82754400feef2887bd662c48f128b8fa28d
MD5 8612f49dfd457d0403ba4fce1a1e69e9
BLAKE2b-256 0d239b79dd6ddd24f0bcb372906f4dc68289171f73a399905c754f4c3087ef69

See more details on using hashes here.

File details

Details for the file wagtailmedia-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: wagtailmedia-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 43.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.3

File hashes

Hashes for wagtailmedia-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4ec2e17d0fd1f6211560ee0fd0eda8a6f71f7cbc2868bed7aca07c9bd8aa8abf
MD5 3c22d4728e6812399d5ca51edfb95b99
BLAKE2b-256 97e3bbdfd57d992c0f1d87b7f0285660dafe37ae48dc8c446feae071b935fca0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page