diff 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
line wrap: on
line diff
--- a/src/server/pages.py	Wed Jan 24 09:57:38 2018 +0100
+++ b/src/server/pages.py	Wed Jan 24 09:57:57 2018 +0100
@@ -282,7 +282,7 @@
                                     name = cb_name, *uri_tuple))
                                 continue
                             else:
-                                cls.registerURI(uri_tuple, cb, new_path)
+                                resource.registerURI(uri_tuple, cb)
 
                 LiberviaPage.importPages(host, resource, new_path)
 
@@ -310,20 +310,19 @@
             main_menu.append((page_name, url))
         cls.main_menu = main_menu
 
-    @classmethod
-    def registerURI(cls, uri_tuple, get_uri_cb, pre_path):
+    def registerURI(self, uri_tuple, get_uri_cb):
         """register a URI handler
 
         @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
             type/subtype as returned by tools/common/parseXMPPUri
+            or type/None to handle all subtypes
         @param get_uri_cb(callable): method which take uri_data dict as only argument
-            and return path with correct arguments relative to page itself
-        @param pre_path(list[unicode]): prefix path to reference the handler page
+            and return absolute path with correct arguments or None if the page
+            can't handle this URL
         """
-        if uri_tuple in cls.uri_callbacks:
+        if uri_tuple in self.uri_callbacks:
             log.info(_(u"{}/{} URIs are already handled, replacing by the new handler").format(*uri_tuple))
-        cls.uri_callbacks[uri_tuple] = {u'callback': get_uri_cb,
-                                        u'pre_path': pre_path}
+        self.uri_callbacks[uri_tuple] = (self, get_uri_cb)
 
     def registerSignal(self, request, signal, check_profile=True):
         r"""register a signal handler
@@ -348,20 +347,30 @@
         LiberviaPage.signals_handlers.setdefault(signal, {})[id(request)] = (self, request, check_profile)
         request._signals_registered.append(signal)
 
-    def getPagePathFromURI(self, uri):
+    @classmethod
+    def getPagePathFromURI(cls, uri):
         """Retrieve page URL from xmpp: URI
 
         @param uri(unicode): URI with a xmpp: scheme
         @return (unicode,None): absolute path (starting from root "/") to page handling the URI
-            None is returned if not page has been registered for this URI
+            None is returned if no page has been registered for this URI
         """
         uri_data = common_uri.parseXMPPUri(uri)
         try:
-            callback_data = self.uri_callbacks[uri_data['type'], uri_data.get('sub_type')]
+            page, cb = cls.uri_callbacks[uri_data['type'], uri_data['sub_type']]
         except KeyError:
-            return
+            url = None
         else:
-            url = os.path.join(u'/', u'/'.join(callback_data['pre_path']), callback_data['callback'](self, uri_data))
+            url = cb(page, uri_data)
+        if url is None:
+            # no handler found
+            # we try to find a more generic one
+            try:
+                page, cb = cls.uri_callbacks[uri_data['type'], None]
+            except KeyError:
+                pass
+            else:
+                url = cb(page, uri_data)
         return url
 
     @classmethod