comparison src/server/pages.py @ 1038:6b906b1f419a

pages: fixed XMPP URIs handling
author Goffi <goffi@goffi.org>
date Wed, 24 Jan 2018 09:57:57 +0100
parents c34f08e05cdf
children 688b52897ba0
comparison
equal deleted inserted replaced
1037:dc6c8c4d8ff6 1038:6b906b1f419a
280 except KeyError: 280 except KeyError:
281 log.error(_(u'missing {name} method to handle {1}/{2}').format( 281 log.error(_(u'missing {name} method to handle {1}/{2}').format(
282 name = cb_name, *uri_tuple)) 282 name = cb_name, *uri_tuple))
283 continue 283 continue
284 else: 284 else:
285 cls.registerURI(uri_tuple, cb, new_path) 285 resource.registerURI(uri_tuple, cb)
286 286
287 LiberviaPage.importPages(host, resource, new_path) 287 LiberviaPage.importPages(host, resource, new_path)
288 288
289 @classmethod 289 @classmethod
290 def setMenu(cls, menus): 290 def setMenu(cls, menus):
308 log.error(_(u"Can'find a named page ({msg}), please check menu_json in configuration.").format(msg=e)) 308 log.error(_(u"Can'find a named page ({msg}), please check menu_json in configuration.").format(msg=e))
309 raise e 309 raise e
310 main_menu.append((page_name, url)) 310 main_menu.append((page_name, url))
311 cls.main_menu = main_menu 311 cls.main_menu = main_menu
312 312
313 @classmethod 313 def registerURI(self, uri_tuple, get_uri_cb):
314 def registerURI(cls, uri_tuple, get_uri_cb, pre_path):
315 """register a URI handler 314 """register a URI handler
316 315
317 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler 316 @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
318 type/subtype as returned by tools/common/parseXMPPUri 317 type/subtype as returned by tools/common/parseXMPPUri
318 or type/None to handle all subtypes
319 @param get_uri_cb(callable): method which take uri_data dict as only argument 319 @param get_uri_cb(callable): method which take uri_data dict as only argument
320 and return path with correct arguments relative to page itself 320 and return absolute path with correct arguments or None if the page
321 @param pre_path(list[unicode]): prefix path to reference the handler page 321 can't handle this URL
322 """ 322 """
323 if uri_tuple in cls.uri_callbacks: 323 if uri_tuple in self.uri_callbacks:
324 log.info(_(u"{}/{} URIs are already handled, replacing by the new handler").format(*uri_tuple)) 324 log.info(_(u"{}/{} URIs are already handled, replacing by the new handler").format(*uri_tuple))
325 cls.uri_callbacks[uri_tuple] = {u'callback': get_uri_cb, 325 self.uri_callbacks[uri_tuple] = (self, get_uri_cb)
326 u'pre_path': pre_path}
327 326
328 def registerSignal(self, request, signal, check_profile=True): 327 def registerSignal(self, request, signal, check_profile=True):
329 r"""register a signal handler 328 r"""register a signal handler
330 329
331 the page must be dynamic 330 the page must be dynamic
346 log.error(_(u"You can't register signal if page is not dynamic")) 345 log.error(_(u"You can't register signal if page is not dynamic"))
347 return 346 return
348 LiberviaPage.signals_handlers.setdefault(signal, {})[id(request)] = (self, request, check_profile) 347 LiberviaPage.signals_handlers.setdefault(signal, {})[id(request)] = (self, request, check_profile)
349 request._signals_registered.append(signal) 348 request._signals_registered.append(signal)
350 349
351 def getPagePathFromURI(self, uri): 350 @classmethod
351 def getPagePathFromURI(cls, uri):
352 """Retrieve page URL from xmpp: URI 352 """Retrieve page URL from xmpp: URI
353 353
354 @param uri(unicode): URI with a xmpp: scheme 354 @param uri(unicode): URI with a xmpp: scheme
355 @return (unicode,None): absolute path (starting from root "/") to page handling the URI 355 @return (unicode,None): absolute path (starting from root "/") to page handling the URI
356 None is returned if not page has been registered for this URI 356 None is returned if no page has been registered for this URI
357 """ 357 """
358 uri_data = common_uri.parseXMPPUri(uri) 358 uri_data = common_uri.parseXMPPUri(uri)
359 try: 359 try:
360 callback_data = self.uri_callbacks[uri_data['type'], uri_data.get('sub_type')] 360 page, cb = cls.uri_callbacks[uri_data['type'], uri_data['sub_type']]
361 except KeyError: 361 except KeyError:
362 return 362 url = None
363 else: 363 else:
364 url = os.path.join(u'/', u'/'.join(callback_data['pre_path']), callback_data['callback'](self, uri_data)) 364 url = cb(page, uri_data)
365 if url is None:
366 # no handler found
367 # we try to find a more generic one
368 try:
369 page, cb = cls.uri_callbacks[uri_data['type'], None]
370 except KeyError:
371 pass
372 else:
373 url = cb(page, uri_data)
365 return url 374 return url
366 375
367 @classmethod 376 @classmethod
368 def getPageByName(cls, name): 377 def getPageByName(cls, name):
369 """retrieve page instance from its name 378 """retrieve page instance from its name