~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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=E0611,W0212

__metaclass__ = type
__all__ = [
    'Poll',
    'PollOption',
    'PollOptionSet',
    'PollSet',
    'VoteCast',
    'Vote',
    'VoteSet',
    'VoteCastSet',
    ]

from datetime import datetime
import random

import pytz
from sqlobject import (
    AND,
    BoolCol,
    ForeignKey,
    IntCol,
    OR,
    SQLObjectNotFound,
    StringCol,
    )
from storm.store import Store
from zope.component import getUtility
from zope.interface import implements

from lp.registry.interfaces.person import validate_public_person
from lp.registry.interfaces.poll import (
    IPoll,
    IPollOption,
    IPollOptionSet,
    IPollSet,
    IVote,
    IVoteCast,
    IVoteCastSet,
    IVoteSet,
    OptionIsNotFromSimplePoll,
    PollAlgorithm,
    PollSecrecy,
    PollStatus,
    )
from lp.services.database.datetimecol import UtcDateTimeCol
from lp.services.database.enumcol import EnumCol
from lp.services.database.sqlbase import (
    SQLBase,
    sqlvalues,
    )


class Poll(SQLBase):
    """See IPoll."""

    implements(IPoll)
    _table = 'Poll'
    sortingColumns = ['title', 'id']
    _defaultOrder = sortingColumns

    team = ForeignKey(
        dbName='team', foreignKey='Person',
        storm_validator=validate_public_person, notNull=True)

    name = StringCol(dbName='name', notNull=True)

    title = StringCol(dbName='title', notNull=True, unique=True)

    dateopens = UtcDateTimeCol(dbName='dateopens', notNull=True)

    datecloses = UtcDateTimeCol(dbName='datecloses', notNull=True)

    proposition = StringCol(dbName='proposition',  notNull=True)

    type = EnumCol(dbName='type', enum=PollAlgorithm,
                   default=PollAlgorithm.SIMPLE)

    allowspoilt = BoolCol(dbName='allowspoilt', default=True, notNull=True)

    secrecy = EnumCol(dbName='secrecy', enum=PollSecrecy,
                      default=PollSecrecy.SECRET)

    def newOption(self, name, title, active=True):
        """See IPoll."""
        return getUtility(IPollOptionSet).new(self, name, title, active)

    def isOpen(self, when=None):
        """See IPoll."""
        if when is None:
            when = datetime.now(pytz.timezone('UTC'))
        return (self.datecloses >= when and self.dateopens <= when)

    @property
    def closesIn(self):
        """See IPoll."""
        return self.datecloses - datetime.now(pytz.timezone('UTC'))

    @property
    def opensIn(self):
        """See IPoll."""
        return self.dateopens - datetime.now(pytz.timezone('UTC'))

    def isClosed(self, when=None):
        """See IPoll."""
        if when is None:
            when = datetime.now(pytz.timezone('UTC'))
        return self.datecloses <= when

    def isNotYetOpened(self, when=None):
        """See IPoll."""
        if when is None:
            when = datetime.now(pytz.timezone('UTC'))
        return self.dateopens > when

    def getAllOptions(self):
        """See IPoll."""
        return getUtility(IPollOptionSet).selectByPoll(self)

    def getActiveOptions(self):
        """See IPoll."""
        return getUtility(IPollOptionSet).selectByPoll(self, only_active=True)

    def getVotesByPerson(self, person):
        """See IPoll."""
        return Vote.selectBy(person=person, poll=self)

    def personVoted(self, person):
        """See IPoll."""
        results = VoteCast.selectBy(person=person, poll=self)
        return bool(results.count())

    def removeOption(self, option, when=None):
        """See IPoll."""
        assert self.isNotYetOpened(when=when)
        if option.poll != self:
            raise ValueError(
                "Can't remove an option that doesn't belong to this poll")
        option.destroySelf()

    def getOptionByName(self, name):
        """See IPoll."""
        return PollOption.selectOneBy(poll=self, name=name)

    def _assertEverythingOkAndGetVoter(self, person, when=None):
        """Use assertions to Make sure all pre-conditions for a person to vote
        are met.

        Return the person if this is not a secret poll or None if it's a
        secret one.
        """
        assert self.isOpen(when=when), "This poll is not open"
        assert not self.personVoted(person), "Can't vote twice in the same poll"
        assert person.inTeam(self.team), (
            "Person %r is not a member of this poll's team." % person)

        # We only associate the option with the person if the poll is not a
        # SECRET one.
        if self.secrecy == PollSecrecy.SECRET:
            voter = None
        else:
            voter = person
        return voter

    def storeCondorcetVote(self, person, options, when=None):
        """See IPoll."""
        voter = self._assertEverythingOkAndGetVoter(person, when=when)
        assert self.type == PollAlgorithm.CONDORCET
        voteset = getUtility(IVoteSet)

        token = voteset.newToken()
        votes = []
        activeoptions = self.getActiveOptions()
        for option, preference in options.items():
            assert option.poll == self, (
                "The option %r doesn't belong to this poll" % option)
            assert option.active, "Option %r is not active" % option
            votes.append(voteset.new(self, option, preference, token, voter))

        # Store a vote with preference = None for each active option of this
        # poll that wasn't in the options argument.
        for option in activeoptions:
            if option not in options:
                votes.append(voteset.new(self, option, None, token, voter))

        getUtility(IVoteCastSet).new(self, person)
        return votes

    def storeSimpleVote(self, person, option, when=None):
        """See IPoll."""
        voter = self._assertEverythingOkAndGetVoter(person, when=when)
        assert self.type == PollAlgorithm.SIMPLE
        voteset = getUtility(IVoteSet)

        if option is None and not self.allowspoilt:
            raise ValueError("This poll doesn't allow spoilt votes.")
        elif option is not None:
            assert option.poll == self, (
                "The option %r doesn't belong to this poll" % option)
            assert option.active, "Option %r is not active" % option
        token = voteset.newToken()
        # This is a simple-style poll, so you can vote only on a single option
        # and this option's preference must be 1
        preference = 1
        vote = voteset.new(self, option, preference, token, voter)
        getUtility(IVoteCastSet).new(self, person)
        return vote

    def getTotalVotes(self):
        """See IPoll."""
        assert self.isClosed()
        return Vote.selectBy(poll=self).count()

    def getWinners(self):
        """See IPoll."""
        assert self.isClosed()
        # XXX: GuilhermeSalgado 2005-08-24:
        # For now, this method works only for SIMPLE-style polls. This is
        # not a problem as CONDORCET-style polls are disabled.
        assert self.type == PollAlgorithm.SIMPLE
        query = """
            SELECT option
            FROM Vote
            WHERE poll = %d AND option IS NOT NULL
            GROUP BY option
            HAVING COUNT(*) = (
                SELECT COUNT(*)
                FROM Vote
                WHERE poll = %d
                GROUP BY option
                ORDER BY COUNT(*) DESC LIMIT 1
                )
            """ % (self.id, self.id)
        results = Store.of(self).execute(query).get_all()
        if not results:
            return None
        return [PollOption.get(id) for (id,) in results]

    def getPairwiseMatrix(self):
        """See IPoll."""
        assert self.type == PollAlgorithm.CONDORCET
        options = list(self.getAllOptions())
        pairwise_matrix = []
        for option1 in options:
            pairwise_row = []
            for option2 in options:
                points_query = """
                    SELECT COUNT(*) FROM Vote as v1, Vote as v2 WHERE
                        v1.token = v2.token AND
                        v1.option = %s AND v2.option = %s AND
                        (
                         (
                          v1.preference IS NOT NULL AND
                          v2.preference IS NOT NULL AND
                          v1.preference < v2.preference
                         )
                          OR
                         (
                          v1.preference IS NOT NULL AND
                          v2.preference IS NULL
                         )
                        )
                    """ % sqlvalues(option1.id, option2.id)
                if option1 == option2:
                    pairwise_row.append(None)
                else:
                    points = Store.of(self).execute(points_query).get_one()[0]
                    pairwise_row.append(points)
            pairwise_matrix.append(pairwise_row)
        return pairwise_matrix


