comparison src/pages/common/blog/page_meta.py @ 1042:f7d45c989c05

pages (common/blog): use more friendly URLs
author Goffi <goffi@goffi.org>
date Wed, 24 Jan 2018 09:58:00 +0100
parents 90b11cd6f28f
children d4290178662c
comparison
equal deleted inserted replaced
1041:688b52897ba0 1042:f7d45c989c05
18 template = u"blog/articles.html" 18 template = u"blog/articles.html"
19 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'} 19 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}
20 20
21 RE_TEXT_URL = re.compile(ur'[^a-zA-Zéèêôà,_]+') 21 RE_TEXT_URL = re.compile(ur'[^a-zA-Zéèêôà,_]+')
22 TEXT_MAX_LEN = 60 22 TEXT_MAX_LEN = 60
23 TEXT_WORD_MIN_LENGHT = 4
24 URL_LIMIT_MARK = 90 # if canonical URL is longer than that, text will not be appended
23 25
24 def microblog_uri(self, uri_data): 26 def microblog_uri(self, uri_data):
25 args = [uri_data[u'path'], uri_data[u'node']] 27 args = [uri_data[u'path'], uri_data[u'node']]
26 if u'item' in uri_data: 28 if u'item' in uri_data:
27 args.extend([u'id', uri_data[u'item']]) 29 args.extend([u'id', uri_data[u'item']])
190 if show_comments: 192 if show_comments:
191 yield appendComments(self, items, identities, profile) 193 yield appendComments(self, items, identities, profile)
192 194
193 # if we know the profile, we use it instead of service + blog (nicer url) 195 # if we know the profile, we use it instead of service + blog (nicer url)
194 if target_profile is None: 196 if target_profile is None:
195 blog_base_url = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@') 197 blog_base_url_item = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'id')
196 else: 198 blog_base_url_tag = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@', u'tag')
197 blog_base_url = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [])]) 199 else:
200 blog_base_url_item = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [u'id'])])
201 blog_base_url_tag = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [u'tag'])])
198 # we also set the background image if specified by user 202 # we also set the background image if specified by user
199 bg_img = yield self.host.bridgeCall(u'asyncGetParamA', u'Background', u'Blog page', u'value', -1, template_data[u'target_profile']) 203 bg_img = yield self.host.bridgeCall(u'asyncGetParamA', u'Background', u'Blog page', u'value', -1, template_data[u'target_profile'])
200 if bg_img: 204 if bg_img:
201 template_data['dynamic_style'] = safe(u""" 205 template_data['dynamic_style'] = safe(u"""
202 :root { 206 :root {
208 if request.args.get('reverse') == ['1']: 212 if request.args.get('reverse') == ['1']:
209 template_data[u'items'].items.reverse() 213 template_data[u'items'].items.reverse()
210 template_data[u'items_http_uri'] = items_http_uri = {} 214 template_data[u'items_http_uri'] = items_http_uri = {}
211 template_data[u'tags_http_uri'] = tags_http_uri = {} 215 template_data[u'tags_http_uri'] = tags_http_uri = {}
212 for item in items: 216 for item in items:
213 # we add text from title or body at the end of URL 217 blog_canonical_url = u'/'.join([blog_base_url_item, utils.quote(item.id)])
214 # to make it more readable 218 if len(blog_canonical_url) > URL_LIMIT_MARK:
215 text = item.title or item.content 219 blog_url = blog_canonical_url
216 text = RE_TEXT_URL.sub(u'-', text).lower().strip(u'-') 220 else:
217 while len(text) > TEXT_MAX_LEN: 221 # we add text from title or body at the end of URL
218 if u'-' in text: 222 # to make it more readable
219 text = text.rsplit(u'-', 1)[0] 223 text = item.title or item.content
224 text = RE_TEXT_URL.sub(u' ', text).lower()
225 text = u'-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT])
226 while len(text) > TEXT_MAX_LEN:
227 if u'-' in text:
228 text = text.rsplit(u'-', 1)[0]
229 else:
230 text = text[:TEXT_MAX_LEN]
231 if text:
232 blog_url = blog_canonical_url + u'/' + text
220 else: 233 else:
221 text = text[:TEXT_MAX_LEN] 234 blog_url = blog_canonical_url
222 235
223 # now we can compute item URL
224 blog_url = u'/'.join([blog_base_url, u'id', utils.quote(item.id), text])
225 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url) 236 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url)
226 for tag in item.tags: 237 for tag in item.tags:
227 if tag not in tags_http_uri: 238 if tag not in tags_http_uri:
228 tag_url = u'/'.join([blog_base_url, u'tag', utils.quote(tag)]) 239 tag_url = u'/'.join([blog_base_url_tag, utils.quote(tag)])
229 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url) 240 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)
230 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False) 241 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
231 242
232 243
233 @defer.inlineCallbacks 244 @defer.inlineCallbacks