Exemplo n.º 1
0
class Gist(BaseOembedRich):
    """
    Post type to include a code snippet from GitHub's Gist service:
    https://gist.github.com/
    """
    gist_url = OEmbedURLField(_('Gist URL'), max_length=1024)
    git_user = models.CharField(_('Author\'s Name'), max_length=512, \
        editable=False)
    git_user_url = models.URLField(_('Author\'s Twitter Profile'), \
        max_length=512, editable=False)
    gist_title = models.CharField(_('Author\'s Name'), max_length=512, \
        editable=False)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'Gist'
        verbose_name_plural = 'Gists'

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'gist_url',
                    'caption',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
        ]

    oembed_endpoint = 'https://github.com/api/oembed'
    oembed_schema = [
        'https://gist.github.com/*',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        (
            'url',
            'gist_url',
        ),
        (
            'author_url',
            'git_user_url',
        ),
        (
            'author_name',
            'git_user',
        ),
        (
            'title',
            'gist_title',
        ),
        ('html', 'embed'),
    )

    @property
    def oembed_resource(self):
        return self.gist_url

    @property
    def gist_id(self):
        """
        Returns GitHub's ID for the Gist.

        This method is necessarily fragile, as Gist's oEmbed response does not
        return this value.
        """
        return int(re.search(r'(\d+)$', self.gist_url).groups()[0])

    @property
    def javascript_embed(self):
        """
        Uses the Gist embed code (which is not what is returned by the oEmbed
        call) to display the Gist, using the naked data returned by oEmbed <aside>
        a noscript fallback.
        """
        return render_to_string('tumblelog/gist_javascript_embed.html', {
            'id': self.gist_id,
            'noscript': self.embed,
        })
Exemplo n.º 2
0
class Vimeo(BaseOembedVideoWithThumbnail):
    """
    Post type for an a Vimeo video, with metadata and embed code pulled
    directly from the source using Vimeo's oEmbed API:

    https://vimeo.com/api/docs/oembed
    """
    vimeo_url = OEmbedURLField(_('Vimeo URL'), max_length=1024)
    vimeo_user = models.CharField(_('Uploader\'s Username'), max_length=512, \
        null=True, blank=True, editable=False)
    vimeo_user_url = models.URLField(_('Uploader\'s Vimeo Profile'), \
        max_length=1024, null=True, blank=True, editable=False)
    vimeo_title = models.CharField(_('Vimeo Title'), max_length=512, \
        null=True, blank=True, editable=False)
    vimeo_video_id = models.IntegerField(_('Vimeo ID'), max_length=9, \
        null=True, blank=True, editable=False)
    duration = models.IntegerField(_('Duration'), max_length=6, null=True, \
        blank=True, editable=False)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'Vimeo Video'
        verbose_name_plural = 'Vimeo Videos'

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'vimeo_url',
                    'caption',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
        ]

    oembed_endpoint = 'http://vimeo.com/api/oembed.json'
    oembed_schema = [
        'http://www.vimeo.com/groups/*/videos/*',
        'http://www.vimeo.com/*',
        'http://vimeo.com/groups/*/videos/*',
        'http://vimeo.com/*',
        'http://vimeo.com/m/#/*',
        'https://www.vimeo.com/groups/*/videos/*',
        'https://www.vimeo.com/*',
        'https://vimeo.com/groups/*/videos/*',
        'https://vimeo.com/*',
        'https://vimeo.com/m/#/*',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        'width',
        'height',
        (
            'author_name',
            'vimeo_user',
        ),
        (
            'author_url',
            'vimeo_user_url',
        ),
        (
            'title',
            'vimeo_title',
        ),
        (
            'video_id',
            'vimeo_video_id',
        ),
        (
            'html',
            'embed',
        ),
        'thumbnail_url',
        'thumbnail_width',
        'thumbnail_height',
        'duration',
    )

    @property
    def oembed_resource(self):
        return self.vimeo_url