class PollSet:
    """See IPollSet."""

    implements(IPollSet)

    def new(self, team, name, title, proposition, dateopens, datecloses,
            secrecy, allowspoilt, poll_type=PollAlgorithm.SIMPLE):
        """See IPollSet."""
        return Poll(team=team, name=name, title=title,
                proposition=proposition, dateopens=dateopens,
                datecloses=datecloses, secrecy=secrecy,
                allowspoilt=allowspoilt, type=poll_type)

    def selectByTeam(self, team, status=PollStatus.ALL, orderBy=None, when=None):
        """See IPollSet."""
        if when is None:
            when = datetime.now(pytz.timezone('UTC'))

        if orderBy is None:
            orderBy = Poll.sortingColumns


        status = set(status)
        status_clauses = []
        if PollStatus.OPEN in status:
            status_clauses.append(AND(Poll.q.dateopens <= when,
                                    Poll.q.datecloses > when))
        if PollStatus.CLOSED in status:
            status_clauses.append(Poll.q.datecloses <= when)
        if PollStatus.NOT_YET_OPENED in status:
            status_clauses.append(Poll.q.dateopens > when)

        assert len(status_clauses) > 0, "No poll statuses were selected"

        results = Poll.select(AND(Poll.q.teamID == team.id,
                                  OR(*status_clauses)))

        return results.orderBy(orderBy)

    def getByTeamAndName(self, team, name, default=None):
        """See IPollSet."""
        query = AND(Poll.q.teamID == team.id, Poll.q.name == name)
        try:
            return Poll.selectOne(query)
        except SQLObjectNotFound:
            return default


