view src/pages/common/blog/page_meta.py @ 928:ee243d48100e

pages (common): added common hierarchy for reusable pages (blog is the first)
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 18:24:24 +0200
parents
children af6a62e21053
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 prepare_render(self, request):
    data = self.getRData(request)
    service, node = data.get(u'service', u''), data.get(u'node', u'')
    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

    request.template_data[u'items'] = data_objects.BlogItems(blog_data)