view src/pages/common/blog/page_meta.py @ 932:af6a62e21053

pages (common/blog): new show_comments variable is used in session data, if True comments are grabbed and chained to items
author Goffi <goffi@goffi.org>
date Mon, 17 Apr 2017 20:43:12 +0200
parents ee243d48100e
children e4c13a995e0b
line wrap: on
line source

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from libervia.server.constants import Const as C
from twisted.words.protocols.jabber import jid
from twisted.internet import defer
from sat.tools.common import data_objects
from sat.core.i18n import _
from sat.core.log import getLogger
import urllib
log = getLogger('pages/common/blog')

"""generic blog (with service/node provided)"""
name = u'blog'
template = u"blog/articles.html"
uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}


def microblog_uri(self, uri_data):
    service = urllib.quote_plus(uri_data[u'path'])
    node = urllib.quote_plus(uri_data[u'node'])
    return service + u'/' + node


def parse_url(self, request):
    """URL is /[service]/[node]

    if [node] is not found, default namespace is used
    if both [service] and [node] are not found, default service is used too
    """
    data = self.getRData(request)

    try:
        service = self.nextPath(request)
    except IndexError:
        data['service'] = u''
    else:
        try:
            data[u"service"] = jid.JID(service)
        except Exception:
            log.warning(_(u"bad service entered: {}").format(service))
            self.pageError(request, C.HTTP_BAD_REQUEST)

    try:
        data['node'] = self.nextPath(request)
    except IndexError:
        data['node'] = u''


@defer.inlineCallbacks
def appendComments(self, blog_items, profile):
    for blog_item in blog_items:
        for comment_data in blog_item.comments:
            service = comment_data[u'service']
            node = comment_data[u'node']
            comments_data = yield self.host.bridge.mbGet(
                                  service,
                                  node,
                                  C.NO_LIMIT,
                                  [],
                                  {},
                                  profile)
            comments = data_objects.BlogItems(comments_data)
            blog_item.appendCommentsItems(comments)
            yield appendComments(self, comments, profile)


@defer.inlineCallbacks
def prepare_render(self, request):
    data = self.getRData(request)
    # if the comments are not explicitly hidden, we show them
    service, node, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'show_comments', True)
    profile = self.getProfile(request)
    if profile is None:
        profile = C.SERVICE_PROFILE

    try:
        blog_data = yield self.host.bridge.mbGet(
                              service.userhost(),
                              node,
                              10,
                              [],
                              {},
                              profile)
    except Exception as e:
        # FIXME: need a better way to test errors in bridge errback
        if u"forbidden" in unicode(e):
            self.pageError(request, 401)
        else:
            raise e

    items = data_objects.BlogItems(blog_data)

    if show_comments:
        yield appendComments(self, items, profile)

    template_data = request.template_data
    template_data[u'items'] = items