~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/tales.py

  • Committer: Francis J. Lacoste
  • Date: 2011-12-06 20:10:56 UTC
  • mfrom: (14448 devel)
  • mto: This revision was merged to the branch mainline in revision 14474.
  • Revision ID: francis.lacoste@canonical.com-20111206201056-jk8q5euywtd4vqk7
Merge devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
    IPrivacy
55
55
    )
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.
667
666
 
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
 
671
        to True.
673
672
        """
 
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'
705
 
                % template.filename)
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):
714
 
                return title_object
715
 
            else:
716
 
                title = title_object(view.context, view)
717
 
                if title is None:
718
 
                    return module.DEFAULT_LAUNCHPAD_TITLE
719
 
                else:
720
 
                    return title
 
693
                    return ROOT_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">&nbsp;</span>')
913
886
 
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">&nbsp;</a>')
916
889
 
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'
1699
1672
 
1700
1673
        url = self.url(view_name=view_name, rootsite=rootsite)
1701
1674
        title = cgi.escape(build.title)
2416
2389
    return clean_path_split
2417
2390
 
2418
2391
 
2419
 
class PageTemplateContextsAPI:
2420
 
    """Adapter from page tempate's CONTEXTS object to fmt:pagetitle.
2421
 
 
2422
 
    This is registered to be used for the dict type.
2423
 
    """
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)
2427
 
 
2428
 
    def __init__(self, contextdict):
2429
 
        self.contextdict = contextdict
2430
 
 
2431
 
    def traverse(self, name, furtherPath):
2432
 
        if name == 'pagetitle':
2433
 
            return self.pagetitle()
2434
 
        else:
2435
 
            raise TraversalError(name)
2436
 
 
2437
 
    def pagetitle(self):
2438
 
        """Return the string title for the page template CONTEXTS dict.
2439
 
 
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.
2444
 
 
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
2447
 
        title.
2448
 
        """
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 "
2457
 
                 "for %s" % name)
2458
 
        elif isinstance(titleobj, basestring):
2459
 
            return titleobj
2460
 
        else:
2461
 
            context = self.contextdict['context']
2462
 
            view = self.contextdict['view']
2463
 
            title = titleobj(context, view)
2464
 
            if title is None:
2465
 
                return canonical.launchpad.pagetitles.DEFAULT_LAUNCHPAD_TITLE
2466
 
            else:
2467
 
                return title
2468
 
 
2469
 
 
2470
2392
class PermissionRequiredQuery:
2471
2393
    """Check if the logged in user has a given permission on a given object.
2472
2394