Beispiel #1
0
    def run_election():
        SiteSettingService.set_voting_done()
        success = True

        for (position_id, position_str) in CANDIDATE_POSITIONS:
            # Special Election, only run election for specific candidates
            # Assume data is valid and correct
            num_candidates_for_position = len(
                Candidate.objects.filter(position=position_id))
            if num_candidates_for_position == 0:
                continue

            positions_won = len(
                Candidate.objects.filter(position=position_id, has_won=True))
            while positions_won < POSITION_NUMS[position_id]:
                winning_candidate = VoteService.get_winner_for_position(
                    position_id)

                # if winner found, make them win, remove ballots for position,
                #       remove all other candidate positions for winner
                if winning_candidate != None:
                    positions_won += 1
                    winning_candidate.has_won = True
                    winning_candidate.save()
                    if positions_won == POSITION_NUMS[position_id]:
                        VoteBallot.objects.filter(
                            position=position_id).delete()
                    Candidate.objects.filter(
                        user__pk=winning_candidate.user.pk,
                        has_won=False).delete()
                else:
                    success = False
                    break

            if not success:
                break

        # Clean up the election state
        VoteBallot.objects.all().delete()
        if not success:
            SiteSettingService.set_voting_applications_open()
            for vote_status in VoteStatus.objects.all():
                vote_status.has_voted = False
                vote_status.save()

        return success
Beispiel #2
0
    def get_context_data(self, **kwargs):
        context = super(CandidateListView, self).get_context_data(**kwargs)

        context['voting_closed'] = SiteSettingService.is_voting_closed()
        if context['voting_closed']:
            return context

        context['voting_open'] = SiteSettingService.is_voting_currently()

        context['has_not_voted'] = True
        try:
            vote_status = VoteStatus.objects.get(
                voter__username=self.request.user.username)
            context['has_not_voted'] = not vote_status.has_voted
        except VoteStatus.DoesNotExist:
            pass

        positions_list = []
        for (position_code, position_name) in CANDIDATE_POSITIONS:
            has_winner = False
            has_applied = False
            is_applying_open = SiteSettingService.is_voting_applications_open()
            try:
                list = Candidate.objects.filter(position=position_code)
                for cand in list:
                    if cand.user.pk == self.request.user.pk:
                        has_applied = True
                    if cand.has_won:
                        has_winner = True
            except Candidate.DoesNotExist:
                list = []
            positions_list.append((
                position_name,
                position_code,
                has_winner,
                has_applied,
                is_applying_open,
                list,
            ))
        context['positions'] = positions_list
        return context
Beispiel #3
0
    def clean(self):
        application = self.save(commit=False)
        # This will validate if forms can be submitted or not

        if application.is_submitted:
            raise forms.ValidationError("Applications was already submitted")

        if not SiteSettingService.is_rush_open():
            raise forms.ValidationError("Application is late")

        if 'submit_btn' in self.data:
            application.is_submitted = True
Beispiel #4
0
    def test_func(self):
        if self.request.user.is_anonymous:
            return False

        if not SiteSettingService.is_voting_currently():
            return False

        has_not_voted_yet = True
        try:
            vote_status = VoteStatus.objects.get(
                voter__username=self.request.user.username)
            has_not_voted_yet = not vote_status.has_voted
        except VoteStatus.DoesNotExist:
            # Not allowed to vote
            return False
        return self.request.user.is_active_user() and has_not_voted_yet
Beispiel #5
0
    def test_func(self):
        if self.request.user.is_anonymous:
            return False

        if not SiteSettingService.is_voting_applications_open():
            return False

        has_not_applied_yet = True
        try:
            Candidate.objects.get(position=self.kwargs.get("position"),
                                  user__username=self.request.user.username)
            has_not_applied_yet = False
        except Candidate.DoesNotExist:
            pass
        return (self.request.user.is_pledge()
                or self.request.user.is_active_user()) and has_not_applied_yet
Beispiel #6
0
    def clean(self):
        # Check if voting is open
        if not SiteSettingService.is_voting_currently():
            raise forms.ValidationError("Voting is not open!")

        # Check if user hasn't already voted
        vote_status_query = VoteStatus.objects.filter(
            voter__username=self.user.username)
        if len(vote_status_query) != 0 and vote_status_query[0].has_voted:
            raise forms.ValidationError("You have already voted!")

        # Check if inputs are valid
        for (position_id, position_str) in CANDIDATE_POSITIONS:
            num_win_candidates = len(
                Candidate.objects.filter(position=position_id, has_won=True))
            # If we have all our winners, no need to fill this out.
            if num_win_candidates == POSITION_NUMS[position_id]:
                continue

            num_position_cand = len(
                Candidate.objects.filter(position=position_id))
            candidate_position_set = set()
            has_voted_none = False

            for i in range(num_position_cand):
                form_tag = position_id + '_' + str(i)
                candidate_id = int(self.cleaned_data[form_tag])
                if (has_voted_none and candidate_id != -1
                    ) or candidate_id in candidate_position_set:
                    raise forms.ValidationError(
                        "You filled your vote ballet wrong in the %s section!"
                        % position_str)
                if candidate_id == -1:
                    has_voted_none = True
                else:
                    candidate_position_set.update(
                        {int(self.cleaned_data[form_tag])})

        return self.cleaned_data
Beispiel #7
0
 def get(self, request, format=None):
     return Response({ 'rush': SiteSettingService.is_rush_open()})
Beispiel #8
0
 def is_open_for_signup(self, request):
     return SiteSettingService.is_rush_open()