Mercurial > libervia-web
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libervia/web/pages/lists/view/page_meta.py Fri Jun 02 16:49:28 2023 +0200 @@ -0,0 +1,152 @@ +#!/usr/bin/env python3 + +from libervia.backend.tools.common import template_xmlui +from libervia.backend.tools.common import data_objects +from libervia.backend.tools.common import data_format +from libervia.backend.core.log import getLogger +from libervia.frontends.bridge.bridge_frontend import BridgeException +from libervia.web.server.constants import Const as C + +log = getLogger(__name__) + +name = "lists" +access = C.PAGES_ACCESS_PUBLIC +template = "list/overview.html" + + +def parse_url(self, request): + self.get_path_args(request, ["service", "node"], service="jid") + data = self.get_r_data(request) + service, node = data["service"], data["node"] + if node is None: + self.http_redirect(request, self.get_page_by_name("lists_disco").url) + if node == "@": + node = data["node"] = "" + template_data = request.template_data + template_data["url_list_items"] = self.get_url(service.full(), node or "@") + template_data["url_list_new"] = self.get_page_by_name("list_new").get_url( + service.full(), node or "@") + + +async def prepare_render(self, request): + data = self.get_r_data(request) + template_data = request.template_data + service, node = data["service"], data["node"] + submitted_node = await self.host.bridge_call( + "ps_schema_submitted_node_get", + node or self.host.ns_map["tickets"] + ) + profile = self.get_profile(request) or C.SERVICE_PROFILE + + self.check_cache(request, C.CACHE_PUBSUB, service=service, node=node, short="tickets") + + try: + lists_types = self.get_page_data(request, "lists_types") + if lists_types is None: + lists_types = {} + self.set_page_data(request, "lists_types", lists_types) + list_type = lists_types[(service, node)] + except KeyError: + ns_tickets_type = self.host.ns_map["tickets_type"] + schema_raw = await self.host.bridge_call( + "ps_schema_dict_get", + service.full(), + submitted_node, + profile + ) + schema = data_format.deserialise(schema_raw) + try: + list_type_field = next( + f for f in schema["fields"] if f["type"] == "hidden" + and f.get("name") == ns_tickets_type + ) + except StopIteration: + list_type = lists_types[(service, node)] = None + else: + if list_type_field.get("value") is None: + list_type = None + else: + list_type = list_type_field["value"].lower().strip() + lists_types[(service, node)] = list_type + + data["list_type"] = template_data["list_type"] = list_type + + extra = self.get_pubsub_extra(request) + extra["labels_as_list"] = C.BOOL_TRUE + self.handle_search(request, extra) + + list_raw = await self.host.bridge_call( + "list_get", + service.full() if service else "", + node, + C.NO_LIMIT, + [], + "", + data_format.serialise(extra), + profile, + ) + if profile != C.SERVICE_PROFILE: + try: + affiliations = await self.host.bridge_call( + "ps_node_affiliations_get", + service.full() if service else "", + submitted_node, + profile + ) + except BridgeException as e: + log.warning( + f"Can't get affiliations for node {submitted_node!r} at {service}: {e}" + ) + template_data["owner"] = False + else: + is_owner = affiliations.get(self.get_jid(request).userhost()) == 'owner' + template_data["owner"] = is_owner + if is_owner: + self.expose_to_scripts( + request, + affiliations={str(e): str(a) for e, a in affiliations.items()} + ) + else: + template_data["owner"] = False + + list_items, metadata = data_format.deserialise(list_raw, type_check=list) + template_data["list_items"] = [ + template_xmlui.create(self.host, x) for x in list_items + ] + view_url = self.get_page_by_name('list_view').get_url(service.full(), node or '@') + template_data["on_list_item_click"] = data_objects.OnClick( + url=f"{view_url}/{{item.id}}" + ) + self.set_pagination(request, metadata) + self.expose_to_scripts( + request, + lists_ns=self.host.ns_map["tickets"], + pubsub_service=service.full(), + pubsub_node=node, + list_type=list_type, + ) + + +async def on_data_post(self, request): + data = self.get_r_data(request) + profile = self.get_profile(request) + service = data["service"] + node = data["node"] + list_type = self.get_posted_data(request, ("type",)) + if list_type == "grocery": + name, quantity = self.get_posted_data(request, ("name", "quantity")) + if not name: + self.page_error(request, C.HTTP_BAD_REQUEST) + item_data = { + "name": [name], + } + if quantity: + item_data["quantity"] = [quantity] + await self.host.bridge_call( + "list_set", service.full(), node, item_data, "", "", "", profile + ) + return C.POST_NO_CONFIRM + else: + raise NotImplementedError( + f"Can't use quick list item set for list of type {list_type!r}" + )