view libervia/pages/lists/new/page_meta.py @ 1510:5ea06e8b06ed

browser: make bridge API closer to the one use with other frontends: `bridge.AsyncBridge` is not used instead of `aio_bridge.Bridge`
author Goffi <goffi@goffi.org>
date Mon, 22 May 2023 11:57:44 +0200
parents 106bae41f5c8
children
line wrap: on
line source

#!/usr/bin/env python3

from libervia.server.constants import Const as C
from sat.tools.common import template_xmlui
from sat.core.log import getLogger

log = getLogger(__name__)


name = "list_new"
access = C.PAGES_ACCESS_PROFILE
template = "list/create_item.html"


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

async def prepare_render(self, request):
    data = self.get_r_data(request)
    template_data = request.template_data
    service, node = data.get("service", ""), data.get("node", "")
    profile = self.get_profile(request)
    schema = await self.host.bridge_call("list_schema_get", service.full(), node, profile)
    data["schema"] = schema
    # following fields are handled in backend
    ignore = (
        "author",
        "author_jid",
        "author_email",
        "created",
        "updated",
        "comments_uri",
        "status",
        "milestone",
        "priority",
    )
    xmlui_obj = template_xmlui.create(self.host, schema, ignore=ignore)
    try:
        # small trick to get a one line text input instead of the big textarea
        xmlui_obj.widgets["labels"].type = "string"
    except KeyError:
        pass

    try:
        wid = xmlui_obj.widgets['body']
    except KeyError:
        pass
    else:
        if wid.type == "xhtmlbox":
            # same as for list_edit, we have to convert for now
            wid.type = "textbox"
            wid.value =  await self.host.bridge_call(
                "syntax_convert", wid.value, C.SYNTAX_XHTML, "markdown",
                False, profile)
    template_data["new_list_item_xmlui"] = xmlui_obj


async def on_data_post(self, request):
    data = self.get_r_data(request)
    service = data["service"]
    node = data["node"]
    posted_data = self.get_all_posted_data(request)
    if (("title" in posted_data and not posted_data["title"])
         or ("body" in posted_data and not posted_data["body"])):
        self.page_error(request, C.HTTP_BAD_REQUEST)
    try:
        posted_data["labels"] = [l.strip() for l in posted_data["labels"][0].split(",")]
    except (KeyError, IndexError):
        pass
    profile = self.get_profile(request)

    # we convert back body to XHTML
    if "body" in posted_data:
        body = await self.host.bridge_call(
            "syntax_convert", posted_data['body'][0], "markdown", C.SYNTAX_XHTML,
            False, profile)
        posted_data['body'] = ['<div xmlns="{ns}">{body}</div>'.format(ns=C.NS_XHTML,
                                                                     body=body)]


    await self.host.bridge_call(
        "list_set", service.full(), node, posted_data, "", "", "", profile
    )
    # we don't want to redirect to creation page on success, but to list overview
    data["post_redirect_page"] = (
        self.get_page_by_name("lists"),
        service.full(),
        node or "@",
    )