Ejemplo n.º 1
0
    def render_result(self, view):
        votes = [x['uid'] for x in self.context.poll_result]
        novotes = set(self.context.proposal_uids) - set(votes)
        translate = view.request.localizer.translate
        total_votes = self._total_votes()
        results = []
        #Adjust result layout
        def _get_percentage(num):
            val = Decimal(num) / total_votes
            return round(val*100, 0)

        for res in tuple(self.context.poll_result):
            results.append({'uid': res['uid'],
                            'num': res['num'],
                            'perc': res['percent'],
                            'perc_int': _get_percentage(res['num'])})
        for uid in novotes:
            results.append({'uid': uid, 'num': 0, 'perc': '0%', 'perc_int': 0})
        
        vote_singular = translate(_("vote_singular", default = "Vote"))
        vote_plural = translate(_("vote_plural", default = "Votes"))
        def _vote_text(count):
            return view.request.localizer.pluralize(vote_singular, vote_plural, count)

        response = {}
        response['view'] = view
        response['context'] = self.context
        response['total_votes'] = total_votes
        response['results'] = results
        response['vote_text'] = _vote_text
        proposals = {}
        for prop in self.context.get_proposal_objects():
            proposals[prop.uid] = prop
        response['proposals'] = proposals
        return render('voteit.dutt:templates/results.pt', response, request = view.request)
Ejemplo n.º 2
0
class DuttSettingsSchema(colander.Schema):
    max = colander.SchemaNode(colander.Int(),
                              title=_(u"Maximum dutts"),
                              description=_(u"A '0' disables this setting."),
                              default=0,
                              missing=0)
    min = colander.SchemaNode(colander.Int(),
                              title=_(u"Minimum dutts"),
                              description=_(u"A '0' disables this setting."),
                              default=0,
                              missing=0)
Ejemplo n.º 3
0
 def render_result(self, request, api, complete=True):
     votes = [x['uid'] for x in self.context.poll_result]
     novotes = set(self.context.proposal_uids) - set(votes)
     response = {}
     response['api'] = api
     response['total_votes'] = self._total_votes()
     response['result'] = self.context.poll_result
     response['novotes'] = novotes
     response['get_proposal_by_uid'] = self.context.get_proposal_by_uid
     response['complete'] = complete
     response['vote_singular'] = api.translate(_(u"vote_singular", default = u"Vote"))
     response['vote_plural'] = api.translate(_(u"vote_plural", default = u"Votes"))
     return render('templates/results.pt', response, request = request)
Ejemplo n.º 4
0
def deferred_proposal_description(node, kw):
    context = kw['context']
    max_choices = context.poll_settings.get('max', len(context.proposals))
    min_choices = context.poll_settings.get('min', 0)
    if min_choices:
        return _(
            "proposal_description_min",
            default="Check at least ${min} and at most ${max} proposal(s).",
            mapping={
                'min': min_choices,
                'max': max_choices
            })
    return _("proposal_description_without_min",
             default="Check at most ${max} proposal(s).",
             mapping={'max': max_choices})
Ejemplo n.º 5
0
def deferred_proposal_description(node, kw):
    context = kw['context']
    max_choices = context.poll_settings.get('max') or len(context.proposals)
    min_choices = context.poll_settings.get('min', 0)
    if max_choices == min_choices:
        return _('proposal_description_exactly',
                 default='Check exactly ${amount} proposal(s).',
                 mapping={'amount': min_choices})
    if min_choices:
        return _("proposal_description_min",
                 default="Check at least ${min} and at most ${max} proposal(s).",
                 mapping={'min': min_choices, 'max': max_choices})
    return _("proposal_description_without_min",
             default="Check at most ${max} proposal(s).",
             mapping={'max': max_choices})
Ejemplo n.º 6
0
 def __call__(self, node, value):
     max_choices = self.context.poll_settings.get('max', 0)
     min_choices = self.context.poll_settings.get('min', 0)
     assert isinstance(max_choices, int)
     assert isinstance(min_choices, int)
     if max_choices:
         if len(value) > max_choices:
             raise colander.Invalid(
                 node,
                 _(u"too_many_selected_error",
                   default=u"You can only select a maximum of ${max}.",
                   mapping={'max': max_choices}))
     if min_choices:
         if len(value) < min_choices:
             raise colander.Invalid(
                 node,
                 _(u"too_few_selected_error",
                   default=u"You must select at least ${min}.",
                   mapping={'min': min_choices}))
