~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
= The macro: TALES namespace =

Launchpad has a 'macro:' TALES namespace that offers controls over the
layout of the page.

    >>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
    >>> class FakeView(object):
    ...     request = LaunchpadTestRequest()

Templates should start by specifying the kind of pagetype they use.
That's done by using the 'macro:page' traversal. That expression returns
the master macro from the main_template.pt and sets on the view the
layout it's using. The following METAL fragment illustrates the way it's
usually done:

    <html metal:use-macro="view/macro:page/main_side" />


    >>> from lp.testing import test_tales
    >>> view = FakeView()

    # Return value is the compiled macro expression.
    >>> test_tales('view/macro:page/main_side', view=view)
    [('version', ...]

The pagetype is registered in the __pagetype__ attribute.

    >>> view.__pagetype__
    'main_side'

If the pagetype isn't defined, a LocationError is raised.

    >>> test_tales('view/macro:page/not-defined', view=FakeView())
    Traceback (most recent call last):
      ...
    LocationError: 'unknown pagetype: not-defined'

The 'macro:pagehas' can then be used to test for features that should be
rendered in the layout.

    >>> test_tales('view/macro:pagehas/applicationtabs', view=view)
    True

    >>> test_tales('view/macro:pagehas/portlets', view=view)
    True


The 'macro:isbetauser' can be used to safely try to determine if the
current user is a beta user. It works for views that provide
isBetaUser() (ie. all views inheriting LaunchpadView), and simply
returns False for older views that do not.

    >>> class LPView(object):
    ...     request = LaunchpadTestRequest()
    ...     def isBetaUser(self):
    ...         return True

    >>> test_tales('view/macro:isbetauser', view=LPView())
    True

    >>> test_tales('view/macro:isbetauser', view=view)
    False