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