Skip to content

Plugin

VideoPlugin

Bases: SingletonPlugin

Resource view for embedding videos (youtube/vimeo)

Source code in ckanext/video/plugin.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class VideoPlugin(SingletonPlugin):
    """
    Resource view for embedding videos (youtube/vimeo)
    """

    implements(interfaces.IConfigurer, inherit=True)
    implements(interfaces.IResourceView, inherit=True)
    implements(interfaces.IPackageController, inherit=True)

    def update_config(self, config):
        toolkit.add_template_directory(config, 'theme/templates')

    def info(self):
        return {
            'name': 'video',
            'title': 'Embedded video',
            'schema': {
                'video_url': [ignore_empty, str, is_valid_video_url],
                'width': [not_empty, is_positive_integer],
                'height': [not_empty, is_positive_integer],
            },
            'iframed': False,
            'icon': 'film',
        }

    def can_view(self, data_dict):
        return True

    def view_template(self, context, data_dict):
        return 'video_view.html'

    def form_template(self, context, data_dict):
        return 'video_form.html'

    def setup_template_variables(self, context, data_dict):
        """
        Setup variables available to templates.

        :param context:
        :param data_dict:
        """
        video_url = data_dict['resource_view'].get('video_url') or data_dict[
            'resource'
        ].get('url')

        # Is this a youtube video?
        if 'youtube.com' in video_url:
            # If this is a youtube link, replace with a URL for embeddable video
            match = re.search(
                video_provider_patterns['youtube_link'], video_url, re.IGNORECASE
            )
            if match:
                video_url = f'https://www.youtube.com/embed/{match.group(1)}'

        # TODO - More video provider types

        return {'defaults': {'width': 480, 'height': 390}, 'video_url': video_url}

setup_template_variables(context, data_dict)

Setup variables available to templates.

Parameters:

Name Type Description Default
context
required
data_dict
required
Source code in ckanext/video/plugin.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def setup_template_variables(self, context, data_dict):
    """
    Setup variables available to templates.

    :param context:
    :param data_dict:
    """
    video_url = data_dict['resource_view'].get('video_url') or data_dict[
        'resource'
    ].get('url')

    # Is this a youtube video?
    if 'youtube.com' in video_url:
        # If this is a youtube link, replace with a URL for embeddable video
        match = re.search(
            video_provider_patterns['youtube_link'], video_url, re.IGNORECASE
        )
        if match:
            video_url = f'https://www.youtube.com/embed/{match.group(1)}'

    # TODO - More video provider types

    return {'defaults': {'width': 480, 'height': 390}, 'video_url': video_url}