示例#1
0
 def vote_success(self, appstruct):
     #Just in case the form rendered before the poll closed
     if not self.can_vote:
         raise HTTPForbidden(_("You're not allowed to vote"))
     Vote = self.poll_plugin.get_vote_class()
     if not IVote.implementedBy(Vote):
         raise TypeError("Poll plugins method get_vote_class returned something that didn't implement IVote.")
     appstruct.pop('csrf_token', None)
     userid = self.api.userid
     if userid in self.context:
         vote = self.context[userid]
         assert IVote.providedBy(vote)
         vote.set_vote_data(appstruct)
     else:
         vote = Vote(creators = [userid])
         #We don't need to send events here, since object added will take care of that
         vote.set_vote_data(appstruct, notify = False)
         #To fire events after set_vote_data is done
         self.context[userid] = vote
     success_msg = _(u"Your vote has been registered!")
     if self.request.is_xhr:
         self.response['success_msg'] = success_msg
         return Response(render("templates/snippets/vote_success.pt", self.response, request = self.request))
     self.api.flash_messages.add(success_msg)
     url = self.request.resource_url(self.context.__parent__, anchor = self.context.uid)
     return HTTPFound(location = url)
示例#2
0
 def vote_success(self, appstruct):
     #Just in case the form rendered before the poll closed
     if not self.can_vote:
         raise HTTPForbidden(_("You're not allowed to vote"))
     Vote = self.poll_plugin.get_vote_class()
     if not IVote.implementedBy(Vote):
         raise TypeError(
             "Poll plugins method get_vote_class returned something that didn't implement IVote."
         )
     appstruct.pop('csrf_token', None)
     userid = self.request.authenticated_userid
     if userid in self.context:
         vote = self.context[userid]
         assert IVote.providedBy(vote), "%r doesn't provide IVote" % vote
         vote.set_vote_data(appstruct)
         success_msg = _("Your vote was changed.")
     else:
         vote = Vote(creators=[userid])
         #We don't need to send events here, since object added will take care of that
         vote.set_vote_data(appstruct, notify=False)
         #To fire events after set_vote_data is done
         self.context[userid] = vote
         success_msg = _(
             "vote_success_msg",
             default="Your vote has been added. If you wish to change it, "
             "you may do so as long as the poll is open.")
     self.flash_messages.add(success_msg)
     return self._remove_modal_response()
示例#3
0
 def poll_form(self):
     """ Return rendered poll form or process a vote. """
     poll_plugin = self.query_poll_plugin(self.context)
     if not poll_plugin:
         return HTTPForbidden()
     schema = poll_plugin.get_vote_schema(self.request, self.api)
     add_csrf_token(self.context, self.request, schema)
     schema = schema.bind(context=self.context,
                          request=self.request,
                          api=self.api)
     form = Form(schema,
                 action=self.request.resource_url(self.context,
                                                  '_poll_form'),
                 buttons=(button_vote, ),
                 formid="vote_form")
     can_vote = has_permission(security.ADD_VOTE, self.context,
                               self.request)
     userid = self.api.userid
     post = self.request.POST
     if 'vote' in post:
         if not can_vote:
             raise HTTPForbidden(u"You're not allowed to vote")
         controls = post.items()
         try:
             appstruct = form.validate(controls)
         except ValidationFailure, e:
             self.response['form'] = e.render()
             return self.response
         Vote = poll_plugin.get_vote_class()
         if not IVote.implementedBy(Vote):
             raise TypeError(
                 "Poll plugins method get_vote_class returned something that didn't implement IVote."
             )
         #Remove crsf_token from appstruct after validation
         appstruct.pop('csrf_token', None)
         if userid in self.context:
             vote = self.context[userid]
             assert IVote.providedBy(vote)
             vote.set_vote_data(appstruct)
         else:
             vote = Vote(creators=[userid])
             #We don't need to send events here, since object added will take care of that
             vote.set_vote_data(appstruct, notify=False)
             #To fire events after set_vote_data is done
             self.context[userid] = vote
         success_msg = _(u"Your vote has been registered!")
         if self.request.is_xhr:
             self.response['success_msg'] = success_msg
             return Response(
                 render("templates/snippets/vote_success.pt",
                        self.response,
                        request=self.request))
         self.api.flash_messages.add(success_msg)
         url = self.request.resource_url(self.context.__parent__,
                                         anchor=self.context.uid)
         return HTTPFound(location=url)
