Mercurial > libervia-web
annotate libervia/pages/forums/topics/page_meta.py @ 1338:9bfd2d0661b2
pages: fixed handling of DataError when posting data
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 15 Aug 2020 23:28:44 +0200 |
parents | 6dfcdbeb0d33 |
children | bcd6ad99b2b0 |
rev | line source |
---|---|
1216 | 1 #!/usr/bin/env python3 |
1239 | 2 |
1058 | 3 |
4 from libervia.server.constants import Const as C | |
5 from twisted.internet import defer | |
6 from sat.core.i18n import _ | |
7 from sat.core.log import getLogger | |
8 from sat.tools.common import uri as xmpp_uri | |
1243
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
9 from sat.tools.common import data_format |
1270
6dfcdbeb0d33
pages: handle themes using `theme` request argument
Goffi <goffi@goffi.org>
parents:
1266
diff
changeset
|
10 from libervia.server import session_iface |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
11 |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
12 log = getLogger(__name__) |
1058 | 13 |
1216 | 14 name = "forum_topics" |
1058 | 15 access = C.PAGES_ACCESS_PUBLIC |
1216 | 16 template = "forum/view_topics.html" |
1058 | 17 |
18 | |
19 def parse_url(self, request): | |
1216 | 20 self.getPathArgs(request, ["service", "node"], 2, service="jid") |
1058 | 21 |
22 | |
23 @defer.inlineCallbacks | |
24 def prepare_render(self, request): | |
25 profile = self.getProfile(request) or C.SERVICE_PROFILE | |
26 data = self.getRData(request) | |
1216 | 27 service, node = data["service"], data["node"] |
28 request.template_data.update({"service": service, "node": node}) | |
1058 | 29 template_data = request.template_data |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
30 topics, metadata = yield self.host.bridgeCall( |
1216 | 31 "forumTopicsGet", service.full(), node, {}, profile |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
32 ) |
1266
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1243
diff
changeset
|
33 identities = self.host.getSessionData( |
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1243
diff
changeset
|
34 request, session_iface.ISATSession |
6b7f9c3558cc
server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents:
1243
diff
changeset
|
35 ).identities |
1058 | 36 for topic in topics: |
1216 | 37 parsed_uri = xmpp_uri.parseXMPPUri(topic["uri"]) |
38 author = topic["author"] | |
39 topic["http_uri"] = self.getPageByName("forum_view").getURL( | |
40 parsed_uri["path"], parsed_uri["node"] | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
41 ) |
1058 | 42 if author not in identities: |
1243
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
43 id_raw = yield self.host.bridgeCall( |
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
44 "identityGet", author, [], True, profile |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
45 ) |
1243
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
46 identities[topic["author"]] = data_format.deserialise(id_raw) |
8aff742d0dd0
pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
47 |
1216 | 48 template_data["topics"] = topics |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
49 |
1058 | 50 |
51 @defer.inlineCallbacks | |
52 def on_data_post(self, request): | |
53 profile = self.getProfile(request) | |
54 if profile is None: | |
1173
0f37b65fe7c2
server: replaced wrong usage of C.HTTP_UNAUTHORIZED by C.HTTP_FORBIDDEN
Goffi <goffi@goffi.org>
parents:
1145
diff
changeset
|
55 self.pageError(request, C.HTTP_FORBIDDEN) |
1216 | 56 type_ = self.getPostedData(request, "type") |
57 if type_ == "new_topic": | |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
58 service, node, title, body = self.getPostedData( |
1216 | 59 request, ("service", "node", "title", "body") |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
60 ) |
1058 | 61 |
62 if not title or not body: | |
63 self.pageError(request, C.HTTP_BAD_REQUEST) | |
1216 | 64 topic_data = {"title": title, "content": body} |
1058 | 65 try: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
66 yield self.host.bridgeCall( |
1216 | 67 "forumTopicCreate", service, node, topic_data, profile |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1058
diff
changeset
|
68 ) |
1058 | 69 except Exception as e: |
1216 | 70 if "forbidden" in str(e): |
1058 | 71 self.pageError(request, 401) |
72 else: | |
73 raise e | |
74 else: | |
1216 | 75 log.warning(_("Unhandled data type: {}").format(type_)) |