Exemplo n.º 3
0
class YouTube(BaseOembedVideoWithThumbnail):
    """
    Post type for an a YouTube video, with metadata and embed code pulled
    directly from the source using YouTube's oEmbed API:

    http://apiblog.youtube.com/2009/10/oembed-support.html
    """
    youtube_url = OEmbedURLField(_('YouTube URL'), max_length=1024)
    youtube_user = models.CharField(_('Uploader\'s Username'), null=True, \
        blank=True, max_length=512, editable=False)
    youtube_user_url = models.URLField(_('Uploader\'s YouTube Channel'), \
        null=True, blank=True, max_length=1024, editable=False)
    youtube_title = models.CharField(_('YouTube Title'), max_length=512, \
        null=True, blank=True, editable=False)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'YouTube Video'
        verbose_name_plural = 'YouTube Videos'

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'youtube_url',
                    'caption',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
        ]

    oembed_endpoint = 'http://www.youtube.com/oembed'
    oembed_schema = [
        'http://*youtube.com/watch*', 'http://*.youtube.com/v/*',
        'https://*youtube.com/watch*', 'https://*.youtube.com/v/*',
        'http://youtu.be/*', 'http://*.youtube.com/user/*',
        'http://*.youtube.com/*#*/*', 'http://m.youtube.com/watch*',
        'http://m.youtube.com/index*', 'http://*.youtube.com/profile*',
        'http://*.youtube.com/view_play_list*',
        'http://*.youtube.com/playlist*'
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        'width',
        'height',
        (
            'author_name',
            'youtube_user',
        ),
        (
            'author_url',
            'youtube_user_url',
        ),
        (
            'title',
            'youtube_title',
        ),
        (
            'html',
            'embed',
        ),
        'thumbnail_url',
        'thumbnail_width',
        'thumbnail_height',
    )

    @property
    def oembed_resource(self):
        return self.youtube_url