示例#4
0
 def poll_form(self):
     """ Return rendered poll form or process a vote. """
     poll_plugin = self.query_poll_plugin(self.context)
     if not poll_plugin:
         return HTTPForbidden()
     schema = poll_plugin.get_vote_schema(self.request, self.api)
     add_csrf_token(self.context, self.request, schema)
     schema = schema.bind(context=self.context, request=self.request, api = self.api)        
     form = Form(schema,
                 action = self.request.resource_url(self.context, '_poll_form'),
                 buttons = (button_vote,),
                 formid = "vote_form")
     can_vote = has_permission(security.ADD_VOTE, self.context, self.request)
     userid = self.api.userid
     post = self.request.POST
     if 'vote' in post:
         if not can_vote:
             raise HTTPForbidden(u"You're not allowed to vote")
         controls = post.items()
         try:
             appstruct = form.validate(controls)
         except ValidationFailure, e:
             self.response['form'] = e.render()
             return self.response
         Vote = poll_plugin.get_vote_class()
         if not IVote.implementedBy(Vote):
             raise TypeError("Poll plugins method get_vote_class returned something that didn't implement IVote.")
         #Remove crsf_token from appstruct after validation
         appstruct.pop('csrf_token', None)
         if userid in self.context:
             vote = self.context[userid]
             assert IVote.providedBy(vote)
             vote.set_vote_data(appstruct)
         else:
             vote = Vote(creators = [userid])
             #We don't need to send events here, since object added will take care of that
             vote.set_vote_data(appstruct, notify = False)
             #To fire events after set_vote_data is done
             self.context[userid] = vote
         success_msg = _(u"Your vote has been registered!")
         if self.request.is_xhr:
             self.response['success_msg'] = success_msg
             return Response(render("templates/snippets/vote_success.pt", self.response, request = self.request))
         self.api.flash_messages.add(success_msg)
         url = self.request.resource_url(self.context.__parent__, anchor = self.context.uid)
         return HTTPFound(location = url)
示例#5
0
def multiply_votes(obj, event):
    """ This subscriber multiplies votes for members that have received several votes.
    """
    request = get_current_request()
    userid = request.authenticated_userid
    # Only preform this function on the initial vote object
    if userid != obj.__name__:
        return
    meeting = find_interface(obj, IMeeting)
    try:
        multi_votes = meeting[MEETING_NAMESPACE]
    except KeyError:
        # This system isn't active
        return

    vote_counter = (
        multi_votes.get_vote_power(userid) - 1
    )  # Since the initial vote was added already

    if vote_counter:  # Votes might have been exhausted if they only had one!

        poll = obj.__parent__
        poll_plugin = poll.get_poll_plugin()
        initial_vote = poll[userid]
        vote_data = (
            initial_vote.get_vote_data()
        )  # Just to make sure, get from the initial one

        if IObjectAddedEvent.providedBy(event):
            Vote = poll_plugin.get_vote_class()
            assert IVote.implementedBy(Vote)
            for i in range(vote_counter):
                name = "{}_{}".format(initial_vote.uid, i)
                vote = Vote(creators=[userid])
                vote.set_vote_data(vote_data, notify=False)
                poll[name] = vote

        if IObjectUpdatedEvent.providedBy(event):
            for i in range(vote_counter):
                name = "{}_{}".format(initial_vote.uid, i)
                poll[name].set_vote_data(vote_data, notify=False)
示例#6
0
def multiply_votes(obj, event):
    """ This subscriber multiplies votes for delegation members that have received several votes.
        Technically a single member might be part of several delegations, but this will hardly be the case.
        Nevertheless, we need to tace care of any eventualities.
    """
    request = get_current_request()
    userid = request.authenticated_userid
    #Only preform this functon on the inital vote object
    if userid != obj.__name__:
        return
    meeting = find_interface(obj, IMeeting)
    delegations = request.registry.getAdapter(meeting, IMeetingDelegations)
    vote_counter = -1 #Since the current vote was added already
    for delegation in delegations.values():
        vote_counter += delegation.voters.get(userid, 0)
    if not vote_counter > 0:
        return
    
    poll = obj.__parent__
    poll_plugin = poll.get_poll_plugin()
    vote_data = poll[userid].get_vote_data() #Just to make sure, get from the initial one

    if IObjectAddedEvent.providedBy(event):
        Vote = poll_plugin.get_vote_class()
        assert IVote.implementedBy(Vote)
        for i in range(vote_counter):
            name = unicode(uuid4())
            vote = Vote(creators = [userid])
            vote.set_vote_data(vote_data, notify = False)
            poll[name] = vote
            
    if IObjectUpdatedEvent.providedBy(event):
        for vote in poll.get_content(iface = IVote):
            if vote.creators[0] != userid:
                continue
            if vote.__name__ == userid:
                continue
            vote.set_vote_data(vote_data)
