comparison libervia/web/pages/lists/view/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/page_meta.py@106bae41f5c8
children
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3 from libervia.backend.tools.common import template_xmlui
4 from libervia.backend.tools.common import data_objects
5 from libervia.backend.tools.common import data_format
6 from libervia.backend.core.log import getLogger
7 from libervia.frontends.bridge.bridge_frontend import BridgeException
8 from libervia.web.server.constants import Const as C
9
10 log = getLogger(__name__)
11
12 name = "lists"
13 access = C.PAGES_ACCESS_PUBLIC
14 template = "list/overview.html"
15
16
17 def parse_url(self, request):
18 self.get_path_args(request, ["service", "node"], service="jid")
19 data = self.get_r_data(request)
20 service, node = data["service"], data["node"]
21 if node is None:
22 self.http_redirect(request, self.get_page_by_name("lists_disco").url)
23 if node == "@":
24 node = data["node"] = ""
25 template_data = request.template_data
26 template_data["url_list_items"] = self.get_url(service.full(), node or "@")
27 template_data["url_list_new"] = self.get_page_by_name("list_new").get_url(
28 service.full(), node or "@")
29
30
31 async def prepare_render(self, request):
32 data = self.get_r_data(request)
33 template_data = request.template_data
34 service, node = data["service"], data["node"]
35 submitted_node = await self.host.bridge_call(
36 "ps_schema_submitted_node_get",
37 node or self.host.ns_map["tickets"]
38 )
39 profile = self.get_profile(request) or C.SERVICE_PROFILE
40
41 self.check_cache(request, C.CACHE_PUBSUB, service=service, node=node, short="tickets")
42
43 try:
44 lists_types = self.get_page_data(request, "lists_types")
45 if lists_types is None:
46 lists_types = {}
47 self.set_page_data(request, "lists_types", lists_types)
48 list_type = lists_types[(service, node)]
49 except KeyError:
50 ns_tickets_type = self.host.ns_map["tickets_type"]
51 schema_raw = await self.host.bridge_call(
52 "ps_schema_dict_get",
53 service.full(),
54 submitted_node,
55 profile
56 )
57 schema = data_format.deserialise(schema_raw)
58 try:
59 list_type_field = next(
60 f for f in schema["fields"] if f["type"] == "hidden"
61 and f.get("name") == ns_tickets_type
62 )
63 except StopIteration:
64 list_type = lists_types[(service, node)] = None
65 else:
66 if list_type_field.get("value") is None:
67 list_type = None
68 else:
69 list_type = list_type_field["value"].lower().strip()
70 lists_types[(service, node)] = list_type
71
72 data["list_type"] = template_data["list_type"] = list_type
73
74 extra = self.get_pubsub_extra(request)
75 extra["labels_as_list"] = C.BOOL_TRUE
76 self.handle_search(request, extra)
77
78 list_raw = await self.host.bridge_call(
79 "list_get",
80 service.full() if service else "",
81 node,
82 C.NO_LIMIT,
83 [],
84 "",
85 data_format.serialise(extra),
86 profile,
87 )
88 if profile != C.SERVICE_PROFILE:
89 try:
90 affiliations = await self.host.bridge_call(
91 "ps_node_affiliations_get",
92 service.full() if service else "",
93 submitted_node,
94 profile
95 )
96 except BridgeException as e:
97 log.warning(
98 f"Can't get affiliations for node {submitted_node!r} at {service}: {e}"
99 )
100 template_data["owner"] = False
101 else:
102 is_owner = affiliations.get(self.get_jid(request).userhost()) == 'owner'
103 template_data["owner"] = is_owner
104 if is_owner:
105 self.expose_to_scripts(
106 request,
107 affiliations={str(e): str(a) for e, a in affiliations.items()}
108 )
109 else:
110 template_data["owner"] = False
111
112 list_items, metadata = data_format.deserialise(list_raw, type_check=list)
113 template_data["list_items"] = [
114 template_xmlui.create(self.host, x) for x in list_items
115 ]
116 view_url = self.get_page_by_name('list_view').get_url(service.full(), node or '@')
117 template_data["on_list_item_click"] = data_objects.OnClick(
118 url=f"{view_url}/{{item.id}}"
119 )
120 self.set_pagination(request, metadata)
121 self.expose_to_scripts(
122 request,
123 lists_ns=self.host.ns_map["tickets"],
124 pubsub_service=service.full(),
125 pubsub_node=node,
126 list_type=list_type,
127 )
128
129
130 async def on_data_post(self, request):
131 data = self.get_r_data(request)
132 profile = self.get_profile(request)
133 service = data["service"]
134 node = data["node"]
135 list_type = self.get_posted_data(request, ("type",))
136 if list_type == "grocery":
137 name, quantity = self.get_posted_data(request, ("name", "quantity"))
138 if not name:
139 self.page_error(request, C.HTTP_BAD_REQUEST)
140 item_data = {
141 "name": [name],
142 }
143 if quantity:
144 item_data["quantity"] = [quantity]
145 await self.host.bridge_call(
146 "list_set", service.full(), node, item_data, "", "", "", profile
147 )
148 return C.POST_NO_CONFIRM
149 else:
150 raise NotImplementedError(
151 f"Can't use quick list item set for list of type {list_type!r}"
152 )