Mercurial > libervia-web
comparison src/server/pages.py @ 1030:66a050b32df8
pages: moved code getting subpage from getSubPageURL to new getSubPageByName method.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 22 Jan 2018 22:16:07 +0100 |
parents | 34240d08f682 |
children | 4ba7df23b976 |
comparison
equal
deleted
inserted
replaced
1029:78b7b5ec7ca1 | 1030:66a050b32df8 |
---|---|
445 if kwargs: | 445 if kwargs: |
446 encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8') | 446 encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8') |
447 current_url = current_url + u'?' + encoded | 447 current_url = current_url + u'?' + encoded |
448 return current_url | 448 return current_url |
449 | 449 |
450 def getSubPageByName(self, page, subpage_name): | |
451 """retrieve a subpage and its path using its name | |
452 | |
453 @param request(server.Request): current HTTP request | |
454 @param page_name(unicode): name of the page to retrieve | |
455 it must be a direct children of current page | |
456 @return (tuple[str, LiberviaPage]): page subpath and instance | |
457 @raise exceptions.NotFound: no page has been found | |
458 """ | |
459 for path, child in page.children.iteritems(): | |
460 try: | |
461 child_name = child.name | |
462 except AttributeError: | |
463 # LiberviaPages have a name, but maybe this is an other Resource | |
464 continue | |
465 if child_name == subpage_name: | |
466 return path, child | |
467 raise exceptions.NotFound(_(u'requested sub page has not been found')) | |
468 | |
450 def getSubPageURL(self, request, page_name, *args): | 469 def getSubPageURL(self, request, page_name, *args): |
451 """retrieve a page in direct children and build its URL according to request | 470 """retrieve a page in direct children and build its URL according to request |
452 | 471 |
453 request's current path is used as base (at current parsing point, | 472 request's current path is used as base (at current parsing point, |
454 i.e. it's more prepath than path). | 473 i.e. it's more prepath than path). |
465 it must be a direct children of current page | 484 it must be a direct children of current page |
466 @param *args(list[unicode]): arguments to add as path elements | 485 @param *args(list[unicode]): arguments to add as path elements |
467 @return unicode: absolute URL to the sub page | 486 @return unicode: absolute URL to the sub page |
468 """ | 487 """ |
469 current_url = self.getCurrentURL(request) | 488 current_url = self.getCurrentURL(request) |
470 | 489 path, child = self.getSubPageByName(self, page_name) |
471 for path, child in self.children.iteritems(): | 490 return os.path.join(u'/', current_url, path, *[quote(a) for a in args]) |
472 try: | 491 |
473 child_name = child.name | |
474 except AttributeError: | |
475 # LiberviaPage have a name, but maybe this is an other Resource | |
476 continue | |
477 if child_name == page_name: | |
478 return os.path.join(u'/', current_url, path, *args) | |
479 raise exceptions.NotFound(_(u'requested sub page has not been found')) | |
480 | 492 |
481 def getChildWithDefault(self, path, request): | 493 def getChildWithDefault(self, path, request): |
482 # we handle children ourselves | 494 # we handle children ourselves |
483 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") | 495 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") |
484 | 496 |