comparison libervia/web/pages/forums/topics/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/forums/topics/page_meta.py@106bae41f5c8
children
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3
4 from libervia.web.server.constants import Const as C
5 from libervia.backend.core.i18n import _, D_
6 from libervia.backend.core.log import getLogger
7 from libervia.backend.tools.common import uri as xmpp_uri
8 from libervia.backend.tools.common import data_format
9 from libervia.web.server import session_iface
10
11 log = getLogger(__name__)
12
13 name = "forum_topics"
14 label = D_("Forum Topics")
15 access = C.PAGES_ACCESS_PUBLIC
16 template = "forum/view_topics.html"
17
18
19 def parse_url(self, request):
20 self.get_path_args(request, ["service", "node"], 2, service="jid")
21
22
23 def add_breadcrumb(self, request, breadcrumbs):
24 data = self.get_r_data(request)
25 breadcrumbs.append({
26 "label": label,
27 "url": self.get_url(data["service"].full(), data["node"])
28 })
29
30
31 async def prepare_render(self, request):
32 profile = self.get_profile(request) or C.SERVICE_PROFILE
33 data = self.get_r_data(request)
34 service, node = data["service"], data["node"]
35 request.template_data.update({"service": service, "node": node})
36 template_data = request.template_data
37 page_max = data.get("page_max", 20)
38 extra = self.get_pubsub_extra(request, page_max=page_max)
39 topics, metadata = await self.host.bridge_call(
40 "forum_topics_get",
41 service.full(),
42 node,
43 extra,
44 profile
45 )
46 metadata = data_format.deserialise(metadata)
47 self.set_pagination(request, metadata)
48 identities = self.host.get_session_data(
49 request, session_iface.IWebSession
50 ).identities
51 for topic in topics:
52 parsed_uri = xmpp_uri.parse_xmpp_uri(topic["uri"])
53 author = topic["author"]
54 topic["http_uri"] = self.get_page_by_name("forum_view").get_url(
55 parsed_uri["path"], parsed_uri["node"]
56 )
57 if author not in identities:
58 id_raw = await self.host.bridge_call(
59 "identity_get", author, [], True, profile
60 )
61 identities[topic["author"]] = data_format.deserialise(id_raw)
62
63 template_data["topics"] = topics
64 template_data["url_topic_new"] = self.get_sub_page_url(request, "forum_topic_new")
65
66
67 async def on_data_post(self, request):
68 profile = self.get_profile(request)
69 if profile is None:
70 self.page_error(request, C.HTTP_FORBIDDEN)
71 type_ = self.get_posted_data(request, "type")
72 if type_ == "new_topic":
73 service, node, title, body = self.get_posted_data(
74 request, ("service", "node", "title", "body")
75 )
76
77 if not title or not body:
78 self.page_error(request, C.HTTP_BAD_REQUEST)
79 topic_data = {"title": title, "content": body}
80 try:
81 await self.host.bridge_call(
82 "forum_topic_create", service, node, topic_data, profile
83 )
84 except Exception as e:
85 if "forbidden" in str(e):
86 self.page_error(request, 401)
87 else:
88 raise e
89 else:
90 log.warning(_("Unhandled data type: {}").format(type_))