Mercurial > libervia-web
comparison libervia/web/pages/lists/edit/page_meta.py @ 1518:eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 16:49:28 +0200 |
parents | libervia/pages/lists/edit/page_meta.py@106bae41f5c8 |
children |
comparison
equal
deleted
inserted
replaced
1517:b8ed9726525b | 1518:eb00d593801d |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 from libervia.web.server.constants import Const as C | |
5 from libervia.backend.core.i18n import _ | |
6 from twisted.internet import defer | |
7 from libervia.backend.tools.common import template_xmlui | |
8 from libervia.backend.tools.common import data_format | |
9 from libervia.backend.core.log import getLogger | |
10 | |
11 log = getLogger(__name__) | |
12 | |
13 name = "list_edit" | |
14 access = C.PAGES_ACCESS_PROFILE | |
15 template = "list/edit.html" | |
16 | |
17 | |
18 def parse_url(self, request): | |
19 self.get_path_args(request, ["service", "node", "item_id"], service="jid", node="@") | |
20 data = self.get_r_data(request) | |
21 if data["item_id"] is None: | |
22 log.warning(_("no list item id specified")) | |
23 self.page_error(request, C.HTTP_BAD_REQUEST) | |
24 | |
25 @defer.inlineCallbacks | |
26 def prepare_render(self, request): | |
27 data = self.get_r_data(request) | |
28 template_data = request.template_data | |
29 service, node, item_id = ( | |
30 data.get("service", ""), | |
31 data.get("node", ""), | |
32 data["item_id"], | |
33 ) | |
34 profile = self.get_profile(request) | |
35 | |
36 # we don't ignore "author" below to keep it when a list item is edited | |
37 # by node owner/admin and "consistent publisher" is activated | |
38 ignore = ( | |
39 "publisher", | |
40 "author", | |
41 "author_jid", | |
42 "author_email", | |
43 "created", | |
44 "updated", | |
45 "comments_uri", | |
46 ) | |
47 list_raw = yield self.host.bridge_call( | |
48 "list_get", | |
49 service.full() if service else "", | |
50 node, | |
51 C.NO_LIMIT, | |
52 [item_id], | |
53 "", | |
54 data_format.serialise({}), | |
55 profile, | |
56 ) | |
57 list_items, metadata = data_format.deserialise(list_raw, type_check=list) | |
58 list_item = [template_xmlui.create(self.host, x, ignore=ignore) for x in list_items][0] | |
59 | |
60 try: | |
61 # small trick to get a one line text input instead of the big textarea | |
62 list_item.widgets["labels"].type = "string" | |
63 list_item.widgets["labels"].value = list_item.widgets["labels"].value.replace( | |
64 "\n", ", " | |
65 ) | |
66 except KeyError: | |
67 pass | |
68 | |
69 # for now we don't have XHTML editor, so we'll go with a TextBox and a convertion | |
70 # to a text friendly syntax using markdown | |
71 wid = list_item.widgets['body'] | |
72 if wid.type == "xhtmlbox": | |
73 wid.type = "textbox" | |
74 wid.value = yield self.host.bridge_call( | |
75 "syntax_convert", wid.value, C.SYNTAX_XHTML, "markdown", | |
76 False, profile) | |
77 | |
78 template_data["new_list_item_xmlui"] = list_item | |
79 | |
80 | |
81 async def on_data_post(self, request): | |
82 data = self.get_r_data(request) | |
83 service = data["service"] | |
84 node = data["node"] | |
85 item_id = data["item_id"] | |
86 posted_data = self.get_all_posted_data(request) | |
87 if not posted_data["title"] or not posted_data["body"]: | |
88 self.page_error(request, C.HTTP_BAD_REQUEST) | |
89 try: | |
90 posted_data["labels"] = [l.strip() for l in posted_data["labels"][0].split(",")] | |
91 except (KeyError, IndexError): | |
92 pass | |
93 profile = self.get_profile(request) | |
94 | |
95 # we convert back body to XHTML | |
96 body = await self.host.bridge_call( | |
97 "syntax_convert", posted_data['body'][0], "markdown", C.SYNTAX_XHTML, | |
98 False, profile) | |
99 posted_data['body'] = ['<div xmlns="{ns}">{body}</div>'.format(ns=C.NS_XHTML, | |
100 body=body)] | |
101 | |
102 extra = {'update': True} | |
103 await self.host.bridge_call( | |
104 "list_set", service.full(), node, posted_data, "", item_id, | |
105 data_format.serialise(extra), profile | |
106 ) | |
107 data["post_redirect_page"] = ( | |
108 self.get_page_by_name("list_view"), | |
109 service.full(), | |
110 node or "@", | |
111 item_id | |
112 ) | |
113 return C.POST_NO_CONFIRM |