Ejemplo n.º 7
0
 def __call__(self, node, value):
     max_choices = self.context.poll_settings.get('max', 0)
     min_choices = self.context.poll_settings.get('min', 0)
     assert isinstance(max_choices, int)
     assert isinstance(min_choices, int)
     if max_choices:
         if max_choices == min_choices and len(value) != max_choices:
             raise colander.Invalid(node, _("not_exactly_selected_error",
                                            default="You have checked ${amount} proposal(s).",
                                            mapping={'amount': len(value)}))
         if len(value) > max_choices:
             raise colander.Invalid(node, _("too_many_selected_error",
                                            default="You can only select a maximum of ${max}.",
                                            mapping={'max': max_choices}))
     if min_choices:
         if len(value) < min_choices:
             raise colander.Invalid(node, _("too_few_selected_error",
                                    default="You must select at least ${min}.",
                                    mapping={'min': min_choices}))
Ejemplo n.º 8
0
def deferred_proposal_title(node, kw):
    context = kw['context']
    if context.get_workflow_state() == 'ongoing':
        return _(u"Mark the proposals you wish to vote for")
    return _(u"You can't change your vote now.")
Ejemplo n.º 9
0
class DuttPoll(PollPlugin):
    name = 'dutt_poll'
    title = _(u"Dutt poll")
    description = _(u"dutt_poll_description",
                    default = u"Tick proposals you like. There's a max amount, but you can add less if you want.")

    def get_settings_schema(self):
        return DuttSettingsSchema()
    
    def get_vote_schema(self):
        """ Get an instance of the schema that this poll uses.
        """
        return DuttSchema()

    def handle_close(self):
        """ Get the calculated result of this ballot.
        """
        total_votes = self._total_votes()

        def _get_percentage(num):
            val = Decimal(num) / total_votes
            return u"%s%%" % (round(val*100, 1))

        counter = Counter()
        for (ballot, factor) in self.context.ballots:
            for uid in ballot['proposals']:
                counter.add(uid, factor)
        result = counter.sorted_results()
        #Add percentage
        for item in result:
            item['percent'] = _get_percentage(item['num'])
        self.context.poll_result = result

    def render_result(self, view):
        votes = [x['uid'] for x in self.context.poll_result]
        novotes = set(self.context.proposal_uids) - set(votes)
        translate = view.request.localizer.translate
        total_votes = self._total_votes()
        results = []
        #Adjust result layout
        def _get_percentage(num):
            val = Decimal(num) / total_votes
            return round(val*100, 0)

        for res in tuple(self.context.poll_result):
            results.append({'uid': res['uid'],
                            'num': res['num'],
                            'perc': res['percent'],
                            'perc_int': _get_percentage(res['num'])})
        for uid in novotes:
            results.append({'uid': uid, 'num': 0, 'perc': '0%', 'perc_int': 0})
        
        vote_singular = translate(_("vote_singular", default = "Vote"))
        vote_plural = translate(_("vote_plural", default = "Votes"))
        def _vote_text(count):
            return view.request.localizer.pluralize(vote_singular, vote_plural, count)

        response = {}
        response['view'] = view
        response['context'] = self.context
        response['total_votes'] = total_votes
        response['results'] = results
        response['vote_text'] = _vote_text
        proposals = {}
        for prop in self.context.get_proposal_objects():
            proposals[prop.uid] = prop
        response['proposals'] = proposals
        return render('voteit.dutt:templates/results.pt', response, request = view.request)

    def _total_votes(self):
        return sum([x[1] for x in self.context.ballots])
Ejemplo n.º 10
0
def deferred_proposal_title(node, kw):
    context = kw['context']
    if context.get_workflow_state() == 'ongoing':
        return _(u"Mark the proposals you wish to vote for")
    return _(u"You can't change your vote now.")