1058
|
1 #!/usr/bin/env python2.7 |
|
2 # -*- coding: utf-8 -*- |
|
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 |
|
9 log = getLogger('pages/forums/topics') |
|
10 |
|
11 name = u"forum_topics" |
|
12 access = C.PAGES_ACCESS_PUBLIC |
|
13 template = u"forum/view_topics.html" |
|
14 |
|
15 |
|
16 def parse_url(self, request): |
|
17 self.getPathArgs(request, ['service', 'node'], 2, |
|
18 service = u'jid') |
|
19 |
|
20 |
|
21 @defer.inlineCallbacks |
|
22 def prepare_render(self, request): |
|
23 profile = self.getProfile(request) or C.SERVICE_PROFILE |
|
24 data = self.getRData(request) |
|
25 service, node = data[u'service'], data[u'node'] |
|
26 request.template_data.update({u'service': service, u'node': node}) |
|
27 template_data = request.template_data |
|
28 topics, metadata = yield self.host.bridgeCall(u'forumTopicsGet', service.full(), node, {}, profile) |
|
29 template_data[u'identities'] = identities = {} |
|
30 for topic in topics: |
|
31 parsed_uri = xmpp_uri.parseXMPPUri(topic[u'uri']) |
|
32 author = topic[u'author'] |
|
33 topic[u'http_uri'] = self.getPageByName(u'forum_view').getURL(parsed_uri[u'path'], parsed_uri[u'node']) |
|
34 if author not in identities: |
|
35 identities[topic[u'author']] = yield self.host.bridgeCall(u'identityGet', author, profile) |
|
36 template_data[u'topics'] = topics |
|
37 |
|
38 @defer.inlineCallbacks |
|
39 def on_data_post(self, request): |
|
40 profile = self.getProfile(request) |
|
41 if profile is None: |
|
42 self.pageError(request, C.HTTP_UNAUTHORIZED) |
|
43 type_ = self.getPostedData(request, u'type') |
|
44 if type_ == u'new_topic': |
|
45 service, node, title, body = self.getPostedData(request, (u'service', u'node', u'title', u'body')) |
|
46 |
|
47 if not title or not body: |
|
48 self.pageError(request, C.HTTP_BAD_REQUEST) |
|
49 topic_data = {u"title": title, u"content": body} |
|
50 try: |
|
51 yield self.host.bridgeCall(u'forumTopicCreate', service, node, topic_data, profile) |
|
52 except Exception as e: |
|
53 if u"forbidden" in unicode(e): |
|
54 self.pageError(request, 401) |
|
55 else: |
|
56 raise e |
|
57 else: |
|
58 log.warning(_(u"Unhandled data type: {}").format(type_)) |