~launchpad-pqm/launchpad/devel

11416.3.3 by Robert Collins
Create lp.services.timeline.requesttimeline for getting Timelines for requests.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Manage a Timeline for a request."""
5
11416.3.11 by Robert Collins
requesttimeline improvements.
6
__all__ = [
7
    'get_request_timeline',
8
    'set_request_timeline',
9
    ]
10
11416.3.17 by Robert Collins
Typo. Arg.
11
__metaclass__ = type
11416.3.11 by Robert Collins
requesttimeline improvements.
12
13
# XXX RobertCollins 2010-09-01 bug=623199: Undesirable but pragmatic.
14
# Because of this bug, rather than using request.annotations we have
15
# to work in with the webapp.adapter request model, which is 
16
# different to that used by get_current_browser_request.
11416.3.5 by Robert Collins
Adjust the requesttimeline API to be a shallow proxy for lib/canonical/launchpad/webapp/adapter.py rather than using request annoations.
17
from canonical.launchpad import webapp
11416.3.3 by Robert Collins
Create lp.services.timeline.requesttimeline for getting Timelines for requests.
18
from timeline import Timeline
19
20
21
def get_request_timeline(request):
11416.3.11 by Robert Collins
requesttimeline improvements.
22
    """Get a `Timeline` for request.
11416.3.3 by Robert Collins
Create lp.services.timeline.requesttimeline for getting Timelines for requests.
23
11416.3.11 by Robert Collins
requesttimeline improvements.
24
    This should ideally return the request.annotations['timeline'], creating it
25
    if necessary. However due to bug 623199 it instead using the adapter
26
    context for 'requests'. Once bug 623199 is fixed it will instead use the
27
    request annotations.
11416.3.3 by Robert Collins
Create lp.services.timeline.requesttimeline for getting Timelines for requests.
28
29
    :param request: A zope/launchpad request object.
30
    :return: A lp.services.timeline.timeline.Timeline object for the request.
31
    """
11416.3.5 by Robert Collins
Adjust the requesttimeline API to be a shallow proxy for lib/canonical/launchpad/webapp/adapter.py rather than using request annoations.
32
    try:
33
        return webapp.adapter._local.request_timeline
34
    except AttributeError:
35
        return set_request_timeline(request, Timeline())
11416.3.11 by Robert Collins
requesttimeline improvements.
36
    # Disabled code path: bug 623199, ideally we would use this code path.
11416.3.3 by Robert Collins
Create lp.services.timeline.requesttimeline for getting Timelines for requests.
37
    return request.annotations.setdefault('timeline', Timeline())
11416.3.5 by Robert Collins
Adjust the requesttimeline API to be a shallow proxy for lib/canonical/launchpad/webapp/adapter.py rather than using request annoations.
38
39
40
def set_request_timeline(request, timeline):
11416.3.11 by Robert Collins
requesttimeline improvements.
41
    """Explicitly set a timeline for request.
11416.3.5 by Robert Collins
Adjust the requesttimeline API to be a shallow proxy for lib/canonical/launchpad/webapp/adapter.py rather than using request annoations.
42
43
    This is used by code which wants to manually assemble a timeline.
44
45
    :param request: A zope/launchpad request object.
46
    :param timeline: A Timeline.
47
    """
48
    webapp.adapter._local.request_timeline = timeline
49
    return timeline
11416.3.11 by Robert Collins
requesttimeline improvements.
50
    # Disabled code path: bug 623199, ideally we would use this code path.
11416.3.5 by Robert Collins
Adjust the requesttimeline API to be a shallow proxy for lib/canonical/launchpad/webapp/adapter.py rather than using request annoations.
51
    request.annotations['timeline'] = timeline