예제 #1
0
    def rate(self, slug, up=None, down=None, **kwargs):
        """Say 'I like this' for the given media.

        :param slug: The media :attr:`~mediacore.model.media.Media.slug`
        :rtype: unicode
        :returns:
            The new number of likes

        """
        # we have to check if current user is anonymous or authenticated
        userid = check_user_authentication(request)
        if not userid:
            log.warn('Anonymous user cannot rate media')
            raise HTTPUnauthorized().exception

        media = fetch_row(Media, slug=slug)

        # check if current user has already voted this media object
        votes = Vote.query.get_votes(media_id=media.id, user_name=userid)
        if votes.count():
            # if true redirect to 'view'
            log.warn('User %s already voted this media')
            redirect(action='view')

        # create new vote object mapping current media and user
        vote = Vote()
        vote.media_id = media.id
        vote.user_name = userid

        # Give the Vote object an ID.
        DBSession.add(vote)
        DBSession.flush()

        if up:
            vote.increment_likes()
            media.increment_likes()
        elif down:
            vote.increment_dislikes()
            media.increment_dislikes()

        if request.is_xhr:
            return u''
        else:
            redirect(action='view')
예제 #2
0
    def rate(self, slug, up=None, down=None, **kwargs):
        """Say 'I like this' for the given media.

        :param slug: The media :attr:`~mediacore.model.media.Media.slug`
        :rtype: unicode
        :returns:
            The new number of likes

        """
        # we have to check if current user is anonymous or authenticated
        userid = check_user_authentication(request)
        if not userid:
            log.warn('Anonymous user cannot rate media')
            raise HTTPUnauthorized().exception
            
        media = fetch_row(Media, slug=slug)
        
        # check if current user has already voted this media object
        votes = Vote.query.get_votes(media_id=media.id, user_name = userid)
        if votes.count():
            # if true redirect to 'view'
            log.warn('User %s already voted this media')
            redirect(action='view')

        # create new vote object mapping current media and user
        vote = Vote()
        vote.media_id = media.id
        vote.user_name = userid

        # Give the Vote object an ID.
        DBSession.add(vote)
        DBSession.flush()

        if up:
            vote.increment_likes()
            media.increment_likes()
        elif down:
            vote.increment_dislikes()
            media.increment_dislikes()
        
        if request.is_xhr:
            return u''
        else:
            redirect(action='view')
예제 #3
0
    def comment(self, slug, body='', **kwargs):
        """Post a comment from :class:`~mediacore.forms.comments.PostCommentForm`.

        :param slug: The media :attr:`~mediacore.model.media.Media.slug`
        :returns: Redirect to :meth:`view` page for media.

        """
        def result(success, message=None, comment=None):
            if request.is_xhr:
                result = dict(success=success, message=message)
                if comment:
                    result['comment'] = render('comments/_list.html',
                                               {'comment_to_render': comment},
                                               method='xhtml')
                return result
            elif success:
                return redirect(action='view')
            else:
                return self.view(slug,
                                 name=name,
                                 email=email,
                                 body=body,
                                 **kwargs)

        userid = check_user_authentication(request)
        if not userid:
            log.warn('Anonymous user cannot comment media')
            raise HTTPUnauthorized().exception

        user = get_authenticated_user(request)

        akismet_key = request.settings['akismet_key']
        if akismet_key:
            akismet = Akismet(agent=USER_AGENT)
            akismet.key = akismet_key
            akismet.blog_url = request.settings['akismet_url'] or \
                url_for('/', qualified=True)
            akismet.verify_key()
            data = {
                'comment_author': name.encode('utf-8'),
                'user_ip': request.environ.get('REMOTE_ADDR'),
                'user_agent': request.environ.get('HTTP_USER_AGENT', ''),
                'referrer': request.environ.get('HTTP_REFERER', 'unknown'),
                'HTTP_ACCEPT': request.environ.get('HTTP_ACCEPT')
            }

            if akismet.comment_check(body.encode('utf-8'), data):
                return result(False, _(u'Your comment has been rejected.'))

        media = fetch_row(Media, slug=slug)

        name = user.user_name
        email = user.email_address
        cmt = Comment()

        name = filter_vulgarity(name)
        cmt.author = AuthorWithIP(name, email, request.environ['REMOTE_ADDR'])
        cmt.subject = 'Re: %s' % media.title
        cmt.body = filter_vulgarity(body)

        require_review = request.settings['req_comment_approval']
        if not require_review:
            cmt.reviewed = True
            cmt.publishable = True

        media.comments.append(cmt)
        DBSession.flush()
        send_comment_notification(media, cmt)

        if require_review:
            message = _('Thank you for your comment! We will post it just as '
                        'soon as a moderator approves it.')
            return result(True, message=message)
        else:
            return result(True, comment=cmt)
