Mercurial > libervia-web
comparison src/server/pages.py @ 1092:63ed5f6bd4eb
pages: new "getURLByPath" method in LiberviaPage, which is similar to getURLByNames, but which a more readable way to request a path with named pages.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 01 Jun 2018 12:58:20 +0200 |
parents | 8f77e36cd51d |
children | 3a7b2b239d3e |
comparison
equal
deleted
inserted
replaced
1091:092e910292c9 | 1092:63ed5f6bd4eb |
---|---|
29 from sat.tools import utils | 29 from sat.tools import utils |
30 from sat.core.log import getLogger | 30 from sat.core.log import getLogger |
31 log = getLogger(__name__) | 31 log = getLogger(__name__) |
32 from libervia.server.constants import Const as C | 32 from libervia.server.constants import Const as C |
33 from libervia.server import session_iface | 33 from libervia.server import session_iface |
34 from libervia.server.utils import quote | 34 from libervia.server.utils import quote, SubPage |
35 import libervia | 35 import libervia |
36 | 36 |
37 from collections import namedtuple | 37 from collections import namedtuple |
38 import uuid | 38 import uuid |
39 import os.path | 39 import os.path |
450 if kwargs: | 450 if kwargs: |
451 encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8') | 451 encoded = urllib.urlencode({k:v.encode('utf-8') for k,v in kwargs.iteritems()}).decode('utf-8') |
452 current_url = current_url + u'?' + encoded | 452 current_url = current_url + u'?' + encoded |
453 return current_url | 453 return current_url |
454 | 454 |
455 def getSubPageByName(self, page, subpage_name): | 455 def getSubPageByName(self, subpage_name, parent=None): |
456 """retrieve a subpage and its path using its name | 456 """retrieve a subpage and its path using its name |
457 | 457 |
458 @param request(server.Request): current HTTP request | 458 @param subpage_name(unicode): name of the sub page |
459 @param page_name(unicode): name of the page to retrieve | 459 it must be a direct children of parent page |
460 it must be a direct children of current page | 460 @param parent(LiberviaPage, None): parent page |
461 None to use current page | |
461 @return (tuple[str, LiberviaPage]): page subpath and instance | 462 @return (tuple[str, LiberviaPage]): page subpath and instance |
462 @raise exceptions.NotFound: no page has been found | 463 @raise exceptions.NotFound: no page has been found |
463 """ | 464 """ |
464 for path, child in page.children.iteritems(): | 465 if parent is None: |
466 parent = self | |
467 for path, child in parent.children.iteritems(): | |
465 try: | 468 try: |
466 child_name = child.name | 469 child_name = child.name |
467 except AttributeError: | 470 except AttributeError: |
468 # LiberviaPages have a name, but maybe this is an other Resource | 471 # LiberviaPages have a name, but maybe this is an other Resource |
469 continue | 472 continue |
486 and potential redirections. | 489 and potential redirections. |
487 @param request(server.Request): current HTTP request | 490 @param request(server.Request): current HTTP request |
488 @param page_name(unicode): name of the page to retrieve | 491 @param page_name(unicode): name of the page to retrieve |
489 it must be a direct children of current page | 492 it must be a direct children of current page |
490 @param *args(list[unicode]): arguments to add as path elements | 493 @param *args(list[unicode]): arguments to add as path elements |
491 @return unicode: absolute URL to the sub page | 494 @return (unicode): absolute URL to the sub page |
492 """ | 495 """ |
493 current_url = self.getCurrentURL(request) | 496 current_url = self.getCurrentURL(request) |
494 path, child = self.getSubPageByName(self, page_name) | 497 path, child = self.getSubPageByName(page_name) |
495 return os.path.join(u'/', current_url, path, *[quote(a) for a in args]) | 498 return os.path.join(u'/', current_url, path, *[quote(a) for a in args]) |
496 | 499 |
497 def getURLByNames(self, named_path): | 500 def getURLByNames(self, named_path): |
498 """retrieve URL from pages names and arguments | 501 """retrieve URL from pages names and arguments |
499 | 502 |
509 for page_name, page_args in named_path: | 512 for page_name, page_args in named_path: |
510 if current_page is None: | 513 if current_page is None: |
511 current_page = self.getPageByName(page_name) | 514 current_page = self.getPageByName(page_name) |
512 path.append(current_page.getURL(*page_args)) | 515 path.append(current_page.getURL(*page_args)) |
513 else: | 516 else: |
514 sub_path, current_page = self.getSubPageByName(current_page, page_name) | 517 sub_path, current_page = self.getSubPageByName(page_name, parent=current_page) |
515 path.append(sub_path) | 518 path.append(sub_path) |
516 if page_args: | 519 if page_args: |
517 path.extend([quote(a) for a in page_args]) | 520 path.extend([quote(a) for a in page_args]) |
518 return self.host.checkRedirection(u'/'.join(path)) | 521 return self.host.checkRedirection(u'/'.join(path)) |
522 | |
523 def getURLByPath(self, *args): | |
524 """generate URL by path | |
525 | |
526 this method as a similar effect as getURLByNames, but it is more readable | |
527 by using SubPage to get pages instead of using tuples | |
528 @param *args: path element: | |
529 - if unicode, will be used as argument | |
530 - if util.SubPage instance, must be the name of a subpage | |
531 @return (unicode): generated path | |
532 """ | |
533 args = list(args) | |
534 if not args: | |
535 raise ValueError('You must specify path elements') | |
536 # root page is the one needed to construct the base of the URL | |
537 # if first arg is not a SubPage instance, we use current page | |
538 if not isinstance(args[0], SubPage): | |
539 root = self | |
540 else: | |
541 root = self.getPageByName(args.pop(0)) | |
542 # we keep track of current page to check subpage | |
543 current_page = root | |
544 url_elts = [] | |
545 arguments = [] | |
546 while True: | |
547 while args and not isinstance(args[0], SubPage): | |
548 arguments.append(quote(args.pop(0))) | |
549 if not url_elts: | |
550 url_elts.append(root.getURL(*arguments)) | |
551 else: | |
552 url_elts.extend(arguments) | |
553 if not args: | |
554 break | |
555 else: | |
556 path, current_page = current_page.getSubPageByName(args.pop(0)) | |
557 arguments = [path] | |
558 return self.host.checkRedirection(u'/'.join(url_elts)) | |
519 | 559 |
520 def getChildWithDefault(self, path, request): | 560 def getChildWithDefault(self, path, request): |
521 # we handle children ourselves | 561 # we handle children ourselves |
522 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") | 562 raise exceptions.InternalError(u"this method should not be used with LiberviaPage") |
523 | 563 |