Esempio n. 1
0
def vote(request, template_name='vote.html'):
    eb = ElectionBusiness()
    context = {}
    signer = Signer()
    if eb.isOccurring():
        # Check if elector already voted.
        if Voted.objects.filter(elector__user=request.user).count() > 0:
            messages.warning(request, 'You have already voted.')   
            return render(request, template_name, context)

        positions = Position.objects.all()
        if request.method == 'POST':
            try:
                for p in positions:
                    pname = 'position{}'.format(p.id)
                    candidate_id = request.POST.get(pname,"")
                    candidate = Candidate.objects.get(id=int(candidate_id))
                    cv, created = CandidateVote.objects.get_or_create(candidate=candidate)
                    cv.quantity = cv.quantity + 1
                    cv.save()
                elector = Elector.objects.get(user=request.user)
                voted = Voted.objects.create(elector=elector)
                voted.system_signature = signer.sign(str(elector.id))
                voted.save()
                messages.success(request, 'Your vote was registred successfully')
            except Exception as e:
                messages.error(request, str(e))
        else:
            context['positions'] = positions
            context['quantity_of_positions'] = positions.count()
    else:
        messages.error(request, 'Election has finished.')
    
    return render(request, template_name, context)
Esempio n. 2
0
 def shouldAddBlock(self):
     eb = ElectionBusiness()
     # While election is occurring, new blocks should be generated.
     if eb.isOccurring():
         return True
     if eb.isLocked() and eb.hadFinished():
         return True
     ec = eb.getCurrentElectionConfig()
     if not ec:
         raise DatabaseError('There is not Election Configuration for the current election')
     # Independently of election is occurring, if we have votes, a new block must be generated.
     if Voted.objects.filter(hash_val__isnull=True).count() > 0:
         return True
     # If election is not occurring and we do not have votes we do not need to generate a block.
     return False
Esempio n. 3
0
    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        eb = ElectionBusiness()
        election_is_occurring = eb.isOccurring()
        election_is_locked = eb.isLocked()
        request.election_is_occurring = election_is_occurring
        request.election_is_locked = election_is_locked

        response = self.get_response(request)

        response.election_is_occurring = election_is_occurring
        response.election_is_locked = election_is_locked
        # Code to be executed for each request/response after
        # the view is called.

        return response