예제 #4
0
def media_player(media,
                 is_widescreen=False,
                 show_like=True,
                 show_dislike=True,
                 show_download=False,
                 show_embed=False,
                 show_playerbar=True,
                 show_popout=True,
                 show_resize=False,
                 show_share=True,
                 js_init=None,
                 **kwargs):
    """Instantiate and render the preferred player that can play this media.

    We make no effort to pick the "best" player here, we simply return
    the first player that *can* play any of the URIs associated with
    the given media object. It's up to the user to declare their own
    preferences wisely.

    Player preferences are fetched from the database and the
    :attr:`mediacore.model.players.c.data` dict is passed as kwargs to
    :meth:`AbstractPlayer.__init__`.

    :type media: :class:`mediacore.model.media.Media`
    :param media: A media instance to play.

    :param js_init: Optional function to call after the javascript player
        controller has been instantiated. Example of a function literal:
        ``function(controller){ controller.setFillScreen(true); }``.
        Any function reference can be used as long as it is defined
        in all pages and accepts the JS player controller as its first
        and only argument.

    :param \*\*kwargs: Extra kwargs for :meth:`AbstractPlayer.__init__`.

    :rtype: `str` or `None`
    :returns: A rendered player.
    """
    # we have to check if current user is anonymous or authenticated
    userid = check_user_authentication(request)
    # check if current user has already voted this media object
    votes = Vote.query.get_votes(media_id=media.id, user_name=userid)

    if (not userid) or votes.count():
        show_like = False
        show_dislike = False

    uris = media.get_uris()

    # Find the first player that can play any uris
    for player_cls, player_data in fetch_enabled_players():
        can_play = player_cls.can_play(uris)
        if any(can_play):
            break
    else:
        return None

    # Grab just the uris that the chosen player can play
    playable_uris = [uri for uri, plays in izip(uris, can_play) if plays]
    kwargs['data'] = player_data
    player = player_cls(media, playable_uris, **kwargs)

    return render(
        'players/html5_or_flash.html', {
            'player': player,
            'media': media,
            'uris': uris,
            'is_widescreen': is_widescreen,
            'js_init': js_init,
            'show_like': show_like,
            'show_dislike': show_dislike,
            'show_download': show_download,
            'show_embed': show_embed,
            'show_playerbar': show_playerbar,
            'show_popout': show_popout,
            'show_resize': show_resize and player.supports_resizing,
            'show_share': show_share,
        })
예제 #5
0
def media_player(media, is_widescreen=False, show_like=True, show_dislike=True,
                 show_download=False, show_embed=False, show_playerbar=True,
                 show_popout=True, show_resize=False, show_share=True,
                 js_init=None, **kwargs):
    """Instantiate and render the preferred player that can play this media.

    We make no effort to pick the "best" player here, we simply return
    the first player that *can* play any of the URIs associated with
    the given media object. It's up to the user to declare their own
    preferences wisely.

    Player preferences are fetched from the database and the
    :attr:`mediacore.model.players.c.data` dict is passed as kwargs to
    :meth:`AbstractPlayer.__init__`.

    :type media: :class:`mediacore.model.media.Media`
    :param media: A media instance to play.

    :param js_init: Optional function to call after the javascript player
        controller has been instantiated. Example of a function literal:
        ``function(controller){ controller.setFillScreen(true); }``.
        Any function reference can be used as long as it is defined
        in all pages and accepts the JS player controller as its first
        and only argument.

    :param \*\*kwargs: Extra kwargs for :meth:`AbstractPlayer.__init__`.

    :rtype: `str` or `None`
    :returns: A rendered player.
    """
    # we have to check if current user is anonymous or authenticated
    userid = check_user_authentication(request)
    # check if current user has already voted this media object
    votes = Vote.query.get_votes(media_id=media.id, user_name = userid)

    if (not userid) or votes.count():
        show_like=False
        show_dislike=False

    uris = media.get_uris()

    # Find the first player that can play any uris
    for player_cls, player_data in fetch_enabled_players():
        can_play = player_cls.can_play(uris)
        if any(can_play):
            break
    else:
        return None

    # Grab just the uris that the chosen player can play
    playable_uris = [uri for uri, plays in izip(uris, can_play) if plays]
    kwargs['data'] = player_data
    player = player_cls(media, playable_uris, **kwargs)

    return render('players/html5_or_flash.html', {
        'player': player,
        'media': media,
        'uris': uris,
        'is_widescreen': is_widescreen,
        'js_init': js_init,
        'show_like': show_like,
        'show_dislike': show_dislike,
        'show_download': show_download,
        'show_embed': show_embed,
        'show_playerbar': show_playerbar,
        'show_popout': show_popout,
        'show_resize': show_resize and player.supports_resizing,
        'show_share': show_share,
    })
예제 #6
0
    def comment(self, slug, body='', **kwargs):
        """Post a comment from :class:`~mediacore.forms.comments.PostCommentForm`.

        :param slug: The media :attr:`~mediacore.model.media.Media.slug`
        :returns: Redirect to :meth:`view` page for media.

        """

        def result(success, message=None, comment=None):
            if request.is_xhr:
                result = dict(success=success, message=message)
                if comment:
                    result['comment'] = render('comments/_list.html',
                        {'comment_to_render': comment},
                        method='xhtml')
                return result
            elif success:
                return redirect(action='view')
            else:
                return self.view(slug, name=name, email=email, body=body,
                                 **kwargs)

        userid = check_user_authentication(request)
        if not userid:
            log.warn('Anonymous user cannot comment media')
            raise HTTPUnauthorized().exception

        user = get_authenticated_user(request)

        akismet_key = request.settings['akismet_key']
        if akismet_key:
            akismet = Akismet(agent=USER_AGENT)
            akismet.key = akismet_key
            akismet.blog_url = request.settings['akismet_url'] or \
                url_for('/', qualified=True)
            akismet.verify_key()
            data = {'comment_author': name.encode('utf-8'),
                    'user_ip': request.environ.get('REMOTE_ADDR'),
                    'user_agent': request.environ.get('HTTP_USER_AGENT', ''),
                    'referrer': request.environ.get('HTTP_REFERER',  'unknown'),
                    'HTTP_ACCEPT': request.environ.get('HTTP_ACCEPT')}

            if akismet.comment_check(body.encode('utf-8'), data):
                return result(False, _(u'Your comment has been rejected.'))

        media = fetch_row(Media, slug=slug)

        name = user.user_name
        email = user.email_address
        cmt = Comment()

        name = filter_vulgarity(name)
        cmt.author = AuthorWithIP(name, email, request.environ['REMOTE_ADDR'])
        cmt.subject = 'Re: %s' % media.title
        cmt.body = filter_vulgarity(body)

        require_review = request.settings['req_comment_approval']
        if not require_review:
            cmt.reviewed = True
            cmt.publishable = True

        media.comments.append(cmt)
        DBSession.flush()
        send_comment_notification(media, cmt)

        if require_review:
            message = _('Thank you for your comment! We will post it just as '
                        'soon as a moderator approves it.')
            return result(True, message=message)
        else:
            return result(True, comment=cmt)