comparison src/pages/common/blog/page_meta.py @ 928:ee243d48100e

pages (common): added common hierarchy for reusable pages (blog is the first)
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 18:24:24 +0200
parents
children af6a62e21053
comparison
equal deleted inserted replaced
927:bb4dfc2802c0 928:ee243d48100e
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3 from libervia.server.constants import Const as C
4 from twisted.words.protocols.jabber import jid
5 from twisted.internet import defer
6 from sat.tools.common import data_objects
7 from sat.core.i18n import _
8 from sat.core.log import getLogger
9 import urllib
10 log = getLogger('pages/common/blog')
11
12 """generic blog (with service/node provided)"""
13 name = u'blog'
14 template = u"blog/articles.html"
15 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}
16
17
18 def microblog_uri(self, uri_data):
19 service = urllib.quote_plus(uri_data[u'path'])
20 node = urllib.quote_plus(uri_data[u'node'])
21 return service + u'/' + node
22
23
24 def parse_url(self, request):
25 """URL is /[service]/[node]
26
27 if [node] is not found, default namespace is used
28 if both [service] and [node] are not found, default service is used too
29 """
30 data = self.getRData(request)
31
32 try:
33 service = self.nextPath(request)
34 except IndexError:
35 data['service'] = u''
36 else:
37 try:
38 data[u"service"] = jid.JID(service)
39 except Exception:
40 log.warning(_(u"bad service entered: {}").format(service))
41 self.pageError(request, C.HTTP_BAD_REQUEST)
42
43 try:
44 data['node'] = self.nextPath(request)
45 except IndexError:
46 data['node'] = u''
47
48
49 @defer.inlineCallbacks
50 def prepare_render(self, request):
51 data = self.getRData(request)
52 service, node = data.get(u'service', u''), data.get(u'node', u'')
53 profile = self.getProfile(request)
54 if profile is None:
55 profile = C.SERVICE_PROFILE
56
57 try:
58 blog_data = yield self.host.bridge.mbGet(
59 service.userhost(),
60 node,
61 10,
62 [],
63 {},
64 profile)
65 except Exception as e:
66 # FIXME: need a better way to test errors in bridge errback
67 if u"forbidden" in unicode(e):
68 self.pageError(request, 401)
69 else:
70 raise e
71
72 request.template_data[u'items'] = data_objects.BlogItems(blog_data)