comparison libervia/web/pages/lists/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/page_meta.py@106bae41f5c8
children
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3 from libervia.web.server.constants import Const as C
4 from twisted.words.protocols.jabber import jid
5 from libervia.backend.core.i18n import _, D_
6 from libervia.backend.core.log import getLogger
7 from libervia.backend.tools.common import data_format
8
9 log = getLogger(__name__)
10
11 name = "lists_disco"
12 label = D_("Lists Discovery")
13 access = C.PAGES_ACCESS_PUBLIC
14 template = "list/discover.html"
15
16 async def prepare_render(self, request):
17 profile = self.get_profile(request)
18 template_data = request.template_data
19 template_data["url_list_create"] = self.get_page_by_name("list_create").url
20 lists_directory_config = self.host.options["lists_directory_json"]
21 lists_directory = request.template_data["lists_directory"] = []
22
23 if lists_directory_config:
24 try:
25 for list_data in lists_directory_config:
26 service = list_data["service"]
27 node = list_data["node"]
28 name = list_data["name"]
29 url = self.get_page_by_name("lists").get_url(service, node)
30 lists_directory.append({"name": name, "url": url, "from_config": True})
31 except KeyError as e:
32 log.warning("Missing field in lists_directory_json: {msg}".format(msg=e))
33 except Exception as e:
34 log.warning("Can't decode lists directory: {msg}".format(msg=e))
35
36 if profile is not None:
37 try:
38 lists_list_raw = await self.host.bridge_call("lists_list", "", "", profile)
39 except Exception as e:
40 log.warning(
41 _("Can't get list of registered lists for {profile}: {reason}")
42 .format(profile=profile, reason=e)
43 )
44 else:
45 lists_list = data_format.deserialise(lists_list_raw, type_check=list)
46 for list_data in lists_list:
47 service = list_data["service"]
48 node = list_data["node"]
49 list_data["url"] = self.get_page_by_name("lists").get_url(service, node)
50 list_data["from_config"] = False
51 lists_directory.append(list_data)
52
53 icons_names = set()
54 for list_data in lists_directory:
55 try:
56 icons_names.add(list_data['icon_name'])
57 except KeyError:
58 pass
59 if icons_names:
60 template_data["icons_names"] = icons_names
61
62
63 def on_data_post(self, request):
64 jid_str = self.get_posted_data(request, "jid")
65 try:
66 jid_ = jid.JID(jid_str)
67 except RuntimeError:
68 self.page_error(request, C.HTTP_BAD_REQUEST)
69 # for now we just use default node
70 url = self.get_page_by_name("lists").get_url(jid_.full(), "@")
71 self.http_redirect(request, url)