56
56
from canonical.launchpad.layers import LaunchpadLayer
57
import canonical.launchpad.pagetitles
58
57
from canonical.launchpad.webapp import canonical_url, urlappend
59
58
from canonical.launchpad.webapp.authorization import check_permission
60
59
from canonical.launchpad.webapp.badge import IHasBadges
666
665
"""The page title to be used.
668
667
By default, reverse breadcrumbs are always used if they are available.
669
If not available, then the view's .page_title attribute or entry in
670
pagetitles.py (deprecated) is used. If breadcrumbs are available,
671
then a view can still choose to override them by setting the attribute
672
.override_title_breadcrumbs to True.
668
If not available, then the view's .page_title attribut is used.
669
If breadcrumbs are available, then a view can still choose to
670
override them by setting the attribute .override_title_breadcrumbs
673
ROOT_TITLE = 'Launchpad'
674
674
view = self._context
675
675
request = get_current_browser_request()
676
module = canonical.launchpad.pagetitles
677
676
hierarchy_view = getMultiAdapter(
678
677
(view.context, request), name='+hierarchy')
679
678
override = getattr(view, 'override_title_breadcrumbs', False)
691
690
if template is None:
692
691
template = getattr(view, 'index', None)
693
692
if template is None:
694
return module.DEFAULT_LAUNCHPAD_TITLE
695
# There is no .page_title attribute on the view, so fallback to
696
# looking for an an entry in pagetitles.py. This is deprecated
697
# though, so issue a warning.
698
filename = os.path.basename(template.filename)
699
name, ext = os.path.splitext(filename)
700
title_name = name.replace('-', '_')
701
title_object = getattr(module, title_name, None)
702
# Page titles are mandatory.
703
assert title_object is not None, (
704
'No .page_title or pagetitles.py found for %s'
706
## 2009-09-08 BarryWarsaw bug 426527: Enable this when we want to
707
## force conversions from pagetitles.py; however tests will fail
708
## because of this output.
709
## warnings.warn('Old style pagetitles.py entry found for %s. '
710
## 'Switch to using a .page_title attribute on the '
711
## 'view instead.' % template.filename,
712
## DeprecationWarning)
713
if isinstance(title_object, basestring):
716
title = title_object(view.context, view)
718
return module.DEFAULT_LAUNCHPAD_TITLE
721
694
# Use the reverse breadcrumbs.
722
695
return SEPARATOR.join(
723
696
breadcrumb.text for breadcrumb
912
885
'<span alt="%s" title="%s" class="%s"> </span>')
914
887
linked_icon_template = (
915
'<a href="%s" alt="%s" title="%s" class="%s"></a>')
888
'<a href="%s" alt="%s" title="%s" class="%s"> </a>')
917
890
def traverse(self, name, furtherPath):
918
891
"""Special-case traversal for icons with an optional rootsite."""
1695
1668
def link(self, view_name, rootsite=None):
1696
1669
build = self._context
1697
1670
if not check_permission('launchpad.View', build):
1698
return 'private source'
1671
return 'private job'
1700
1673
url = self.url(view_name=view_name, rootsite=rootsite)
1701
1674
title = cgi.escape(build.title)
2416
2389
return clean_path_split
2419
class PageTemplateContextsAPI:
2420
"""Adapter from page tempate's CONTEXTS object to fmt:pagetitle.
2422
This is registered to be used for the dict type.
2424
# 2009-09-08 BarryWarsaw bug 426532. Remove this class, all references
2425
# to it, and all instances of CONTEXTS/fmt:pagetitle
2426
implements(ITraversable)
2428
def __init__(self, contextdict):
2429
self.contextdict = contextdict
2431
def traverse(self, name, furtherPath):
2432
if name == 'pagetitle':
2433
return self.pagetitle()
2435
raise TraversalError(name)
2437
def pagetitle(self):
2438
"""Return the string title for the page template CONTEXTS dict.
2440
Take the simple filename without extension from
2441
self.contextdict['template'].filename, replace any hyphens with
2442
underscores, and use this to look up a string, unicode or
2443
function in the module canonical.launchpad.pagetitles.
2445
If no suitable object is found in canonical.launchpad.pagetitles, emit
2446
a warning that this page has no title, and return the default page
2449
template = self.contextdict['template']
2450
filename = os.path.basename(template.filename)
2451
name, ext = os.path.splitext(filename)
2452
name = name.replace('-', '_')
2453
titleobj = getattr(canonical.launchpad.pagetitles, name, None)
2454
if titleobj is None:
2455
raise AssertionError(
2456
"No page title in canonical.launchpad.pagetitles "
2458
elif isinstance(titleobj, basestring):
2461
context = self.contextdict['context']
2462
view = self.contextdict['view']
2463
title = titleobj(context, view)
2465
return canonical.launchpad.pagetitles.DEFAULT_LAUNCHPAD_TITLE
2470
2392
class PermissionRequiredQuery:
2471
2393
"""Check if the logged in user has a given permission on a given object.