comparison libervia/pages/blog/view/page_meta.py @ 1140:e1a953512f72

pages (blog/view): pagination improvments: - getBlogData renamed to getBlogItems and only returns BlogItems objects, blog_data are not returned anymore as they are already included in the metadata attribute - desired page size can be specified using "page_max" key in request data - better use of RSM data to know if we should set previous or next page URLs - don't display anymore the first page is no items are found, this was confusing and not really making sense
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 16:45:01 +0100
parents dfd6545a205a
children 02fc28aac2b6
comparison
equal deleted inserted replaced
1139:e45480b6ba24 1140:e1a953512f72
119 comments = data_objects.BlogItems(comments_data) 119 comments = data_objects.BlogItems(comments_data)
120 blog_item.appendCommentsItems(comments) 120 blog_item.appendCommentsItems(comments)
121 yield appendComments(self, comments, identities, profile) 121 yield appendComments(self, comments, identities, profile)
122 122
123 @defer.inlineCallbacks 123 @defer.inlineCallbacks
124 def getBlogData(self, request, service, node, item_id, extra, profile): 124 def getBlogItems(self, request, service, node, item_id, extra, profile):
125 try: 125 try:
126 if item_id: 126 if item_id:
127 items_id = [item_id] 127 items_id = [item_id]
128 else: 128 else:
129 items_id = [] 129 items_id = []
141 else: 141 else:
142 log.warning(_(u"can't retrieve blog for [{service}]: {msg}".format( 142 log.warning(_(u"can't retrieve blog for [{service}]: {msg}".format(
143 service = service.userhost(), msg=e))) 143 service = service.userhost(), msg=e)))
144 blog_data = ([], {}) 144 blog_data = ([], {})
145 145
146 items = data_objects.BlogItems(blog_data) 146 defer.returnValue(data_objects.BlogItems(blog_data))
147 defer.returnValue((blog_data, items))
148 147
149 @defer.inlineCallbacks 148 @defer.inlineCallbacks
150 def prepare_render(self, request): 149 def prepare_render(self, request):
151 data = self.getRData(request) 150 data = self.getRData(request)
151 page_max = data.get(u"page_max", 10)
152 # if the comments are not explicitly hidden, we show them 152 # if the comments are not explicitly hidden, we show them
153 service, node, item_id, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'item'), data.get(u'show_comments', True) 153 service, node, item_id, show_comments = data.get(u'service', u''), data.get(u'node', u''), data.get(u'item'), data.get(u'show_comments', True)
154 profile = self.getProfile(request) 154 profile = self.getProfile(request)
155 if profile is None: 155 if profile is None:
156 profile = C.SERVICE_PROFILE 156 profile = C.SERVICE_PROFILE
163 if item_id: 163 if item_id:
164 extra = {} 164 extra = {}
165 else: 165 else:
166 extra = { 166 extra = {
167 C.KEY_ORDER_BY: C.ORDER_BY_CREATION, 167 C.KEY_ORDER_BY: C.ORDER_BY_CREATION,
168 u'rsm_max': u'10', 168 u'rsm_max': unicode(page_max),
169 } 169 }
170 if u'after' in params: 170 if u'after' in params:
171 extra[u'rsm_after'] = params[u'after'] 171 extra[u'rsm_after'] = params[u'after']
172 elif u'before' in params: 172 elif u'before' in params:
173 extra[u'rsm_before'] = params[u'before'] 173 extra[u'rsm_before'] = params[u'before']
175 if tag: 175 if tag:
176 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag 176 extra[u'mam_filter_{}'.format(C.MAM_FILTER_CATEGORY)] = tag
177 177
178 ## main data ## 178 ## main data ##
179 # we get data from backend/XMPP here 179 # we get data from backend/XMPP here
180 blog_data, items = yield getBlogData(self, request, service, node, item_id, extra, profile) 180 items = yield getBlogItems(self, request, service, node, item_id, extra, profile)
181 181
182 ## navigation ## 182 ## navigation ##
183 # no let's fill service, node and pagination URLs 183 # no let's fill service, node and pagination URLs
184 template_data = request.template_data 184 template_data = request.template_data
185 if u'service' not in template_data: 185 if u'service' not in template_data:
187 if u'node' not in template_data: 187 if u'node' not in template_data:
188 template_data[u'node'] = node 188 template_data[u'node'] = node
189 target_profile = template_data.get(u'target_profile') 189 target_profile = template_data.get(u'target_profile')
190 190
191 if items: 191 if items:
192 if not item_id: 192 if items.with_rsm and not item_id:
193 last_id = items[-1].id 193 last_id = items.rsm_last
194 template_data['older_url'] = self.getParamURL(request, after=last_id) 194
195 if u'before' in params or u'after' in params: 195 if not items.complete:
196 first_id = items[0].id 196 # we also show the page next button if complete is None because we
197 # can't know where we are in the feed in this case.
198 template_data['older_url'] = self.getParamURL(request, after=last_id)
199 if items.metadata.get("rsm_index", 1) > 0:
200 # We only show previous button if it's not the first page already.
201 # If we have no index, we default to display the button anyway
202 # as we can't know if we are on the first page or not.
203 first_id = items.rsm_first
197 template_data['newer_url'] = self.getParamURL(request, before=first_id) 204 template_data['newer_url'] = self.getParamURL(request, before=first_id)
198 else: 205 else:
199 if item_id: 206 if item_id:
200 # if item id has been specified in URL and it's not found, 207 # if item id has been specified in URL and it's not found,
201 # we must return an error 208 # we must return an error
202 self.pageError(request, C.HTTP_NOT_FOUND) 209 self.pageError(request, C.HTTP_NOT_FOUND)
203 210
204 # no items, we have requested items before last post, or blog is empty
205 extra = {
206 C.KEY_ORDER_BY: C.ORDER_BY_CREATION,
207 u'rsm_max': u'10',
208 }
209 blog_data, items = yield getBlogData(self, request, service, node, None, extra, profile)
210 if items:
211 last_id = items[-1].id
212 template_data['older_url'] = self.getParamURL(request, after=last_id)
213 211
214 ## identities ## 212 ## identities ##
215 # identities are used to show nice nickname or avatars 213 # identities are used to show nice nickname or avatars
216 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities 214 identities = template_data[u'identities'] = self.host.getSessionData(request, session_iface.ISATSession).identities
217 215