annotate src/pages/blog/view/page_meta.py @ 1117:d2036b2db6be

server: replaced glib2reactor by gireactor
author Goffi <goffi@goffi.org>
date Sun, 15 Jul 2018 09:01:34 +0200
parents ff503f23ac37
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1077
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 from libervia.server.constants import Const as C
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from twisted.words.protocols.jabber import jid
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from twisted.internet import defer
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from sat.tools.common import data_objects
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 from libervia.server import session_iface
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat.core.i18n import _
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 from sat.tools.common.template import safe
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 from sat.tools.common import uri
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 from libervia.server import utils
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 import unicodedata
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 import re
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 import cgi
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 from sat.core.log import getLogger
1083
ff503f23ac37 pages: fixed calls to "blog" instead of "blog_view"
Goffi <goffi@goffi.org>
parents: 1077
diff changeset
16 log = getLogger('pages/blog/view')
1077
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 """generic blog (with service/node provided)"""
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 name = u'blog_view'
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 template = u"blog/articles.html"
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 RE_TEXT_URL = re.compile(ur'[^a-zA-Z,_]+')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 TEXT_MAX_LEN = 60
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 TEXT_WORD_MIN_LENGHT = 4
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def microblog_uri(self, uri_data):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 args = [uri_data[u'path'], uri_data[u'node']]
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 if u'item' in uri_data:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 args.extend([u'id', uri_data[u'item']])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 return self.getURL(*args)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def parse_url(self, request):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 """URL is /[service]/[node]/[filter_keyword]/[item]|[other]
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 if [node] is '@', default namespace is used
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if a value is unset, default one will be used
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 keyword can be one of:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 id: next value is a item id
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 tag: next value is a blog tag
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 """
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 data = self.getRData(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 service = self.nextPath(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 except IndexError:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 data['service'] = u''
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 data[u"service"] = jid.JID(service)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 except Exception:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 log.warning(_(u"bad service entered: {}").format(service))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.pageError(request, C.HTTP_BAD_REQUEST)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 data['node'] = self.nextPath(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 except IndexError:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 data['node'] = u''
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 if data['node'] == u'@':
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 data['node'] = u''
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 filter_kw = data['filter_keyword'] = self.nextPath(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 except IndexError:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 pass
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if filter_kw == u'id':
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 data[u'item'] = self.nextPath(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 except IndexError:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.pageError(request, C.HTTP_BAD_REQUEST)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 # we get one more argument in case text has been added to have a nice URL
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.nextPath(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 except IndexError:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 pass
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 elif filter_kw == u'tag':
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 data[u'tag'] = self.nextPath(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 except IndexError:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.pageError(request, C.HTTP_BAD_REQUEST)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 # invalid filter keyword
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 log.warning(_(u"invalid filter keyword: {filter_kw}").format(filter_kw=filter_kw))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.pageError(request, C.HTTP_BAD_REQUEST)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 @defer.inlineCallbacks
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def appendComments(self, blog_items, identities, profile):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 for blog_item in blog_items:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if identities is not None:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 author = blog_item.author_jid
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if not author:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 log.warning(_(u"no author found for item {item_id}").format(item_id=blog_item.id))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if author not in identities:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 for comment_data in blog_item.comments:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 service = comment_data[u'service']
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 node = comment_data[u'node']
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 comments_data = yield self.host.bridgeCall(u'mbGet',
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 service,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 node,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 C.NO_LIMIT,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 [],
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 {},
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 except Exception as e:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 log.warning(_(u"Can't get comments at {node} (service: {service}): {msg}").format(
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 service=service,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 node=node,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 msg=e))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 continue
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 comments = data_objects.BlogItems(comments_data)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 blog_item.appendCommentsItems(comments)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 yield appendComments(self, comments, identities, profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 @defer.inlineCallbacks
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 def getBlogData(self, request, service, node, item_id, extra, profile):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 if item_id:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 items_id = [item_id]
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 items_id = []
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 blog_data = yield self.host.bridgeCall(u'mbGet',
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 service.userhost(),
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 node,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 C.NO_LIMIT,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 items_id,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 extra,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 except Exception as e:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 # FIXME: need a better way to test errors in bridge errback
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if u"forbidden" in unicode(e):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 self.pageError(request, 401)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 log.warning(_(u"can't retrieve blog for [{service}]: {msg}".format(
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 service = service.userhost(), msg=e)))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 blog_data = ([], {})
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 items = data_objects.BlogItems(blog_data)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 defer.returnValue((blog_data, items))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
148
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 @defer.inlineCallbacks
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 def prepare_render(self, request):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 data = self.getRData(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 # if the comments are not explicitly hidden, we show them
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 service, node, item_id, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'item'), data.get(u'show_comments', True)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 profile = self.getProfile(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if profile is None:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 profile = C.SERVICE_PROFILE
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 ## pagination/filtering parameters
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 params = self.getAllPostedData(request, multiple=False)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if item_id:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 extra = {}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 extra = {u'rsm_max': u'10'}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 if u'after' in params:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 extra[u'rsm_after'] = params[u'after']
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 elif u'before' in params:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 extra[u'rsm_before'] = params[u'before']
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 tag = data.get('tag')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 if tag:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 ## main data ##
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 # we get data from backend/XMPP here
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 ## navigation ##
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 # no let's fill service, node and pagination URLs
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 template_data = request.template_data
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 if u'service' not in template_data:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 template_data[u'service'] = service
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 if u'node' not in template_data:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 template_data[u'node'] = node
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 target_profile = template_data.get(u'target_profile')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if items:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 if not item_id:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 last_id = items[-1].id
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 template_data['older_url'] = self.getParamURL(request, after=last_id)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if u'before' in params or u'after' in params:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 first_id = items[0].id
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 template_data['newer_url'] = self.getParamURL(request, before=first_id)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if item_id:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 # if item id has been specified in URL and it's not found,
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 # we must return an error
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 self.pageError(request, C.HTTP_NOT_FOUND)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
197
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 # no items, we have requested items before last post, or blog is empty
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 extra = {u'rsm_max': u'10'}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 if items:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 last_id = items[-1].id
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 template_data['older_url'] = self.getParamURL(request, after=last_id)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
204
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 ## identities ##
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 # identities are use to show nice nickname or avatars
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
208
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 ## Comments ##
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 # if comments are requested, we need to take them
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 if show_comments:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 yield appendComments(self, items, identities, profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 ## URLs ##
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 # We will fill items_http_uri and tags_http_uri in template_data with suitable urls
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 # if we know the profile, we use it instead of service + blog (nicer url)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if target_profile is None:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 blog_base_url_item = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'id')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 blog_base_url_tag = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'tag')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 blog_base_url_item = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [u'id'])])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 blog_base_url_tag = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [u'tag'])])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 # we also set the background image if specified by user
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 bg_img = yield self.host.bridgeCall(u'asyncGetParamA', u'Background', u'Blog page', u'value', -1, template_data[u'target_profile'])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 if bg_img:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 template_data['dynamic_style'] = safe(u"""
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 :root {
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 --bg-img: url("%s");
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 }
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 """ % cgi.escape(bg_img, True))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
231
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 template_data[u'items'] = data[u'items'] = items
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 if request.args.get('reverse') == ['1']:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 template_data[u'items'].items.reverse()
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 template_data[u'items_http_uri'] = items_http_uri = {}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 template_data[u'tags_http_uri'] = tags_http_uri = {}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
237
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 for item in items:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 blog_canonical_url = u'/'.join([blog_base_url_item, utils.quote(item.id)])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 if len(blog_canonical_url) > URL_LIMIT_MARK:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 blog_url = blog_canonical_url
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 # we add text from title or body at the end of URL
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 # to make it more human readable
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 text = item.title or item.content
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 text = RE_TEXT_URL.sub(u' ', text).lower()
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 text = u'-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 while len(text) > TEXT_MAX_LEN:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if u'-' in text:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 text = text.rsplit(u'-', 1)[0]
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 text = text[:TEXT_MAX_LEN]
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 if text:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 blog_url = blog_canonical_url + u'/' + text
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 blog_url = blog_canonical_url
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 for tag in item.tags:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 if tag not in tags_http_uri:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 tag_url = u'/'.join([blog_base_url_tag, utils.quote(tag)])
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
266
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 # if True, page should display a comment box
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
269
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 # last but not least, we add a xmpp: link to the node
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 uri_args = {u'path': service.full()}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 if node:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 uri_args[u'node'] = node
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 if item_id:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 uri_args[u'item'] = item_id
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 template_data[u'xmpp_uri'] = uri.buildXMPPUri(u'pubsub', subtype='microblog', **uri_args)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
277
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
278
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 @defer.inlineCallbacks
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 def on_data_post(self, request):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 profile = self.getProfile(request)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 if profile is None:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 self.pageError(request, C.HTTP_UNAUTHORIZED)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 type_ = self.getPostedData(request, u'type')
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 if type_ == u'comment':
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 service, node, body = self.getPostedData(request, (u'service', u'node', u'body'))
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
287
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 if not body:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 self.pageError(request, C.HTTP_BAD_REQUEST)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 comment_data = {u"content": body}
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 try:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 yield self.host.bridgeCall(u'mbSend', service, node, comment_data, profile)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 except Exception as e:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 if u"forbidden" in unicode(e):
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 self.pageError(request, 401)
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 raise e
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 else:
880ea673aaff blog: moved blog page from /common to /blog:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 log.warning(_(u"Unhandled data type: {}").format(type_))