Merge lp:~james-w/python-timeline-django/timeline-factory into lp:python-timeline-django

Proposed by James Westby
Status: Approved
Approved by: Michael Nelson
Approved revision: 28
Proposed branch: lp:~james-w/python-timeline-django/timeline-factory
Merge into: lp:python-timeline-django
Diff against target: 84 lines (+32/-7)
3 files modified
timeline_django/setup.py (+1/-7)
timeline_django/tests/test_wsgi.py (+19/-0)
timeline_django/wsgi.py (+12/-0)
To merge this branch: bzr merge lp:~james-w/python-timeline-django/timeline-factory
Reviewer Review Type Date Requested Status
Michael Nelson (community) Approve
Review via email: mp+239254@code.launchpad.net

Commit message

Add a timeline_factory function.

To post a comment you must log in.
28. By James Westby

Fix typo, thanks Michael.

Revision history for this message
Michael Nelson (michael.nelson) :
review: Approve

Unmerged revisions

28. By James Westby

Fix typo, thanks Michael.

27. By James Westby

Add a timeline_factory function.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'timeline_django/setup.py'
2--- timeline_django/setup.py 2012-09-22 09:00:56 +0000
3+++ timeline_django/setup.py 2014-10-23 13:49:02 +0000
4@@ -13,8 +13,6 @@
5 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6 # GNU Lesser General Public License version 3 (see the file LICENSE).
7
8-from functools import partial
9-
10 from . import hooks, timeline_cursor, wsgi
11
12
13@@ -37,11 +35,7 @@
14 factory.
15 """
16 if timeline_factory is None:
17- def timeline_factory():
18- environ = wsgi.get_environ()
19- if not environ:
20- return None
21- return environ.get('timeline.timeline')
22+ timeline_factory = wsgi.timeline_factory
23 timeline_cursor.setup_timeline_cursor_hooks(timeline_factory, prefix=prefix)
24 receiver = hooks.TimelineReceiver(timeline_factory)
25 receiver.connect_to_signals()
26
27=== modified file 'timeline_django/tests/test_wsgi.py'
28--- timeline_django/tests/test_wsgi.py 2014-08-29 14:45:51 +0000
29+++ timeline_django/tests/test_wsgi.py 2014-10-23 13:49:02 +0000
30@@ -35,6 +35,9 @@
31 def test_get_environ_is_None_first_call(self):
32 self.assertEqual(None, wsgi.get_environ())
33
34+ def test_timeline_factory_is_None_first_call(self):
35+ self.assertEqual(None, wsgi.timeline_factory())
36+
37 def test_wsgi_sets_timeline(self):
38 environ = {}
39 start_response = lambda status, headers: None
40@@ -45,6 +48,22 @@
41 out = app(environ, start_response)
42 self.assertEqual(["foo"], list(out))
43
44+ def test_timeline_factory_with_no_timeline(self):
45+ environ = {}
46+ start_response = lambda status, headers: None
47+ def do_check(environ, start_response):
48+ self.assertEqual(None, wsgi.timeline_factory())
49+ app = wsgi.make_app(do_check)
50+ out = app(environ, start_response)
51+
52+ def test_timeline_factory_with_timeline(self):
53+ environ = {'timeline.timeline': 'a'}
54+ start_response = lambda status, headers: None
55+ def do_check(environ, start_response):
56+ self.assertEqual('a', wsgi.timeline_factory())
57+ app = wsgi.make_app(do_check)
58+ out = app(environ, start_response)
59+
60 def test_wsgi_unsets_timeline(self):
61 environ = {}
62 start_response = lambda status, headers: None
63
64=== modified file 'timeline_django/wsgi.py'
65--- timeline_django/wsgi.py 2014-08-29 13:02:39 +0000
66+++ timeline_django/wsgi.py 2014-10-23 13:49:02 +0000
67@@ -33,6 +33,18 @@
68 return getattr(_thread_local, 'environ', None)
69
70
71+def timeline_factory():
72+ """Get a timeline for the currently active request.
73+
74+ Calling this factory will get you a timeline for the currently
75+ active request, if there is one. If there is no active request,
76+ or if the request environ has no timeline then it returns None.
77+ """
78+ environ = get_environ()
79+ if environ:
80+ return environ.get('timeline.timeline')
81+
82+
83 def make_app(application):
84 """Create a WSGI application that stores the environ in a thread-local.
85

Subscribers

People subscribed via source and target branches