comparison libervia/pages/blog/view/page_meta.py @ 1302:04e7dd6b6f4d

pages (blog, tickets, merge-requests): updated code to handle new serialisation, following backend changes
author Goffi <goffi@goffi.org>
date Thu, 16 Jul 2020 09:08:47 +0200
parents 6b7f9c3558cc
children a0954b6610aa
comparison
equal deleted inserted replaced
1301:ff44f822bfdd 1302:04e7dd6b6f4d
109 "title": "{service}'s blog".format(service=service)}) 109 "title": "{service}'s blog".format(service=service)})
110 110
111 111
112 @defer.inlineCallbacks 112 @defer.inlineCallbacks
113 def appendComments(self, blog_items, identities, profile): 113 def appendComments(self, blog_items, identities, profile):
114 for blog_item in blog_items: 114 for blog_item in blog_items['items']:
115 if identities is not None: 115 if identities is not None:
116 author = blog_item.author_jid 116 author = blog_item['author_jid']
117 if not author: 117 if not author:
118 log.warning(_("no author found for item {item_id}").format(item_id=blog_item.id)) 118 log.warning(_("no author found for item {item_id}").format(
119 item_id=blog_item['id']))
119 else: 120 else:
120 if author not in identities: 121 if author not in identities:
121 id_raw = yield self.host.bridgeCall( 122 id_raw = yield self.host.bridgeCall(
122 'identityGet', author, [], True, profile) 123 'identityGet', author, [], True, profile)
123 identities[author] = data_format.deserialise(id_raw) 124 identities[author] = data_format.deserialise(id_raw)
124 for comment_data in blog_item.comments: 125 for comment_data in blog_item['comments']:
125 service = comment_data['service'] 126 service = comment_data['service']
126 node = comment_data['node'] 127 node = comment_data['node']
127 try: 128 try:
128 comments_data = yield self.host.bridgeCall('mbGet', 129 comments_data = yield self.host.bridgeCall('mbGet',
129 service, 130 service,
131 C.NO_LIMIT, 132 C.NO_LIMIT,
132 [], 133 [],
133 {C.KEY_ORDER_BY: C.ORDER_BY_CREATION}, 134 {C.KEY_ORDER_BY: C.ORDER_BY_CREATION},
134 profile) 135 profile)
135 except Exception as e: 136 except Exception as e:
136 log.warning(_("Can't get comments at {node} (service: {service}): {msg}").format( 137 log.warning(
137 service=service, 138 _("Can't get comments at {node} (service: {service}): {msg}").format(
138 node=node, 139 service=service,
139 msg=e)) 140 node=node,
141 msg=e))
140 continue 142 continue
141 143
142 comments = data_objects.BlogItems(comments_data) 144 comments = data_format.deserialise(comments_data)
143 blog_item.appendCommentsItems(comments) 145 comment_data['items'] = comments['items']
144 yield appendComments(self, comments, identities, profile) 146 yield appendComments(self, comments, identities, profile)
145 147
146 @defer.inlineCallbacks 148 @defer.inlineCallbacks
147 def getBlogItems(self, request, service, node, item_id, extra, profile): 149 def getBlogItems(self, request, service, node, item_id, extra, profile):
148 try: 150 try:
162 if "forbidden" in str(e): 164 if "forbidden" in str(e):
163 self.pageError(request, 401) 165 self.pageError(request, 401)
164 else: 166 else:
165 log.warning(_("can't retrieve blog for [{service}]: {msg}".format( 167 log.warning(_("can't retrieve blog for [{service}]: {msg}".format(
166 service = service.userhost(), msg=e))) 168 service = service.userhost(), msg=e)))
167 blog_data = ([], {}) 169 blog_data = {"items": []}
168 170 else:
169 defer.returnValue(data_objects.BlogItems(blog_data)) 171 blog_data = data_format.deserialise(blog_data)
172
173 defer.returnValue(blog_data)
170 174
171 @defer.inlineCallbacks 175 @defer.inlineCallbacks
172 def prepare_render(self, request): 176 def prepare_render(self, request):
173 data = self.getRData(request) 177 data = self.getRData(request)
174 page_max = data.get("page_max", 10) 178 page_max = data.get("page_max", 10)
190 if tag: 194 if tag:
191 extra['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag 195 extra['mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
192 196
193 ## main data ## 197 ## main data ##
194 # we get data from backend/XMPP here 198 # we get data from backend/XMPP here
195 items = yield getBlogItems(self, request, service, node, item_id, extra, profile) 199 blog_items = yield getBlogItems(self, request, service, node, item_id, extra, profile)
196 200
197 ## navigation ## 201 ## navigation ##
198 # no let's fill service, node and pagination URLs 202 # no let's fill service, node and pagination URLs
199 template_data = request.template_data 203 template_data = request.template_data
200 if 'service' not in template_data: 204 if 'service' not in template_data:
201 template_data['service'] = service 205 template_data['service'] = service
202 if 'node' not in template_data: 206 if 'node' not in template_data:
203 template_data['node'] = node 207 template_data['node'] = node
204 target_profile = template_data.get('target_profile') 208 target_profile = template_data.get('target_profile')
205 209
206 if items: 210 if blog_items:
207 if not item_id: 211 if not item_id:
208 self.setPagination(request, items.metadata) 212 self.setPagination(request, blog_items)
209 else: 213 else:
210 if item_id: 214 if item_id:
211 # if item id has been specified in URL and it's not found, 215 # if item id has been specified in URL and it's not found,
212 # we must return an error 216 # we must return an error
213 self.pageError(request, C.HTTP_NOT_FOUND) 217 self.pageError(request, C.HTTP_NOT_FOUND)
217 identities = self.host.getSessionData(request, session_iface.ISATSession).identities 221 identities = self.host.getSessionData(request, session_iface.ISATSession).identities
218 222
219 ## Comments ## 223 ## Comments ##
220 # if comments are requested, we need to take them 224 # if comments are requested, we need to take them
221 if show_comments: 225 if show_comments:
222 yield appendComments(self, items, identities, profile) 226 yield appendComments(self, blog_items, identities, profile)
223 227
224 ## URLs ## 228 ## URLs ##
225 # We will fill items_http_uri and tags_http_uri in template_data with suitable urls 229 # We will fill items_http_uri and tags_http_uri in template_data with suitable urls
226 # if we know the profile, we use it instead of service + blog (nicer url) 230 # if we know the profile, we use it instead of service + blog (nicer url)
227 if target_profile is None: 231 if target_profile is None:
237 :root { 241 :root {
238 --bg-img: url("%s"); 242 --bg-img: url("%s");
239 } 243 }
240 """ % html.escape(bg_img, True)) 244 """ % html.escape(bg_img, True))
241 245
242 template_data['items'] = data['items'] = items 246 template_data['blog_items'] = data['blog_items'] = blog_items
243 if request.args.get(b'reverse') == ['1']: 247 if request.args.get(b'reverse') == ['1']:
244 template_data['items'].items.reverse() 248 template_data['blog_items'].items.reverse()
245 template_data['items_http_uri'] = items_http_uri = {} 249 template_data['items_http_uri'] = items_http_uri = {}
246 template_data['tags_http_uri'] = tags_http_uri = {} 250 template_data['tags_http_uri'] = tags_http_uri = {}
247 251
248 252
249 for item in items: 253 for item in blog_items['items']:
250 blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item.id)]) 254 blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item['id'])])
251 if len(blog_canonical_url) > URL_LIMIT_MARK: 255 if len(blog_canonical_url) > URL_LIMIT_MARK:
252 blog_url = blog_canonical_url 256 blog_url = blog_canonical_url
253 else: 257 else:
254 # we add text from title or body at the end of URL 258 # we add text from title or body at the end of URL
255 # to make it more human readable 259 # to make it more human readable
256 text = item.title or item.content 260 text = item.get('title', item['content'])
257 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567 261 # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567
258 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8') 262 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8')
259 text = RE_TEXT_URL.sub(' ', text).lower() 263 text = RE_TEXT_URL.sub(' ', text).lower()
260 text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) 264 text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT])
261 while len(text) > TEXT_MAX_LEN: 265 while len(text) > TEXT_MAX_LEN:
266 if text: 270 if text:
267 blog_url = blog_canonical_url + '/' + text 271 blog_url = blog_canonical_url + '/' + text
268 else: 272 else:
269 blog_url = blog_canonical_url 273 blog_url = blog_canonical_url
270 274
271 items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url) 275 items_http_uri[item['id']] = self.host.getExtBaseURL(request, blog_url)
272 for tag in item.tags: 276 for tag in item['tags']:
273 if tag not in tags_http_uri: 277 if tag not in tags_http_uri:
274 tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)]) 278 tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)])
275 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url) 279 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)
276 280
277 # if True, page should display a comment box 281 # if True, page should display a comment box