def removeUserVote(self, poll_id, unicity_factor): """ remove the previous vote """ self._results[poll_id][unicity_factor] = [] logger.debug("removed user vote %s", self._results) logger.debug("User vote: %s", self._results[poll_id][unicity_factor]) return
def getVoteUnicity(self, poll_id, create=1): """ getVoteUnicity(self, poll_id, create = 1) => string or int or None return a tag that allows PlonePopollTool to think that this vote isn't made twice for the same user. We first try to use the username as a tag. If it's not available or if the user is anonymous, we use a counter. - poll_id is the poll id (!), ie. a sitewide unique id identifying the poll. IT MUST NOT CHANGE DURING THE POLL'S LIFE. - create is an additional parameter set to 1 by default. If it is true, a new unicity token will be automatically generated. If it is false, this method returns the FORMER one if it exists, OR None if no token exists. This is convenient to check if a user has voted without consuming a ZODB transaction. => NOT USED """ # Try to fetch the user name unicity = None mtool = getToolByName(self, 'portal_membership') try: if not mtool.isAnonymousUser(): unicity = mtool.getAuthenticatedMember().getUserName() except: raise NameError, 'unicity' + unicity logger.error("Unable to find username %s", exc_info=True) pass # If we didn't find a valid user name, then we use a counter and store it # in a cookie logger.debug("Computing unicity") if not unicity: if self.REQUEST.has_key('AnonymousVoted%s' % (poll_id)): unicity = self.REQUEST['AnonymousVoted%s' % (poll_id)] elif create: unicity = int(time.time() * 10000000) + random.randint(0, 99) expires = 'Wed, 19 Feb 2020 14:28:00 GMT' self.REQUEST.RESPONSE.setCookie( 'AnonymousVoted%s' % (poll_id, ), str(unicity), path='/', expires=expires, ) # Add the poll id to the unicity factor logger.debug("bloub %s", unicity) if unicity: unicity = "%s%s" % ( unicity, poll_id, ) # Return unicity factor return unicity
def getVoteUnicity(self, poll_id, create = 1): """ getVoteUnicity(self, poll_id, create = 1) => string or int or None return a tag that allows PlonePopollTool to think that this vote isn't made twice for the same user. We first try to use the username as a tag. If it's not available or if the user is anonymous, we use a counter. - poll_id is the poll id (!), ie. a sitewide unique id identifying the poll. IT MUST NOT CHANGE DURING THE POLL'S LIFE. - create is an additional parameter set to 1 by default. If it is true, a new unicity token will be automatically generated. If it is false, this method returns the FORMER one if it exists, OR None if no token exists. This is convenient to check if a user has voted without consuming a ZODB transaction. => NOT USED """ # Try to fetch the user name unicity = None mtool = getToolByName(self, 'portal_membership') try: if not mtool.isAnonymousUser(): unicity = mtool.getAuthenticatedMember().getUserName() except: raise NameError, 'unicity' + unicity logger.error("Unable to find username %s", exc_info=True) pass # If we didn't find a valid user name, then we use a counter and store it # in a cookie logger.debug("Computing unicity") if not unicity: if self.REQUEST.has_key('AnonymousVoted%s' % (poll_id)): unicity = self.REQUEST['AnonymousVoted%s' % (poll_id)] elif create: unicity = int(time.time() * 10000000) + random.randint(0,99) expires = 'Wed, 19 Feb 2020 14:28:00 GMT' self.REQUEST.RESPONSE.setCookie( 'AnonymousVoted%s' % (poll_id,), str(unicity), path='/', expires=expires, ) # Add the poll id to the unicity factor logger.debug("bloub %s", unicity) if unicity: unicity = "%s%s" % (unicity, poll_id, ) # Return unicity factor return unicity
def getResults(self, sort=0): """ getResults(self) -> return results as a TUPLE of tuples (id, count, percentage). percentage is 0 <= percentage <= 100 The order is the same as for listChoiceIds if sort if false. If sort is true, results are sorted by score (best first) """ res = getToolByName(self, 'portal_popoll').getBackend().getResults( self.getVoteId()) ret = [] votes_count = 0 idx = 0 for id in self.choices[:]: # If a choice has not been selected, it is not in res! if not res.has_key(idx): ret.append((id, 0)) else: votes_count += res[idx]['count'] ret.append((id, res[idx]['count'])) idx += 1 # Create a new tuple that includes the percentage as well result = [] for r in ret: try: percentage = float(r[1]) / float(votes_count) * 100.0 except: percentage = 0.0 #result.append((r[0],r[1],int(percentage))) ## We should not do the int(). Fixed (KA & MR) ! result.append((r[0], r[1], percentage)) if sort: result.sort(lambda x, y: cmp(y[1], x[1])) logger.debug("getResults for the popoll :%s", result) return tuple(result)
def getResults(self, sort = 0): """ getResults(self) -> return results as a TUPLE of tuples (id, count, percentage). percentage is 0 <= percentage <= 100 The order is the same as for listChoiceIds if sort if false. If sort is true, results are sorted by score (best first) """ res = getToolByName(self, 'portal_popoll').getBackend().getResults(self.getVoteId()) ret = [] votes_count = 0 idx = 0 for id in self.choices[:]: # If a choice has not been selected, it is not in res! if not res.has_key(idx): ret.append((id, 0)) else: votes_count += res[idx]['count'] ret.append((id, res[idx]['count'])) idx += 1 # Create a new tuple that includes the percentage as well result=[] for r in ret: try: percentage=float(r[1])/float(votes_count)*100.0 except: percentage=0.0 #result.append((r[0],r[1],int(percentage))) ## We should not do the int(). Fixed (KA & MR) ! result.append((r[0], r[1], percentage)) if sort: result.sort(lambda x,y: cmp(y[1], x[1])) logger.debug("getResults for the popoll :%s", result) return tuple(result)
def vote(self, poll_id, choice, unicity_factor, REQUEST = {}, **kw): """ vote """ # Record the vote in the _results dictionary if not self._results.has_key(poll_id): self._results[poll_id] = {} if not self._results.has_key(poll_id) or not self._results[poll_id].has_key(unicity_factor) : self._results[poll_id][unicity_factor] = [] self._results[poll_id][unicity_factor].append(choice) self._results = self._results logger.debug("vote %s", self._results[poll_id]) logger.debug("vote %s", self._results[poll_id][unicity_factor]) logger.debug("vote %s", self._results) return
def vote(self, poll_id, choice, unicity_factor, REQUEST={}, **kw): """ vote """ # Record the vote in the _results dictionary if poll_id not in self._results: self._results[poll_id] = {} if poll_id not in self._results or unicity_factor not in self._results[ poll_id]: self._results[poll_id][unicity_factor] = [] self._results[poll_id][unicity_factor].append(choice) self._results = self._results logger.debug("vote %s", self._results[poll_id]) logger.debug("vote %s", self._results[poll_id][unicity_factor]) logger.debug("vote %s", self._results) return