changeset 2766:93a421de0e3d

tools (common/data_objects): metadata parsing in BlogItems: metadata are parsed to deserialise some well known values like rsm index and count, and some properties have been added to have easier access. A "complete" property (or item in metadata) is set to True if we are on the last page, False if we are not, or None if we don't have enough data to know if we are on the last page or not.
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 16:35:13 +0100
parents 378188abe941
children a97c43dc4924
files sat/tools/common/data_objects.py
diffstat 1 files changed, 51 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/data_objects.py	Fri Jan 11 11:13:15 2019 +0100
+++ b/sat/tools/common/data_objects.py	Fri Jan 11 16:35:13 2019 +0100
@@ -19,6 +19,7 @@
 
 """Objects handling bridge data, with jinja2 safe markup handling"""
 
+from sat.core.constants import Const as C
 from sat.tools.common import data_format
 
 try:
@@ -149,7 +150,7 @@
 class BlogItems(object):
     def __init__(self, mb_data):
         self.items = [BlogItem(i, self) for i in mb_data[0]]
-        self.metadata = mb_data[1]
+        self.metadata = self._parseMetadata(mb_data[1])
 
     @property
     def service(self):
@@ -163,6 +164,55 @@
     def uri(self):
         return self.metadata[u"uri"]
 
+    @property
+    def with_rsm(self):
+        """Return True if RSM is activated on this request"""
+        return u"rsm_first" in self.metadata
+
+    @property
+    def rsm_first(self):
+        return self.metadata[u"rsm_first"]
+
+    @property
+    def rsm_last(self):
+        return self.metadata[u"rsm_last"]
+
+    @property
+    def rsm_index(self):
+        return self.metadata[u"rsm_index"]
+
+    @property
+    def rsm_count(self):
+        return self.metadata[u"rsm_count"]
+
+    @property
+    def complete(self):
+        return self.metadata[u"complete"]
+
+    def _parseMetadata(self, metadata):
+        data = {}
+
+        for key, value in metadata.iteritems():
+            if key in (u"rsm_index", u"rsm_count"):
+                value = int(value)
+            elif key == u"mam_stable":
+                value = C.bool(value)
+            elif key == u"mam_complete":
+                key = u"complete"
+                value = C.bool(value)
+            data[key] = value
+        if u"complete" not in data:
+            index = data.get(u"rsm_index")
+            count = data.get(u"rsm_count")
+            if index is None or count is None:
+                # we don't have enough information to know if the data is complete or not
+                data[u"complete"] = None
+            else:
+                # normally we have a strict equality here but XEP-0059 states
+                # that index MAY be approximative, so just in caseā€¦
+                data[u"complete"] = index + len(self.items) >= count
+        return data
+
     def __len__(self):
         return self.items.__len__()