def update(self, id, format='html'): c.comment = get_entity_or_abort(model.Comment, id) require.comment.edit(c.comment) rev = c.comment.create_revision( self.form_result.get('text'), c.user, sentiment=self.form_result.get('sentiment')) model.meta.Session.commit() if can.user.vote(): decision = democracy.Decision(c.user, c.comment.poll) if not decision.result == model.Vote.YES: decision.make(model.Vote.YES) model.meta.Session.commit() # do not modify watch state as comments are always watched event.emit(event.T_COMMENT_EDIT, c.user, instance=c.instance, topics=[c.comment.topic], comment=c.comment, topic=c.comment.topic, rev=rev) if len(request.params.get('ret_url', '')): redirect(request.params.get('ret_url') + "#c" + str(c.comment.id)) if format != 'html': return ret_success(entity=c.comment, format=format) else: return ret_success(entity=c.comment, format='fwd')
def delegates_result(self, result): agents = [] for agent in self.delegates: decision = democracy.Decision(agent, self.poll) if decision.is_decided() and decision.result == result: agents.append(agent) return agents
def vote(self, id, format): c.poll = self._get_open_poll(id) if c.poll.action != model.Poll.ADOPT: abort(400, _("This is not an adoption poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) votes = decision.make(self.form_result.get("position")) model.meta.Session.commit() if not asbool(config.get('adhocracy.hide_individual_votes', 'false')): for vote in votes: event.emit(event.T_VOTE_CAST, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) if format == 'json': vdetail = votedetail.calc_votedetail_dict(c.instance, c.poll)\ if votedetail.is_enabled() else None return render_json( dict(decision=decision, score=c.poll.tally.score, votedetail=vdetail)) else: redirect(h.entity_url(c.poll.subject))
def rate(self, id, format): # rating is like polling but steps via abstention, i.e. if you have # first voted "for", rating will first go to "abstain" and only # then produce "against"- c.poll = self._get_open_poll(id) if c.poll.action not in [model.Poll.RATE, model.Poll.SELECT]: abort(400, _("This is not a rating poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) old = decision.result new = self.form_result.get("position") positions = { (model.Vote.YES, model.Vote.YES): model.Vote.YES, (model.Vote.ABSTAIN, model.Vote.YES): model.Vote.YES, (model.Vote.NO, model.Vote.YES): model.Vote.ABSTAIN, (model.Vote.YES, model.Vote.NO): model.Vote.ABSTAIN, (model.Vote.ABSTAIN, model.Vote.NO): model.Vote.NO, (model.Vote.NO, model.Vote.NO): model.Vote.NO } position = positions.get((old, new), new) votes = decision.make(position) tally = model.Tally.create_from_poll(c.poll) event_type = { model.Poll.RATE: event.T_RATING_CAST, model.Poll.SELECT: event.T_SELECT_VARIANT }.get(c.poll.action) model.meta.Session.commit() if not asbool(config.get('adhocracy.hide_individual_votes', 'false')): for vote in votes: event.emit(event_type, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) if format == 'json': vdetail = votedetail.calc_votedetail_dict(c.instance, c.poll)\ if votedetail.is_enabled() else None return render_json( dict(decision=decision, tally=tally.to_dict(), votedetail=vdetail)) elif format == 'ajax': return self.widget(id, format=self.form_result.get('cls')) elif c.poll.action == model.Poll.SELECT: redirect(h.entity_url(c.poll.selection)) else: redirect(h.entity_url(c.poll.subject))
def vote(self, id, format): c.poll = self._get_open_poll(id) if c.poll.action != model.Poll.ADOPT: abort(400, _("This is not an adoption poll.")) require.poll.vote(c.poll) decision = democracy.Decision(c.user, c.poll) votes = decision.make(self.form_result.get("position")) for vote in votes: event.emit(event.T_VOTE_CAST, vote.user, instance=c.instance, topics=[c.poll.scope], vote=vote, poll=c.poll) model.meta.Session.commit() if format == 'json': return render_json(dict(decision=decision, score=c.poll.tally.score)) redirect(h.entity_url(c.poll.subject))
def vote_source(event): """ Notify users about their voting behaviour, especially about delegated votes. """ if event.event in [T_VOTE_CAST, T_SELECT_VARIANT, T_RATING_CAST]: decision = democracy.Decision(event.user, event.poll) before = decision.without_vote(event.vote) if (map(lambda v: v.delegation, decision.relevant_votes) == map(lambda v: v.delegation, before.relevant_votes)) and \ (before.result == decision.result): return if not decision.is_decided(): yield Notification(event, event.user, type=N_DELEGATE_CONFLICT) elif decision.is_self_decided(): yield Notification(event, event.user, type=N_SELF_VOTED) else: yield Notification(event, event.user, type=N_DELEGATE_VOTED)
def _decision(self): if not self.__decision and c.user: self.__decision = democracy.Decision(c.user, self.poll) return self.__decision