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.
This commit is contained in:
parent
7a9ee35cfc
commit
d88d2a7d8f
1 changed files with 51 additions and 1 deletions
|
|
@ -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 BlogItem(object):
|
|||
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 @@ class BlogItems(object):
|
|||
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__()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue