comparison src/pages/common/blog/page_meta.py @ 1036:f5661761b1b9

pages (common/blog): URL improvments: - if target_profile is set, user_blog is used for links instead of blog, making the URL nicer (by using profile instead of service and node) - added part of title or content to URL, to make it more explicit
author Goffi <goffi@goffi.org>
date Mon, 22 Jan 2018 22:25:37 +0100
parents 78b7b5ec7ca1
children 6b906b1f419a
comparison
equal deleted inserted replaced
1035:09f5a5d649df 1036:f5661761b1b9
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 9 from sat.core.log import getLogger
10 from sat.tools.common.template import safe 10 from sat.tools.common.template import safe
11 from server import utils
12 import re
11 import urllib 13 import urllib
12 import cgi 14 import cgi
13 log = getLogger('pages/common/blog') 15 log = getLogger('pages/common/blog')
14 16
15 """generic blog (with service/node provided)""" 17 """generic blog (with service/node provided)"""
16 name = u'blog' 18 name = u'blog'
17 template = u"blog/articles.html" 19 template = u"blog/articles.html"
18 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'} 20 uri_handlers = {(u'pubsub', u'microblog'): 'microblog_uri'}
19 21
22 RE_TEXT_URL = re.compile(ur'[^a-zA-Zéèêôà,_]+')
23 TEXT_MAX_LEN = 60
20 24
21 def microblog_uri(self, uri_data): 25 def microblog_uri(self, uri_data):
22 service = urllib.quote_plus(uri_data[u'path']) 26 service = urllib.quote_plus(uri_data[u'path'])
23 node = urllib.quote_plus(uri_data[u'node']) 27 node = urllib.quote_plus(uri_data[u'node'])
24 return service + u'/' + node 28 return service + u'/' + node
62 if filter_kw == u'id': 66 if filter_kw == u'id':
63 try: 67 try:
64 data[u'item'] = self.nextPath(request) 68 data[u'item'] = self.nextPath(request)
65 except IndexError: 69 except IndexError:
66 self.pageError(request, C.HTTP_BAD_REQUEST) 70 self.pageError(request, C.HTTP_BAD_REQUEST)
71 # we get one more argument in case text has been added to have a nice URL
72 try:
73 self.nextPath(request)
74 except IndexError:
75 pass
67 elif filter_kw == u'tag': 76 elif filter_kw == u'tag':
68 try: 77 try:
69 data[u'tag'] = self.nextPath(request) 78 data[u'tag'] = self.nextPath(request)
70 except IndexError: 79 except IndexError:
71 self.pageError(request, C.HTTP_BAD_REQUEST) 80 self.pageError(request, C.HTTP_BAD_REQUEST)
150 if tag: 159 if tag:
151 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag 160 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
152 161
153 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile) 162 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile)
154 template_data = request.template_data 163 template_data = request.template_data
164 target_profile = template_data.get(u'target_profile')
165
155 if items: 166 if items:
156 if not item_id: 167 if not item_id:
157 last_id = items[-1].id 168 last_id = items[-1].id
158 template_data['older_url'] = self.getParamURL(request, after=last_id) 169 template_data['older_url'] = self.getParamURL(request, after=last_id)
159 if u'before' in params or u'after' in params: 170 if u'before' in params or u'after' in params:
175 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities 186 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
176 187
177 if show_comments: 188 if show_comments:
178 yield appendComments(self, items, identities, profile) 189 yield appendComments(self, items, identities, profile)
179 190
180 blog_view = self.getPageByName(u'blog_view') 191 # if we know the profile, we use it instead of service + blog (nicer url)
181 template_data[u'items'] = data[u'items'] = items 192 if target_profile is None:
182 if request.args.get('reverse') == ['1']: 193 blog_base_url = self.getPageByName(u'blog_view').getURL(service.full(), node or u'@')
183 template_data[u'items'].items.reverse() 194 else:
184 template_data[u'items_http_uri'] = items_http_uri = {} 195 blog_base_url = self.getURLByNames([(u'user', [target_profile]), (u'user_blog', [])])
185 template_data[u'tags_http_uri'] = tags_http_uri = {} 196 # we also set the background image if specified by user
186 for item in items:
187 service_s = service.full()
188 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_view.getURL(service_s, node or '@', u'id', item.id))
189 for tag in item.tags:
190 if tag not in tags_http_uri:
191 tags_http_uri[tag] = self.host.getExtBaseURL(request, blog_view.getURL(service_s, node or '@', u'tag', tag))
192 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
193 if u'target_profile' in template_data:
194 bg_img = yield self.host.bridgeCall(u'asyncGetParamA', u'Background', u'Blog page', u'value', -1, template_data[u'target_profile']) 197 bg_img = yield self.host.bridgeCall(u'asyncGetParamA', u'Background', u'Blog page', u'value', -1, template_data[u'target_profile'])
195 if bg_img: 198 if bg_img:
196 template_data['dynamic_style'] = safe(u""" 199 template_data['dynamic_style'] = safe(u"""
197 :root { 200 :root {
198 --bg-img: url("%s"); 201 --bg-img: url("%s");
199 } 202 }
200 """ % cgi.escape(bg_img, True)) 203 """ % cgi.escape(bg_img, True))
204
205 template_data[u'items'] = data[u'items'] = items
206 if request.args.get('reverse') == ['1']:
207 template_data[u'items'].items.reverse()
208 template_data[u'items_http_uri'] = items_http_uri = {}
209 template_data[u'tags_http_uri'] = tags_http_uri = {}
210 for item in items:
211 # we add text from title or body at the end of URL
212 # to make it more readable
213 text = item.title or item.content
214 text = RE_TEXT_URL.sub(u'-', text).lower().strip(u'-')
215 while len(text) > TEXT_MAX_LEN:
216 if u'-' in text:
217 text = text.rsplit(u'-', 1)[0]
218 else:
219 text = text[:TEXT_MAX_LEN]
220
221 # now we can compute item URL
222 blog_url = u'/'.join([blog_base_url, u'id', utils.quote(item.id), text])
223 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url)
224 for tag in item.tags:
225 if tag not in tags_http_uri:
226 tag_url = u'/'.join([blog_base_url, u'tag', utils.quote(tag)])
227 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)
228 template_data[u'allow_commenting'] = data.get(u'allow_commenting', False)
201 229
202 230
203 @defer.inlineCallbacks 231 @defer.inlineCallbacks
204 def on_data_post(self, request): 232 def on_data_post(self, request):
205 profile = self.getProfile(request) 233 profile = self.getProfile(request)