annotate libervia/web/pages/lists/view_item/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/view_item/page_meta.py@106bae41f5c8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1145
diff changeset
1 #!/usr/bin/env python3
1239
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
2
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
3 from twisted.words.protocols.jabber import jid
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
4 from libervia.backend.core.i18n import _, D_
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
5 from libervia.backend.tools.common import template_xmlui
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
6 from libervia.backend.tools.common import uri
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
7 from libervia.backend.tools.common import data_format
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
8 from libervia.backend.core.log import getLogger
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
9 from libervia.frontends.bridge.bridge_frontend import BridgeException
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
10 from libervia.web.server.constants import Const as C
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
11 from libervia.web.server.utils import SubPage
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
12 from libervia.web.server import session_iface
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1089
diff changeset
13
1145
29eb15062416 pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents: 1142
diff changeset
14 log = getLogger(__name__)
967
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
16
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
17 name = "list_view"
967
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 access = C.PAGES_ACCESS_PUBLIC
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
19 template = "list/item.html"
967
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 def parse_url(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
23 self.get_path_args(request, ["service", "node", "item_id"], service="jid", node="@")
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
24 data = self.get_r_data(request)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
25 if data["item_id"] is None:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
26 log.warning(_("no list item id specified"))
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
27 self.page_error(request, C.HTTP_BAD_REQUEST)
967
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
1468
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
29
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
30 def add_breadcrumb(self, request, breadcrumbs):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
31 data = self.get_r_data(request)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
32 list_url = self.get_page_by_name("lists").get_url(data["service"].full(),
1468
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
33 data.get("node") or "@")
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
34 breadcrumbs.append({
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
35 "label": D_("List"),
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
36 "url": list_url
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
37 })
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
38 breadcrumbs.append({
1470
837ae0c4b858 pages (lists/view_item): use uppercase for firt letter of Item breadcrumb
Goffi <goffi@goffi.org>
parents: 1468
diff changeset
39 "label": D_("Item"),
1468
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
40 })
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
41
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
42
1302
04e7dd6b6f4d pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
43 async def prepare_render(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
44 data = self.get_r_data(request)
967
4d1dcf97740f pages (tickets): tickets first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 template_data = request.template_data
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
46 session = self.host.get_session_data(request, session_iface.IWebSession)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
47 service, node, item_id = (
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
48 data.get("service", ""),
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
49 data.get("node", ""),
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
50 data["item_id"],
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
51 )
1461
8ac062d2ff8b pages(lists/view_item): fix node used:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
52 namespace = node or self.host.ns_map["tickets"]
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
53 node = await self.host.bridge_call("ps_schema_submitted_node_get", namespace)
1461
8ac062d2ff8b pages(lists/view_item): fix node used:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
54
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
55 profile = self.get_profile(request)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
56 if profile is None:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
57 profile = C.SERVICE_PROFILE
1142
003597f895a0 pages (tickets): use the new getPubsubExtra and setPagination methods to handle pagination
Goffi <goffi@goffi.org>
parents: 1137
diff changeset
58
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
59 list_raw = await self.host.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
60 "list_get",
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1145
diff changeset
61 service.full() if service else "",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1089
diff changeset
62 node,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1089
diff changeset
63 C.NO_LIMIT,
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
64 [item_id],
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1089
diff changeset
65 "",
1492
3a34d78f2717 pages (view_item): fix `extra` serialisation on `listGet`
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
66 data_format.serialise({"labels_as_list": C.BOOL_TRUE}),
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1089
diff changeset
67 profile,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1089
diff changeset
68 )
1468
e85ef5f3515e pages (lists/view_item): add current list to breadcrumbs
Goffi <goffi@goffi.org>
parents: 1461
diff changeset
69 list_items, __ = data_format.deserialise(list_raw, type_check=list)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
70 list_item = [template_xmlui.create(self.host, x) for x in list_items][0]
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
71 template_data["item"] = list_item
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
72 try:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
73 comments_uri = list_item.widgets["comments_uri"].value
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
74 except KeyError:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
75 pass
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
76 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
77 if comments_uri:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
78 uri_data = uri.parse_xmpp_uri(comments_uri)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
79 template_data["comments_node"] = comments_node = uri_data["node"]
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
80 template_data["comments_service"] = comments_service = uri_data["path"]
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
81 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
82 comments = data_format.deserialise(await self.host.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
83 "mb_get", comments_service, comments_node, C.NO_LIMIT, [],
1447
907f519faaf0 pages: pubsub's `extra` is now serialised, following backend change
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
84 data_format.serialise({}), profile
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
85 ))
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
86 except BridgeException as e:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
87 if e.classname == 'NotFound' or e.condition == 'item-not-found':
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
88 log.warning(
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
89 _("Can't find comment node at [{service}] {node!r}")
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
90 .format(service=comments_service, node=comments_node)
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
91 )
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
92 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
93 raise e
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
94 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
95 template_data["comments"] = comments
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
96 template_data["login_url"] = self.get_page_redirect_url(request)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
97 self.expose_to_scripts(
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
98 request,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
99 comments_node=comments_node,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
100 comments_service=comments_service,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
101 )
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
102
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
103 if session.connected:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
104 # we activate modification action (edit, delete) only if user is the publisher or
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
105 # the node owner
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
106 publisher = jid.JID(list_item.widgets["publisher"].value)
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
107 is_publisher = publisher.userhostJID() == session.jid.userhostJID()
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
108 affiliation = None
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
109 if not is_publisher:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
110 affiliation = await self.host.get_affiliation(request, service, node)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
111 if is_publisher or affiliation == "owner":
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
112 self.expose_to_scripts(
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
113 request,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
114 pubsub_service = service.full(),
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
115 pubsub_node = node,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
116 pubsub_item = item_id,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
117 )
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
118 template_data["can_modify"] = True
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
119 template_data["url_list_item_edit"] = self.get_url_by_path(
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
120 SubPage("list_edit"),
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
121 service.full(),
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
122 node or "@",
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
123 item_id,
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
124 )
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
125
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
126 # we add xmpp: URI
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
127 uri_args = {'path': service.full()}
1461
8ac062d2ff8b pages(lists/view_item): fix node used:
Goffi <goffi@goffi.org>
parents: 1387
diff changeset
128 uri_args['node'] = node
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
129 if item_id:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
130 uri_args['item'] = item_id
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
131 template_data['xmpp_uri'] = uri.build_xmpp_uri('pubsub', **uri_args)
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
132
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
133
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
134 async def on_data_post(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
135 type_ = self.get_posted_data(request, "type")
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
136 if type_ == "comment":
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
137 blog_page = self.get_page_by_name("blog_view")
1387
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
138 await blog_page.on_data_post(self, request)
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
139 else:
a84383c659b4 lists: creation, invitation, item deletion:
Goffi <goffi@goffi.org>
parents: 1378
diff changeset
140 log.warning(_("Unhandled data type: {}").format(type_))