view libervia/pages/forums/list/page_meta.py @ 1216:b2d067339de3

python 3 port: /!\ Python 3.6+ is now needed to use libervia /!\ instability may occur and features may not be working anymore, this will improve with time /!\ TxJSONRPC dependency has been removed The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog, JSON RPC related code). Adapted code to work without `html` and `themes` dirs.
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:12:31 +0200
parents 29eb15062416
children f511f8fbbf8a
line wrap: on
line source

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from libervia.server.constants import Const as C
from twisted.internet import defer
from sat.core.log import getLogger
from sat.core.i18n import _
from sat.tools.common import uri as xmpp_uri

log = getLogger(__name__)
import json

"""forum handling pages"""

name = "forums"
access = C.PAGES_ACCESS_PUBLIC
template = "forum/overview.html"


def parse_url(self, request):
    self.getPathArgs(
        request,
        ["service", "node", "forum_key"],
        service="@jid",
        node="@",
        forum_key="",
    )


def getLinks(self, forums):
    for forum in forums:
        try:
            uri = forum["uri"]
        except KeyError:
            pass
        else:
            uri = xmpp_uri.parseXMPPUri(uri)
            service = uri["path"]
            node = uri["node"]
            forum["http_url"] = self.getPageByName("forum_topics").getURL(service, node)
        if "sub-forums" in forum:
            getLinks(self, forum["sub-forums"])


@defer.inlineCallbacks
def prepare_render(self, request):
    data = self.getRData(request)
    template_data = request.template_data
    service, node, key = data["service"], data["node"], data["forum_key"]
    profile = self.getProfile(request) or C.SERVICE_PROFILE

    try:
        forums_raw = yield self.host.bridgeCall(
            "forumsGet", service.full() if service else "", node, key, profile
        )
    except Exception as e:
        log.warning(_("Can't retrieve forums: {msg}").format(msg=e))
        forums = []
    else:
        forums = json.loads(forums_raw)
    getLinks(self, forums)

    template_data["forums"] = forums