comparison src/pages/common/blog/page_meta.py @ 1057:50ba8947a6e8

common/blog: "service" and "node" are now set in template_data
author Goffi <goffi@goffi.org>
date Tue, 30 Jan 2018 08:04:33 +0100
parents d3ac6fb10fd5
children 7dc20bacd3b6
comparison
equal deleted inserted replaced
1056:47c354ca66a3 1057:50ba8947a6e8
4 from twisted.words.protocols.jabber import jid 4 from twisted.words.protocols.jabber import jid
5 from twisted.internet import defer 5 from twisted.internet import defer
6 from sat.tools.common import data_objects 6 from sat.tools.common import data_objects
7 from libervia.server import session_iface 7 from libervia.server import session_iface
8 from sat.core.i18n import _ 8 from sat.core.i18n import _
9 from sat.core.log import getLogger
10 from sat.tools.common.template import safe 9 from sat.tools.common.template import safe
11 from libervia.server import utils 10 from libervia.server import utils
12 import unicodedata 11 import unicodedata
13 import re 12 import re
14 import cgi 13 import cgi
14 from sat.core.log import getLogger
15 log = getLogger('pages/common/blog') 15 log = getLogger('pages/common/blog')
16 16
17 """generic blog (with service/node provided)""" 17 """generic blog (with service/node provided)"""
18 name = u'blog' 18 name = u'blog'
19 template = u"blog/articles.html" 19 template = u"blog/articles.html"
21 21
22 RE_TEXT_URL = re.compile(ur'[^a-zA-Z,_]+') 22 RE_TEXT_URL = re.compile(ur'[^a-zA-Z,_]+')
23 TEXT_MAX_LEN = 60 23 TEXT_MAX_LEN = 60
24 TEXT_WORD_MIN_LENGHT = 4 24 TEXT_WORD_MIN_LENGHT = 4
25 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended 25 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended
26
26 27
27 def microblog_uri(self, uri_data): 28 def microblog_uri(self, uri_data):
28 args = [uri_data[u'path'], uri_data[u'node']] 29 args = [uri_data[u'path'], uri_data[u'node']]
29 if u'item' in uri_data: 30 if u'item' in uri_data:
30 args.extend([u'id', uri_data[u'item']]) 31 args.extend([u'id', uri_data[u'item']])
149 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) 150 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)
150 profile = self.getProfile(request) 151 profile = self.getProfile(request)
151 if profile is None: 152 if profile is None:
152 profile = C.SERVICE_PROFILE 153 profile = C.SERVICE_PROFILE
153 154
155 ## pagination/filtering parameters
154 params = self.getAllPostedData(request, multiple=False) 156 params = self.getAllPostedData(request, multiple=False)
155 if item_id: 157 if item_id:
156 extra = {} 158 extra = {}
157 else: 159 else:
158 extra = {u'rsm_max': u'10'} 160 extra = {u'rsm_max': u'10'}
162 extra[u'rsm_before'] = params[u'before'] 164 extra[u'rsm_before'] = params[u'before']
163 tag = data.get('tag') 165 tag = data.get('tag')
164 if tag: 166 if tag:
165 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag 167 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
166 168
169 ## main data ##
170 # we get data from backend/XMPP here
167 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile) 171 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile)
172
173 ## navigation ##
174 # no let's fill service, node and pagination URLs
168 template_data = request.template_data 175 template_data = request.template_data
176 if u'service' not in template_data:
177 template_data[u'service'] = service
178 if u'node' not in template_data:
179 template_data[u'node'] = node
169 target_profile = template_data.get(u'target_profile') 180 target_profile = template_data.get(u'target_profile')
170 181
171 if items: 182 if items:
172 if not item_id: 183 if not item_id:
173 last_id = items[-1].id 184 last_id = items[-1].id
186 blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile) 197 blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile)
187 if items: 198 if items:
188 last_id = items[-1].id 199 last_id = items[-1].id
189 template_data['older_url'] = self.getParamURL(request, after=last_id) 200 template_data['older_url'] = self.getParamURL(request, after=last_id)
190 201
202 ## identities ##
203 # identities are use to show nice nickname or avatars
191 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities 204 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
192 205
206 ## Comments ##
207 # if comments are requested, we need to take them
193 if show_comments: 208 if show_comments:
194 yield appendComments(self, items, identities, profile) 209 yield appendComments(self, items, identities, profile)
195 210
211 ## URLs ##
212 # We will fill items_http_uri and tags_http_uri in template_data with suitable urls
196 # if we know the profile, we use it instead of service + blog (nicer url) 213 # if we know the profile, we use it instead of service + blog (nicer url)
197 if target_profile is None: 214 if target_profile is None:
198 blog_base_url_item = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'id') 215 blog_base_url_item = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'id')
199 blog_base_url_tag = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'tag') 216 blog_base_url_tag = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'tag')
200 else: 217 else:
212 template_data[u'items'] = data[u'items'] = items 229 template_data[u'items'] = data[u'items'] = items
213 if request.args.get('reverse') == ['1']: 230 if request.args.get('reverse') == ['1']:
214 template_data[u'items'].items.reverse() 231 template_data[u'items'].items.reverse()
215 template_data[u'items_http_uri'] = items_http_uri = {} 232 template_data[u'items_http_uri'] = items_http_uri = {}
216 template_data[u'tags_http_uri'] = tags_http_uri = {} 233 template_data[u'tags_http_uri'] = tags_http_uri = {}
234
235
217 for item in items: 236 for item in items:
218 blog_canonical_url = u'/'.join([blog_base_url_item, utils.quote(item.id)]) 237 blog_canonical_url = u'/'.join([blog_base_url_item, utils.quote(item.id)])
219 if len(blog_canonical_url) > URL_LIMIT_MARK: 238 if len(blog_canonical_url) > URL_LIMIT_MARK:
220 blog_url = blog_canonical_url 239 blog_url = blog_canonical_url
221 else: 240 else:
222 # we add text from title or body at the end of URL 241 # we add text from title or body at the end of URL
223 # to make it more readable 242 # to make it more human readable
224 text = item.title or item.content 243 text = item.title or item.content
225 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567 244 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567
226 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore') 245 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore')
227 text = RE_TEXT_URL.sub(u' ', text).lower() 246 text = RE_TEXT_URL.sub(u' ', text).lower()
228 text = u'-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) 247 text = u'-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT])
239 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url) 258 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url)
240 for tag in item.tags: 259 for tag in item.tags:
241 if tag not in tags_http_uri: 260 if tag not in tags_http_uri:
242 tag_url = u'/'.join([blog_base_url_tag, utils.quote(tag)]) 261 tag_url = u'/'.join([blog_base_url_tag, utils.quote(tag)])
243 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url) 262 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)
263
264 # if True, page should display a comment box
244 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False) 265 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
245 266
246 267
247 @defer.inlineCallbacks 268 @defer.inlineCallbacks
248 def on_data_post(self, request): 269 def on_data_post(self, request):