Exemplo n.º 4
0
class SoundCloud(BaseOembedRich):
    """

    """
    soundcloud_url = OEmbedURLField(
        _('Soundcloud URL'),
        max_length=1024,
        help_text=_('Can be the URL of an track, set, group, or user'))
    soundcloud_title = models.CharField(_('Soundcloud Title'),
                                        max_length=512,
                                        null=True,
                                        blank=True,
                                        editable=False)
    soundcloud_description = models.CharField(_('Soundcloud Description'),
                                              max_length=8192,
                                              null=True,
                                              blank=True,
                                              editable=False)
    maxwidth = models.IntegerField(
        _('Width'),
        max_length=4,
        null=True,
        blank=True,
        help_text=_('Measured in pixels'),
    )
    maxheight = models.CharField(
        _('Height'),
        max_length=3,
        choices=SOUNDCLOUD_HEIGHT_CHOICES,
        blank=True,
        null=True,
        help_text=_('Only valid for tracks'),
    )
    color = models.CharField(
        _('Color'),
        max_length=6,
        default=SOUNDCLOUD_COLOR,
        blank=True,
        null=True,
        help_text=_(
            'The primary color of the widget as a hex triplet, e.g. "ff0066"'),
    )
    auto_play = models.BooleanField(_('Autoplay on load?'), default=False)
    show_comments = models.BooleanField(
        _('Show SoundCloud\'s timed comments?'), default=False)
    html5_player = models.BooleanField(_('Use the HTML5 player?'),
                                       default=True)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'Soundcloud'
        verbose_name_plural = 'Soundcloud Players'

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'soundcloud_url',
                    'caption',
                )
            }),
            ('Settings', {
                'classes': ('collapse', ),
                'fields': (
                    (
                        'maxwidth',
                        'maxheight',
                    ),
                    'color',
                    'auto_play',
                    'show_comments',
                    'html5_player',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
            'soundcloud_title',
            'soundcloud_description',
        ]

    oembed_endpoint = 'http://soundcloud.com/oembed'
    oembed_schema = [
        'http://soundcloud.com/*',
        'http://snd.sc/*',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        'width',
        'height',
        (
            'title',
            'soundcloud_title',
        ),
        (
            'description',
            'soundcloud_description',
        ),
        (
            'title',
            'soundcloud_title',
        ),
        (
            'html',
            'embed',
        ),
    )

    @property
    def oembed_resource(self):
        return self.soundcloud_url

    @property
    def oembed_endpoint_params(self):
        params = {
            'auto_play': str(self.auto_play).lower(),
            'show_comments': str(self.show_comments).lower(),
            'iframe': str(self.html5_player).lower(),
        }
        if self.maxwidth:
            params['maxwidth'] = self.maxwidth
        if self.maxheight:
            params['maxheight'] = self.maxheight
        if self.color:
            params['color'] = self.color
        return params

    def oembed_clean_value(self, key, value):
        if key == 'width' and value == '100%':
            return None
        return value
Exemplo n.º 5
0
class Rdio(BaseOembedRich):
    """

    """
    rdio_url = OEmbedURLField(
        _('Rdio URL'),
        max_length=1024,
        help_text=_('Can be the URL of an album, track, or playlist'))
    rdio_title = models.CharField(_('Rdio Title'),
                                  max_length=512,
                                  null=True,
                                  blank=True,
                                  editable=False)
    thumbnail_url = models.URLField(_('Thumbnail'), max_length=1024, \
        null=True, blank=True, editable=False)
    thumbnail_width = models.IntegerField(_('Thumbnail Width'), null=True, \
        blank=True, editable=False)
    thumbnail_height = models.IntegerField(_('Thumbnail Height'), null=True, \
        blank=True, editable=False)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'Rdio'
        verbose_name_plural = 'Rdio Players'

    class TumblelogMeta:
        date_hierarchy = 'date_published'
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        search_fields = [
            'title',
            'slug',
            'caption',
        ]
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'rdio_url',
                    'caption',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        prepopulated_fields = {'slug': ('title', )}

    oembed_endpoint = 'http://www.rdio.com/api/oembed/'
    oembed_schema = [
        'http://www.rdio.com/*',
        'http://rd.io/x/*',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        'width',
        'height',
        (
            'title',
            'rdio_title',
        ),
        (
            'html',
            'embed',
        ),
        'thumbnail_url',
        'thumbnail_width',
        'thumbnail_height',
    )

    @property
    def oembed_resource(self):
        return self.rdio_url

    @property
    def thumbnail(self):
        return {
            'url': self.thumbnail_url,
            'width': self.thumbnail_width,
            'height': self.thumbnail_height,
        }
Exemplo n.º 6
0
class Flickr(BaseOembedPhoto):
    """
    Post type for an image from Flickr, with metadata pulled directly from the
    source using Flickr's oEmbed API.
    """
    flickr_url = OEmbedURLField(_('Flickr URL'), max_length=1024)
    flickr_user = models.CharField(_('Photographer\'s Name'),
                                   max_length=512,
                                   editable=False)
    flickr_user_url = models.URLField(_('Photographer\'s Flickr Stream'),
                                      null=True,
                                      blank=True,
                                      max_length=1024,
                                      editable=False)
    flickr_title = models.CharField(_('Flickr Title'),
                                    max_length=512,
                                    editable=False)
    size = models.CharField(_('Size'),
                            max_length=4,
                            default=FLICKR_WIDTH,
                            choices=FLICKR_SIZE_CHOICES)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'Flickr Photo'
        verbose_name_plural = 'Flickr Photos'

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'flickr_url',
                    'size',
                    'caption',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
        ]

    oembed_endpoint = 'http://www.flickr.com/services/oembed'
    oembed_schema = [
        'http://www.flickr.com/photos/*',
        'http://flic.kr/*',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        'width',
        'height',
        (
            'url',
            'image_url',
        ),
        (
            'author_name',
            'flickr_user',
        ),
        (
            'author_url',
            'flickr_user_url',
        ),
        (
            'title',
            'flickr_title',
        ),
    )

    @property
    def oembed_resource(self):
        return self.flickr_url

    @property
    def oembed_endpoint_params(self):
        return {
            'maxwidth': self.size,
            'maxheight': self.size,
        }
Exemplo n.º 7
0
class Instagram(BaseOembedPhoto):
    """
    Post type for an image from Instagram, with metadata pulled directly from
    the source using Instagram's oEmbed API.
    """
    instagram_url = OEmbedURLField(_('Instagram URL'), max_length=1024)
    instagram_user = models.CharField(
        _('Photographer\'s Name'),
        max_length=512,
        editable=False,
    )
    instagram_title = models.CharField(
        _('Instagram Title'),
        max_length=512,
        editable=False,
    )

    class Meta:
        app_label = 'tumblelog'
        verbose_name = 'Instagram Photo'
        verbose_name_plural = 'Instagram Photos'

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'instagram_url',
                    'caption',
                )
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
        ]

    oembed_endpoint = 'http://api.instagram.com/oembed'
    oembed_schema = [
        'http://instagr.am/p/*',
        'http://instagram.com/p/*/',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        'width',
        'height',
        (
            'url',
            'image_url',
        ),
        (
            'author_name',
            'instagram_user',
        ),
        (
            'title',
            'instagram_title',
        ),
    )

    @property
    def oembed_resource(self):
        return self.instagram_url
