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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
= Poll Pages =
First import some stuff and setup some things we'll use in this test.
>>> from zope.component import getUtility, getMultiAdapter
>>> from zope.publisher.browser import TestRequest
>>> from lp.services.webapp.servers import LaunchpadTestRequest
>>> from lp.registry.interfaces.person import IPersonSet
>>> from lp.registry.interfaces.poll import IPollSet
>>> from datetime import datetime, timedelta
>>> login("test@canonical.com")
>>> ubuntu_team = getUtility(IPersonSet).getByName('ubuntu-team')
== Creating new polls ==
When creating a new poll, its start date must be at least 12h after it is
created.
First we attempt to create a poll which starts 11h from now. That will fail
with a proper explanation of why it failed.
>>> eleven_hours_from_now = datetime.now() + timedelta(hours=11)
>>> eleven_hours_from_now = eleven_hours_from_now.strftime(
... '%Y-%m-%d %H:%M:%S')
>>> form = {
... 'field.name': 'test-poll',
... 'field.title': 'test-poll',
... 'field.proposition': 'test-poll',
... 'field.allowspoilt': '1',
... 'field.secrecy': 'SECRET',
... 'field.dateopens': eleven_hours_from_now,
... 'field.datecloses': '2025-06-04',
... 'field.actions.continue': 'Continue'}
>>> request = LaunchpadTestRequest(method='POST', form=form)
>>> new_poll = getMultiAdapter((ubuntu_team, request), name="+newpoll")
>>> new_poll.initialize()
>>> print "\n".join(new_poll.errors)
A poll cannot open less than 12 hours after it's created.
Now we successfully create a poll which starts 12h from now.
>>> twelve_hours_from_now = datetime.now() + timedelta(hours=12)
>>> twelve_hours_from_now = twelve_hours_from_now.strftime(
... '%Y-%m-%d %H:%M:%S')
>>> form['field.dateopens'] = twelve_hours_from_now
>>> request = LaunchpadTestRequest(method='POST', form=form)
>>> new_poll = getMultiAdapter((ubuntu_team, request), name="+newpoll")
>>> new_poll.initialize()
>>> new_poll.errors
[]
== Displaying results of condorcet polls ==
>>> poll = getUtility(IPollSet).getByTeamAndName(ubuntu_team, 'director-2004')
>>> poll.type.title
'Condorcet Voting'
Although condorcet polls are disabled now, everything is implemented and we're
using a pairwise matrix to display the results. It's very trick to create this
matrix on page templates, so the view provides a method wich return this
matrix as a python list, with the necessary headers (the option's names).
>>> poll_results = getMultiAdapter((poll, TestRequest()), name="+index")
>>> for row in poll_results.getPairwiseMatrixWithHeaders():
... print row
[None, u'A', u'B', u'C', u'D']
[u'A', None, 2L, 2L, 2L]
[u'B', 2L, None, 2L, 2L]
[u'C', 1L, 1L, None, 1L]
[u'D', 2L, 1L, 2L, None]
== Voting on closed polls ==
This is not allowed, and apart from not linking to the +vote page and not
even displaying its content for a closed poll, we also have some lower
level checks.
>>> request = TestRequest(form={'changevote': 'Change Vote'})
>>> request.method = 'POST'
>>> voting_page = getMultiAdapter((poll, request), name="+vote")
>>> form_processed = False
>>> def form_processing():
... form_processed = True
>>> voting_page.processCondorcetVotingForm = form_processing
>>> voting_page.initialize()
>>> form_processed
False
>>> voting_page.feedback
'This poll is not open.'
|