diff libervia/pages/lists/edit/page_meta.py @ 1378:e3e303a30a74

pages (tickets): renamed "tickets" to "lists": "lists" is more generic, and tickets is actually a specific kind of list. /!\ "tickets_trackers_json" option has been renamed to "lists_directory_json".
author Goffi <goffi@goffi.org>
date Thu, 28 Jan 2021 18:51:44 +0100
parents libervia/pages/tickets/edit/page_meta.py@04e7dd6b6f4d
children a84383c659b4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/pages/lists/edit/page_meta.py	Thu Jan 28 18:51:44 2021 +0100
@@ -0,0 +1,117 @@
+#!/usr/bin/env python3
+
+
+from libervia.server.constants import Const as C
+from sat.core.i18n import _
+from twisted.internet import defer
+from sat.tools.common import template_xmlui
+from sat.tools.common import data_format
+from sat.core.log import getLogger
+
+log = getLogger(__name__)
+
+name = "list_edit"
+access = C.PAGES_ACCESS_PROFILE
+template = "list/edit.html"
+
+
+def parse_url(self, request):
+    try:
+        item_id = self.nextPath(request)
+    except IndexError:
+        log.warning(_("no list item id specified"))
+        self.pageError(request, C.HTTP_BAD_REQUEST)
+
+    data = self.getRData(request)
+    data["list_item_id"] = item_id
+
+
+@defer.inlineCallbacks
+def prepare_render(self, request):
+    data = self.getRData(request)
+    template_data = request.template_data
+    service, node, list_item_id = (
+        data.get("service", ""),
+        data.get("node", ""),
+        data["list_item_id"],
+    )
+    profile = self.getProfile(request)
+
+    # we don't ignore "author" below to keep it when a list item is edited
+    # by node owner/admin and "consistent publisher" is activated
+    ignore = (
+        "publisher",
+        "author",
+        "author_jid",
+        "author_email",
+        "created",
+        "updated",
+        "comments_uri",
+    )
+    list_raw = yield self.host.bridgeCall(
+        "listGet",
+        service.full() if service else "",
+        node,
+        C.NO_LIMIT,
+        [list_item_id],
+        "",
+        {},
+        profile,
+    )
+    list_items, metadata = data_format.deserialise(list_raw, type_check=list)
+    list_item = [template_xmlui.create(self.host, x, ignore=ignore) for x in list_items][0]
+
+    try:
+        # small trick to get a one line text input instead of the big textarea
+        list_item.widgets["labels"].type = "string"
+        list_item.widgets["labels"].value = list_item.widgets["labels"].value.replace(
+            "\n", ", "
+        )
+    except KeyError:
+        pass
+
+    # for now we don't have XHTML editor, so we'll go with a TextBox and a convertion
+    # to a text friendly syntax using markdown
+    wid = list_item.widgets['body']
+    if wid.type == "xhtmlbox":
+        wid.type = "textbox"
+        wid.value =  yield self.host.bridgeCall(
+            "syntaxConvert", wid.value, C.SYNTAX_XHTML, "markdown",
+            False, profile)
+
+    template_data["new_list_item_xmlui"] = list_item
+
+
+@defer.inlineCallbacks
+def on_data_post(self, request):
+    data = self.getRData(request)
+    service = data["service"]
+    node = data["node"]
+    list_item_id = data["list_item_id"]
+    posted_data = self.getAllPostedData(request)
+    if not posted_data["title"] or not posted_data["body"]:
+        self.pageError(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.getProfile(request)
+
+    # we convert back body to XHTML
+    body = yield self.host.bridgeCall(
+        "syntaxConvert", 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)]
+
+    extra = {'update': True}
+    yield self.host.bridgeCall(
+        "listSet", service.full(), node, posted_data, "", list_item_id,
+        data_format.serialise(extra), profile
+    )
+    # we don't want to redirect to edit page on success, but to list overview
+    data["post_redirect_page"] = (
+        self.getPageByName("lists"),
+        service.full(),
+        node or "@",
+    )