~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/views.py

Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
from canonical.config import config
25
25
from canonical.launchpad.layers import setFirstLayer
26
 
from canonical.launchpad.webapp.interfaces import IPlacelessAuthUtility
 
26
from canonical.launchpad.webapp.interfaces import (
 
27
    ICanonicalUrlData,
 
28
    IPlacelessAuthUtility,
 
29
    )
 
30
from canonical.launchpad.webapp.publisher import layer_for_rootsite
27
31
from canonical.launchpad.webapp.servers import LaunchpadTestRequest
28
32
from canonical.lazr import ExportedFolder
29
33
 
30
34
 
31
35
def create_view(context, name, form=None, layer=None, server_url=None,
32
36
                method='GET', principal=None, query_string='', cookie='',
33
 
                request=None, path_info='/', current_request=False, **kwargs):
 
37
                request=None, path_info='/', current_request=False,
 
38
                rootsite=None, **kwargs):
34
39
    """Return a view based on the given arguments.
35
40
 
36
41
    :param context: The context for the view.
59
64
    else:
60
65
        request.setPrincipal(
61
66
            getUtility(IPlacelessAuthUtility).unauthenticatedPrincipal())
 
67
    if layer is None:
 
68
        # If a layer hasn't been specified, try to get the layer for the
 
69
        # rootsite.
 
70
        if rootsite is None:
 
71
            # If we haven't been told a site, try to get it from the canonical
 
72
            # url data of the object.
 
73
            obj_urldata = ICanonicalUrlData(context, None)
 
74
            if obj_urldata is not None:
 
75
                rootsite = obj_urldata.rootsite
 
76
        layer = layer_for_rootsite(rootsite)
62
77
    if layer is not None:
63
78
        setFirstLayer(request, layer)
64
79
    if current_request:
70
85
def create_initialized_view(context, name, form=None, layer=None,
71
86
                            server_url=None, method=None, principal=None,
72
87
                            query_string=None, cookie=None, request=None,
73
 
                            path_info='/'):
 
88
                            path_info='/', rootsite=None):
74
89
    """Return a view that has already been initialized."""
75
90
    if method is None:
76
91
        if form is None:
79
94
            method = 'POST'
80
95
    view = create_view(
81
96
        context, name, form, layer, server_url, method, principal,
82
 
        query_string, cookie, request, path_info)
 
97
        query_string, cookie, request, path_info, rootsite=rootsite)
83
98
    view.initialize()
84
99
    return view
85
100