diff 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
line wrap: on
line diff
--- a/libervia/pages/blog/view/page_meta.py	Fri Jun 19 16:47:51 2020 +0200
+++ b/libervia/pages/blog/view/page_meta.py	Thu Jul 16 09:08:47 2020 +0200
@@ -111,17 +111,18 @@
 
 @defer.inlineCallbacks
 def appendComments(self, blog_items, identities, profile):
-    for blog_item in blog_items:
+    for blog_item in blog_items['items']:
         if identities is not None:
-            author = blog_item.author_jid
+            author = blog_item['author_jid']
             if not author:
-                log.warning(_("no author found for item {item_id}").format(item_id=blog_item.id))
+                log.warning(_("no author found for item {item_id}").format(
+                    item_id=blog_item['id']))
             else:
                 if author not in identities:
                     id_raw = yield self.host.bridgeCall(
                         'identityGet', author, [], True, profile)
                     identities[author] = data_format.deserialise(id_raw)
-        for comment_data in blog_item.comments:
+        for comment_data in blog_item['comments']:
             service = comment_data['service']
             node = comment_data['node']
             try:
@@ -133,14 +134,15 @@
                                       {C.KEY_ORDER_BY: C.ORDER_BY_CREATION},
                                       profile)
             except Exception as e:
-                log.warning(_("Can't get comments at {node} (service: {service}): {msg}").format(
-                    service=service,
-                    node=node,
-                    msg=e))
+                log.warning(
+                    _("Can't get comments at {node} (service: {service}): {msg}").format(
+                        service=service,
+                        node=node,
+                        msg=e))
                 continue
 
-            comments = data_objects.BlogItems(comments_data)
-            blog_item.appendCommentsItems(comments)
+            comments = data_format.deserialise(comments_data)
+            comment_data['items'] = comments['items']
             yield appendComments(self, comments, identities, profile)
 
 @defer.inlineCallbacks
@@ -164,9 +166,11 @@
         else:
             log.warning(_("can't retrieve blog for [{service}]: {msg}".format(
                 service = service.userhost(), msg=e)))
-            blog_data = ([], {})
+            blog_data = {"items": []}
+    else:
+        blog_data = data_format.deserialise(blog_data)
 
-    defer.returnValue(data_objects.BlogItems(blog_data))
+    defer.returnValue(blog_data)
 
 @defer.inlineCallbacks
 def prepare_render(self, request):
@@ -192,7 +196,7 @@
 
     ## main data ##
     # we get data from backend/XMPP here
-    items = yield getBlogItems(self, request, service, node, item_id, extra, profile)
+    blog_items = yield getBlogItems(self, request, service, node, item_id, extra, profile)
 
     ## navigation ##
     # no let's fill service, node and pagination URLs
@@ -203,9 +207,9 @@
         template_data['node'] = node
     target_profile = template_data.get('target_profile')
 
-    if items:
+    if blog_items:
         if not item_id:
-            self.setPagination(request, items.metadata)
+            self.setPagination(request, blog_items)
     else:
         if item_id:
             # if item id has been specified in URL and it's not found,
@@ -219,7 +223,7 @@
     ## Comments ##
     # if comments are requested, we need to take them
     if show_comments:
-        yield appendComments(self, items, identities, profile)
+        yield appendComments(self, blog_items, identities, profile)
 
     ## URLs ##
     # We will fill items_http_uri and tags_http_uri in template_data with suitable urls
@@ -239,21 +243,21 @@
                 }
                 """ % html.escape(bg_img, True))
 
-    template_data['items'] = data['items'] = items
+    template_data['blog_items'] = data['blog_items'] = blog_items
     if request.args.get(b'reverse') == ['1']:
-        template_data['items'].items.reverse()
+        template_data['blog_items'].items.reverse()
     template_data['items_http_uri'] = items_http_uri = {}
     template_data['tags_http_uri'] = tags_http_uri = {}
 
 
-    for item in items:
-        blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item.id)])
+    for item in blog_items['items']:
+        blog_canonical_url = '/'.join([blog_base_url_item, utils.quote(item['id'])])
         if len(blog_canonical_url) > URL_LIMIT_MARK:
             blog_url = blog_canonical_url
         else:
             # we add text from title or body at the end of URL
             # to make it more human readable
-            text = item.title or item.content
+            text = item.get('title', item['content'])
             # we change special chars to ascii one, trick found at https://stackoverflow.com/a/3194567
             text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8')
             text = RE_TEXT_URL.sub(' ', text).lower()
@@ -268,8 +272,8 @@
             else:
                 blog_url = blog_canonical_url
 
-        items_http_uri[item.id] = self.host.getExtBaseURL(request, blog_url)
-        for tag in item.tags:
+        items_http_uri[item['id']] = self.host.getExtBaseURL(request, blog_url)
+        for tag in item['tags']:
             if tag not in tags_http_uri:
                 tag_url = '/'.join([blog_base_url_tag, utils.quote(tag)])
                 tags_http_uri[tag] = self.host.getExtBaseURL(request, tag_url)