def send_vote_reminder(): elections = Election.objects.all() for election in elections: end_date_str = election.end_date.split(' ')[0].split('-') today = datetime.today() if not (today.year == int(end_date_str[0]) and today.month == int( end_date_str[1]) and today.day == int(end_date_str[2])): continue msg = "Subject: Election " + election.name + " Ending Soon!\n\nMake sure to login to eLect today to cast your vote before the election closes." registered_voters = RegisterLink.objects.filter(election=election) for voter in registered_voters: if not VoterToElection.objects.filter(election=election, voter=voter.participant): try: sendMail(voter.participant.email, msg) except: continue
def AddElectionRestrictions(request): try: election = Election.objects.get(pk=request.data['election_id']) except: return JsonResponse({ 'success': False, 'error': 'Election not found, make sure election_id is sent correctly.' }) if 'max_voters' in request.data.keys() and request.data['max_voters'] > 0: try: max_voters = int(request.data['max_voters']) except: return JsonResponse({ 'success': False, 'error': 'max_voters must be a valid integer' }) election.max_voters = max_voters election.save() # generate max_voters tokens and email them to host keys = random.sample(range(1, 999999), election.max_voters) for key in keys: new_key = ElectionKey.objects.create(election_id=election.pk, key=key) sendMail( election.creator.email, "Subject: Your Keys\n\nSend each voter one of these private passcodes to give them access to vote in your election\n\n" + ', '.join(str(e) for e in keys)) try: if 'email_domain' in request.data.keys( ) and request.data['email_domain'] != '': election.email_domain = request.data['email_domain'] election.save() except: return JsonResponse({ 'success': False, 'error': 'Invalid email domain' }) return JsonResponse({'success': True})
def Notify(request): user = User.objects.get(pk=request.user.pk) try: election = Election.objects.get(pk=request.data['election_id']) except: return JsonResponse({ 'success': False, 'error': 'Election not found, make sure election_id is sent correctly.' }) voters = VoterToElection.objects.filter(election=election) for voter in voters: msg = "Subject: The Results Are In!\n\nThe results are in for " + election.name + ".\n Login to eLect to view them." sendMail(voter.voter.email, msg) return JsonResponse({'success': True, 'live': True})
def PublicRegister(request): user = User.objects.get(pk=request.user.pk) try: election = Election.objects.get(pk=request.data['election_id']) except: return JsonResponse({ 'success': False, 'error': 'Election not found, make sure election_id is sent correctly.' }) if election.max_voters > 0: num_registered = len(RegisterLink.objects.filter(election=election)) if num_registered >= election.max_voters: return JsonResponse({ 'success': False, 'error': 'Max number of voters has been reached' }) if election.email_domain != "": if election.email_domain not in request.user.email: return JsonResponse({ 'success': False, 'error': 'User cannot register for election because of email restrictions' }) registeredUser = RegisterLink.objects.create(election=election, participant=user) msg = "Subject: You're Registered!\n\nYou have been successfully registered for " + election.name sendMail(user.email, msg) return JsonResponse({'success': True})
def Cast(request): user = User.objects.get(pk=request.user.pk) try: election = Election.objects.get(pk=request.data['election_id']) except: return JsonResponse({ 'success': False, 'error': 'Election not found, make sure election_id is sent correctly.' }) ballot_items = BallotItem.objects.filter(election=election) if not ballot_items: return JsonResponse({ 'success': False, 'error': 'No ballot items found for this election' }) try: answer = request.data['candidate'] except: return JsonResponse({ 'success': False, 'error': 'Candidate choice not sent' }) json = canUserVote(user, election) if json: return json valid_candidate = False for ballot_item in ballot_items: ballot_item_choices = BallotItemChoice.objects.filter( ballot_item=ballot_item) for ballot_item_choice in ballot_item_choices: if ballot_item_choice.answer == answer: valid_candidate = True if not valid_candidate: return JsonResponse({'success': False, "error": "invalid candidate"}) issue_val = 0 election_id = election.pk selection_val = answer headers = {'Content-Type': 'application/x-www-form-urlencoded'} vote_corda_url = "http://206.81.10.10:10050/put?electionVal=O=Host0,L=London,C=GB&voter=O=Voter,L=NewYork,C=US" vote_corda_url += "&issueVal=" + str(issue_val) vote_corda_url += "&selectionVal=" + str(selection_val) vote_corda_url += "&electionID=" + str(election_id) data = {} try: response = requests.post(url=vote_corda_url, data=data, headers=headers) except: return JsonResponse({ 'success': False, 'error': 'Corda API error while voting' }) voter_to_election = VoterToElection.objects.create(election=election, voter=user) msg = "Subject: You've Voted!\n\nYou have been successfully voted in " + election.name sendMail(user.email, msg) return JsonResponse({'success': True})
def Register(request): user = User.objects.get(pk=request.user.pk) try: election = Election.objects.get(pk=request.data['election_id']) except: return JsonResponse({ 'success': False, 'error': 'Election not found, make sure election_id is sent correctly.' }) registration_check = RegisterLink.objects.filter(election=election, participant=user) if len(registration_check) != 0: return JsonResponse({ 'success': False, 'error': 'User has already registered in this election' }) try: passcode = request.data['passcode'] except: return JsonResponse({ 'success': False, 'error': 'Make sure to send passcode in JSON data' }) keys = ElectionKey.objects.filter(election=election) list_of_keys = [] for key in keys: list_of_keys.append(key.key) if passcode != election.passcode and passcode not in list_of_keys: return JsonResponse({'success': False, 'error': 'Invalid passcode'}) if passcode in list_of_keys: key = keys.get(key=passcode) key.delete() if election.max_voters > 0: num_registered = len(RegisterLink.objects.filter(election=election)) if num_registered >= election.max_voters: return JsonResponse({ 'success': False, 'error': 'Max number of voters has been reached' }) if election.email_domain != "": if election.email_domain not in request.user.email: return JsonResponse({ 'success': False, 'error': 'User cannot register for election because of email restrictions' }) registeredUser = RegisterLink.objects.create(election=election, participant=user) msg = "Subject: You're Registered!\n\nYou have been successfully registered for " + election.name sendMail(user.email, msg) return JsonResponse({'success': True})