def print_voting_timestamps(poll, request):
    print "\n"
    print poll.title
    print "="*80
    print "Start time:".ljust(20), request.dt_handler.format_dt(poll.start_time)
    print "End time:".ljust(20), request.dt_handler.format_dt(poll.end_time)
    print "Minutes since poll started, within this minute. (Ie 1 min, voted in 0-59 seconds)"
    vote_times = {}
    for vote in poll.values():
        if not IVote.providedBy(vote):
            continue
        ts_min = (vote.created - poll.start_time).total_seconds() // 60
        ts_min = int(floor(ts_min) + 1)
        if ts_min not in vote_times:
            vote_times[ts_min] = 0
        vote_times[ts_min] += 1
    sv_times = sorted(vote_times.items(), key=lambda x: x[0])
    print "-"*80
    print "Minutes".ljust(20), "Voters"
    for (k, v) in sv_times:
        print str(k).ljust(20), v
    print "\n"
    print "Total:".ljust(20), sum(vote_times.values())
示例#8
0
 def add(self, name, other, send_events=True):
     assert IVote.providedBy(other)
     super(Poll, self).add(name, other, send_events=send_events)
示例#9
0
 def calculate_ballots(self):
     ballot_counter = Ballots()
     for vote in self.values():
         assert IVote.providedBy(vote)
         ballot_counter.add(vote.get_vote_data())
     return ballot_counter.result()
示例#10
0
            success_msg = _(u"Your vote has been registered!")
            if self.request.is_xhr:
                self.response['success_msg'] = success_msg
                return Response(
                    render("templates/snippets/vote_success.pt",
                           self.response,
                           request=self.request))
            self.api.flash_messages.add(success_msg)
            url = self.request.resource_url(self.context.__parent__,
                                            anchor=self.context.uid)
            return HTTPFound(location=url)

        #No vote recieved, continue to render form
        if userid in self.context:
            vote = self.context[userid]
            assert IVote.providedBy(vote)
            appstruct = vote.get_vote_data()
            #Poll might still be open, in that case the poll should be changable
            readonly = not can_vote
            self.response['form'] = form.render(appstruct=appstruct,
                                                readonly=readonly)
        #User has not voted
        else:
            readonly = not can_vote
            self.response['form'] = form.render(readonly=readonly)
        return self.response

    @view_config(name='modal_poll',
                 context=IPoll,
                 permission=security.VIEW,
                 xhr=True)  #
示例#11
0
 def _recurse(base, results):
     for obj in base.values():
         if IBaseContent.providedBy(obj) or IVote.providedBy(obj):
             results.add(obj)
         _recurse(obj, results)
示例#12
0
 def _recurse(base, results):
     for obj in base.values():
         if IBaseContent.providedBy(obj) or IVote.providedBy(obj):
             results.add(obj)
         _recurse(obj, results)
示例#13
0
 def get_all_votes(self):
     """ Returns all votes in this context. """
     return frozenset([x for x in self.values() if IVote.providedBy(x)])
示例#14
0
                #We don't need to send events here, since object added will take care of that
                vote.set_vote_data(appstruct, notify = False)
                #To fire events after set_vote_data is done
                self.context[userid] = vote
            success_msg = _(u"Your vote has been registered!")
            if self.request.is_xhr:
                self.response['success_msg'] = success_msg
                return Response(render("templates/snippets/vote_success.pt", self.response, request = self.request))
            self.api.flash_messages.add(success_msg)
            url = self.request.resource_url(self.context.__parent__, anchor = self.context.uid)
            return HTTPFound(location = url)
            
        #No vote recieved, continue to render form
        if userid in self.context:
            vote = self.context[userid]
            assert IVote.providedBy(vote)
            appstruct = vote.get_vote_data()
            #Poll might still be open, in that case the poll should be changable
            readonly = not can_vote
            self.response['form'] = form.render(appstruct=appstruct, readonly=readonly)
        #User has not voted
        else:
            readonly = not can_vote
            self.response['form'] = form.render(readonly=readonly)
        return self.response

    @view_config(name = 'modal_poll', context = IPoll, permission = security.VIEW, xhr = True) #
    def poll_view(self):
        """ This is the modal window that opens when you click for instance the vote button
            It will also call the view that renders the actual poll form.
        """
示例#15
0
 def get_all_votes(self):
     """ Returns all votes in this context. """
     return frozenset([x for x in self.values() if IVote.providedBy(x)])