class PollOption(SQLBase):
    """See IPollOption."""

    implements(IPollOption)
    _table = 'PollOption'
    _defaultOrder = ['title', 'id']

    poll = ForeignKey(dbName='poll', foreignKey='Poll', notNull=True)

    name = StringCol(notNull=True)

    title = StringCol(notNull=True)

    active = BoolCol(notNull=True, default=False)


class PollOptionSet:
    """See IPollOptionSet."""

    implements(IPollOptionSet)

    def new(self, poll, name, title, active=True):
        """See IPollOptionSet."""
        return PollOption(poll=poll, name=name, title=title, active=active)

    def selectByPoll(self, poll, only_active=False):
        """See IPollOptionSet."""
        query = PollOption.q.pollID == poll.id
        if only_active:
            query = AND(query, PollOption.q.active == True)
        return PollOption.select(query)

    def getByPollAndId(self, poll, option_id, default=None):
        """See IPollOptionSet."""
        query = AND(PollOption.q.pollID == poll.id,
                    PollOption.q.id == option_id)
        try:
            return PollOption.selectOne(query)
        except SQLObjectNotFound:
            return default


class VoteCast(SQLBase):
    """See IVoteCast."""

    implements(IVoteCast)
    _table = 'VoteCast'
    _defaultOrder = 'id'

    person = ForeignKey(
        dbName='person', foreignKey='Person',
        storm_validator=validate_public_person, notNull=True)

    poll = ForeignKey(dbName='poll', foreignKey='Poll', notNull=True)


class VoteCastSet:
    """See IVoteCastSet."""

    implements(IVoteCastSet)

    def new(self, poll, person):
        """See IVoteCastSet."""
        return VoteCast(poll=poll, person=person)


class Vote(SQLBase):
    """See IVote."""

    implements(IVote)
    _table = 'Vote'
    _defaultOrder = ['preference', 'id']

    person = ForeignKey(
        dbName='person', foreignKey='Person',
        storm_validator=validate_public_person)

    poll = ForeignKey(dbName='poll', foreignKey='Poll', notNull=True)

    option = ForeignKey(dbName='option', foreignKey='PollOption')

    preference = IntCol(dbName='preference')

    token = StringCol(dbName='token', notNull=True, unique=True)


class VoteSet:
    """See IVoteSet."""

    implements(IVoteSet)

    def newToken(self):
        """See IVoteSet."""
        chars = '23456789bcdfghjkmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ'
        length = 10
        token = ''.join([random.choice(chars) for c in range(length)])
        while self.getByToken(token):
            token = ''.join([random.choice(chars) for c in range(length)])
        return token

    def new(self, poll, option, preference, token, person):
        """See IVoteSet."""
        return Vote(poll=poll, option=option, preference=preference,
                    token=token, person=person)

    def getByToken(self, token):
        """See IVoteSet."""
        return Vote.selectBy(token=token)

    def getVotesByOption(self, option):
        """See IVoteSet."""
        if option.poll.type != PollAlgorithm.SIMPLE:
            raise OptionIsNotFromSimplePoll(
                '%r is not an option of a simple-style poll.' % option)
        return Vote.selectBy(option=option).count()