comparison sat/tools/common/data_objects.py @ 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 56f94936df1e
children 2d353b74010e
comparison
equal deleted inserted replaced
2765:378188abe941 2766:93a421de0e3d
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """Objects handling bridge data, with jinja2 safe markup handling""" 20 """Objects handling bridge data, with jinja2 safe markup handling"""
21 21
22 from sat.core.constants import Const as C
22 from sat.tools.common import data_format 23 from sat.tools.common import data_format
23 24
24 try: 25 try:
25 from jinja2 import Markup as safe 26 from jinja2 import Markup as safe
26 except ImportError: 27 except ImportError:
147 148
148 149
149 class BlogItems(object): 150 class BlogItems(object):
150 def __init__(self, mb_data): 151 def __init__(self, mb_data):
151 self.items = [BlogItem(i, self) for i in mb_data[0]] 152 self.items = [BlogItem(i, self) for i in mb_data[0]]
152 self.metadata = mb_data[1] 153 self.metadata = self._parseMetadata(mb_data[1])
153 154
154 @property 155 @property
155 def service(self): 156 def service(self):
156 return self.metadata[u"service"] 157 return self.metadata[u"service"]
157 158
160 return self.metadata[u"node"] 161 return self.metadata[u"node"]
161 162
162 @property 163 @property
163 def uri(self): 164 def uri(self):
164 return self.metadata[u"uri"] 165 return self.metadata[u"uri"]
166
167 @property
168 def with_rsm(self):
169 """Return True if RSM is activated on this request"""
170 return u"rsm_first" in self.metadata
171
172 @property
173 def rsm_first(self):
174 return self.metadata[u"rsm_first"]
175
176 @property
177 def rsm_last(self):
178 return self.metadata[u"rsm_last"]
179
180 @property
181 def rsm_index(self):
182 return self.metadata[u"rsm_index"]
183
184 @property
185 def rsm_count(self):
186 return self.metadata[u"rsm_count"]
187
188 @property
189 def complete(self):
190 return self.metadata[u"complete"]
191
192 def _parseMetadata(self, metadata):
193 data = {}
194
195 for key, value in metadata.iteritems():
196 if key in (u"rsm_index", u"rsm_count"):
197 value = int(value)
198 elif key == u"mam_stable":
199 value = C.bool(value)
200 elif key == u"mam_complete":
201 key = u"complete"
202 value = C.bool(value)
203 data[key] = value
204 if u"complete" not in data:
205 index = data.get(u"rsm_index")
206 count = data.get(u"rsm_count")
207 if index is None or count is None:
208 # we don't have enough information to know if the data is complete or not
209 data[u"complete"] = None
210 else:
211 # normally we have a strict equality here but XEP-0059 states
212 # that index MAY be approximative, so just in caseā€¦
213 data[u"complete"] = index + len(self.items) >= count
214 return data
165 215
166 def __len__(self): 216 def __len__(self):
167 return self.items.__len__() 217 return self.items.__len__()
168 218
169 def __missing__(self, key): 219 def __missing__(self, key):