コード例 #1
0
    def comment(self, slug, name='', email=None, body='', **kwargs):
        """Post a comment from :class:`~mediadrop.forms.comments.PostCommentForm`.

        :param slug: The media :attr:`~mediadrop.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)

        if request.settings['comments_engine'] != 'builtin':
            abort(404)
        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)
        request.perm.assert_permission(u'view', media.resource)

        c = Comment()

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

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

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

        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=c)
コード例 #2
0
ファイル: media.py プロジェクト: josuermcosta/mediadrop
    def comment(self, slug, name='', email=None, body='', **kwargs):
        """Post a comment from :class:`~mediadrop.forms.comments.PostCommentForm`.

        :param slug: The media :attr:`~mediadrop.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)

        if request.settings['comments_engine'] != 'builtin':
            abort(404)
        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)
        request.perm.assert_permission(u'view', media.resource)

        c = Comment()

        aux = DBSession.query(User).filter(User.display_name == name)

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

        if request.perm.user.display_name != "Anonymous User":
            name = request.perm.user.display_name
            email = request.perm.user.email_address
            if aux:
                name = filter_vulgarity(name)
                c.author = AuthorWithIP(name, email,
                                        request.environ['REMOTE_ADDR'])

        c.subject = 'Re: %s' % media.title
        c.body = filter_vulgarity(body)

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

        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=c)