comparison libervia/pages/lists/view_item/page_meta.py @ 1387:a84383c659b4

lists: creation, invitation, item deletion: this big patch includes: - reorganisation of pages for consistency, discovery is now the main list page, and list overview is now in `view` while item view is moved to `view_item` - lists from lists of interest are now shown in discovery page - list deletion from discory page - list can now be created, using templates now available from backend - invitation manager can now be used from list overview - list item can now be deleted from `view_item`
author Goffi <goffi@goffi.org>
date Sat, 20 Feb 2021 14:07:22 +0100
parents libervia/pages/lists/page_meta.py@e3e303a30a74
children 907f519faaf0 8ac062d2ff8b
comparison
equal deleted inserted replaced
1386:83be300d17e3 1387:a84383c659b4
1 #!/usr/bin/env python3
2
3 from twisted.words.protocols.jabber import jid
4 from sat.core.i18n import _
5 from sat.tools.common import template_xmlui
6 from sat.tools.common import uri
7 from sat.tools.common import data_format
8 from sat.core.log import getLogger
9 from sat_frontends.bridge.bridge_frontend import BridgeException
10 from libervia.server.constants import Const as C
11 from libervia.server.utils import SubPage
12 from libervia.server import session_iface
13
14 log = getLogger(__name__)
15
16
17 name = "list_view"
18 access = C.PAGES_ACCESS_PUBLIC
19 template = "list/item.html"
20
21
22 def parse_url(self, request):
23 self.getPathArgs(request, ["service", "node", "item_id"], service="jid", node="@")
24 data = self.getRData(request)
25 if data["item_id"] is None:
26 log.warning(_("no list item id specified"))
27 self.pageError(request, C.HTTP_BAD_REQUEST)
28
29 async def prepare_render(self, request):
30 data = self.getRData(request)
31 template_data = request.template_data
32 session = self.host.getSessionData(request, session_iface.ISATSession)
33 service, node, item_id = (
34 data.get("service", ""),
35 data.get("node", ""),
36 data["item_id"],
37 )
38 profile = self.getProfile(request)
39
40 if profile is None:
41 profile = C.SERVICE_PROFILE
42
43 list_raw = await self.host.bridgeCall(
44 "listGet",
45 service.full() if service else "",
46 node,
47 C.NO_LIMIT,
48 [item_id],
49 "",
50 {"labels_as_list": C.BOOL_TRUE},
51 profile,
52 )
53 list_items, metadata = data_format.deserialise(list_raw, type_check=list)
54 list_item = [template_xmlui.create(self.host, x) for x in list_items][0]
55 template_data["item"] = list_item
56 try:
57 comments_uri = list_item.widgets["comments_uri"].value
58 except KeyError:
59 pass
60 else:
61 if comments_uri:
62 uri_data = uri.parseXMPPUri(comments_uri)
63 template_data["comments_node"] = comments_node = uri_data["node"]
64 template_data["comments_service"] = comments_service = uri_data["path"]
65 try:
66 comments = data_format.deserialise(await self.host.bridgeCall(
67 "mbGet", comments_service, comments_node, C.NO_LIMIT, [], {}, profile
68 ))
69 except BridgeException as e:
70 if e.classname == 'NotFound' or e.condition == 'item-not-found':
71 log.warning(
72 _("Can't find comment node at [{service}] {node!r}")
73 .format(service=comments_service, node=comments_node)
74 )
75 else:
76 raise e
77 else:
78 template_data["comments"] = comments
79 template_data["login_url"] = self.getPageRedirectURL(request)
80 self.exposeToScripts(
81 request,
82 comments_node=comments_node,
83 comments_service=comments_service,
84 )
85
86 if session.connected:
87 # we activate modification action (edit, delete) only if user is the publisher or
88 # the node owner
89 publisher = jid.JID(list_item.widgets["publisher"].value)
90 is_publisher = publisher.userhostJID() == session.jid.userhostJID()
91 affiliation = None
92 if not is_publisher:
93 node = node or self.host.ns_map["tickets"]
94 affiliation = await self.host.getAffiliation(request, service, node)
95 if is_publisher or affiliation == "owner":
96 self.exposeToScripts(
97 request,
98 pubsub_service = service.full(),
99 pubsub_node = node,
100 pubsub_item = item_id,
101 )
102 template_data["can_modify"] = True
103 template_data["url_list_item_edit"] = self.getURLByPath(
104 SubPage("list_edit"),
105 service.full(),
106 node or "@",
107 item_id,
108 )
109
110 # we add xmpp: URI
111 uri_args = {'path': service.full()}
112 uri_args['node'] = node or self.host.ns_map["tickets"]
113 if item_id:
114 uri_args['item'] = item_id
115 template_data['xmpp_uri'] = uri.buildXMPPUri('pubsub', **uri_args)
116
117
118 async def on_data_post(self, request):
119 type_ = self.getPostedData(request, "type")
120 if type_ == "comment":
121 blog_page = self.getPageByName("blog_view")
122 await blog_page.on_data_post(self, request)
123 else:
124 log.warning(_("Unhandled data type: {}").format(type_))