Exemplo n.º 8
0
class Tweet(BaseOembedRich):
    """
    Post type for an a Tweet, with metadata and embed code pulled directly from
    the source using Twitter's oEmbed API:

    https://dev.twitter.com/docs/api/1/get/statuses/oembed
    """
    tweet_url = OEmbedURLField(_('Tweet URL'), max_length=1024)
    hide_media = models.BooleanField(_('Hide media?'), default=False)
    hide_thread = models.BooleanField(_('Hide thread?'), default=False)
    maxwidth = models.IntegerField(_('Maximum Width'),
                                   max_length=3,
                                   default=TWITTER_WIDTH)
    language = models.CharField(_('Language'),
                                default=TWITTER_LANGUAGE,
                                max_length=2,
                                choices=TWITTER_LANGUAGE_CHOICES)
    twitter_user = models.CharField(_('Author\'s Name'),
                                    max_length=512,
                                    editable=False)
    twitter_user_url = models.URLField(_('Author\'s Twitter Profile'),
                                       max_length=512,
                                       editable=False)

    class Meta:
        app_label = 'tumblelog'
        verbose_name = _('Tweet')
        verbose_name_plural = _('Tweets')

    class TumblelogMeta:
        actions = [
            actions.mark_as_published,
            actions.mark_as_draft,
        ]
        date_hierarchy = 'date_published'
        fieldsets = (
            (None, {
                'fields': (
                    'title',
                    'tweet_url',
                    'caption',
                )
            }),
            ('Tweet Settings', {
                'classes': ('collapse', ),
                'fields': (
                    'hide_media',
                    'hide_thread',
                    'language',
                    'maxwidth',
                ),
            }),
            ('Meta', {
                'classes': ('collapse', ),
                'fields': (
                    'status',
                    'slug',
                    'date_published',
                    'author',
                )
            }),
        )
        list_display = (
            'title',
            'author',
            'date_published',
            'status',
        )
        list_filter = (
            filters.PubliclyVisibleListFilter,
            filters.PublicationDateListFilter,
            filters.StatusListFilter,
            'author',
        )
        prepopulated_fields = {'slug': ('title', )}
        search_fields = [
            'title',
            'slug',
            'caption',
        ]

    oembed_endpoint = 'https://api.twitter.com/1/statuses/oembed.json'
    oembed_schema = [
        'http://twitter.com/#!/*/status/*',
        'https://twitter.com/#!/*/status/*',
        'http://twitter.com/*/status/*',
        'https://twitter.com/*/status/*',
    ]
    oembed_map = (
        'version',
        'provider_name',
        'provider_url',
        (
            'author_name',
            'twitter_user',
        ),
        (
            'author_url',
            'twitter_user_url',
        ),
        'cache_age',
        (
            'url',
            'tweet_url',
        ),
        ('html', 'embed'),
        ('width', 'width'),
    )

    @property
    def oembed_resource(self):
        return self.tweet_url

    @property
    def oembed_endpoint_params(self):
        return {
            'hide_media': str(self.hide_media).lower(),
            'hide_thread': str(self.hide_thread).lower(),
            'lang': self.language.lower(),
            'maxwidth': self.maxwidth
        }

    @property
    def twitter_username(self):
        """
        Returns the poster's Twitter username.

        This method is necessarily fragile, as Twitter's oEmbed response does
        not return this property.
        """
        return re.search(r'([^\/]+)$', self.twitter_user_url).groups()[0]

    @property
    def tweet_id(self):
        """
        Returns this tweet's ID.

        This method is necessarily fragile, as Twitter's oEmbed response does
        not return this property.
        """
        return re.search(r'(\d+)$', self.tweet_url).groups()[0]

    @property
    def intents(self):
        """
        A selection of URLs conforming to Twitter's Web Intents API:

        https://dev.twitter.com/docs/intents
        """
        base = 'https://twitter.com/intent/'
        return {
            'reply': '%stweet?in_reply_to=%s' % (
                base,
                self.tweet_id,
            ),
            'retweet': '%sretweet?tweet_id=%s' % (
                base,
                self.tweet_id,
            ),
            'favorite': '%sfavorite?tweet_id=%s' % (
                base,
                self.tweet_id,
            ),
            'follow': '%suser?screen_name=%s' % (
                base,
                self.twitter_username,
            ),
        }