1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with this program. If not, see
17 # <http://www.gnu.org/licenses/>.
19 class ElectionController < ApplicationController
20 require_dependency "raw_voter_list"
21 require_dependency "voter"
22 require_dependency "vote"
23 require_dependency "candidate"
26 ## methods for displaying, creating,
27 ## and manipulating election overview data
28 ####################################################################
31 redirect_to :action => 'general_information'
34 def general_information
35 @sidebar_content = render_to_string :partial => 'progress',
36 :locals => { :page => 'overview' }
37 @election = Election.new
38 render :action => 'general_information'
42 @election = Election.new(params[:election])
45 @election.user = session[:user]
46 @election.anonymous = 1
47 @election.startdate = Time.now
49 unless params[:top_bar][:uploaded_data].to_s.empty?
50 token_generator = UniqueTokenGenerator.new( 16 )
51 @election.embed_custom_string = token_generator.token
52 add_theme(@election.embed_custom_string)
56 flash[:notice] = 'Election was successfully created.'
57 redirect_to :action => 'edit_candidates', :id => @election.id
59 render :action => 'general_information'
63 # TODO add filter to verify that the person working on or looking at
64 # something is the owner
65 def edit_general_information
66 @election = Election.find(params[:id])
69 def update_general_information
70 @election = Election.find(params[:id])
72 unless (params[:top_bar][:uploaded_data].to_s.empty? and params[:default_image][:uploaded_data].to_s.empty? and params[:bg1][:uploaded_data].to_s.empty? and params[:bg2][:uploaded_data].to_s.empty? and params[:bottom_bar][:uploaded_data].to_s.empty?)
73 unless @election.embed_custom.string
74 token_generator = UniqueTokenGenerator.new( 16 )
75 @election.embed_custom_string = token_generator.token
78 add_theme(@election.embed_custom_string)
81 if @election.update_attributes(params[:election])
82 flash[:notice] = 'Election was successfully updated.'
83 redirect_to :action => 'show', :id => @election
85 render :action => 'edit'
90 unless params[:top_bar][:uploaded_data].to_s.empty?
91 top_bar = SkinPicture.new(params[:top_bar])
92 top_bar.filename = prefix + "top_bar"
95 unless params[:default_image][:uploaded_data].to_s.empty?
96 default_image = SkinPicture.new(params[:default_image])
97 default_image.filename = prefix + "default_image"
100 unless params[:bg1][:uploaded_data].to_s.empty?
101 bg1 = SkinPicture.new(params[:bg1])
102 bg1.filename = prefix + "bg1"
105 unless params[:bg2][:uploaded_data].to_s.empty?
106 bg2 = SkinPicture.new(params[:bg2])
107 bg2.filename = prefix + "bg2"
110 unless params[:bottom_bar][:uploaded_data].to_s.empty?
111 bottom_bar = SkinPicture.new(params[:bottom_bar])
112 bottom_bar.filename = prefix + "bottom_bar"
119 @sidebar_content = render_to_string :partial => 'progress',
120 :locals => { :page => 'review' }
122 @election = Election.find(params[:id])
123 if @election.type == QuickVote
124 redirect_to(:controller => 'quickvote', :action => 'index', :ident => @election.id)
130 @election = Election.find(params[:id])
132 @election.voters.each do |voter|
133 voter.vote = Vote.new
134 email_voter voter unless voter.email.nil?
138 redirect_to :action => 'show', :id => @election.id
141 # methods fod display, adding, deleting, and manipulating candidate
142 # information for elections
143 ####################################################################
145 @sidebar_content = render_to_string :partial => 'progress',
146 :locals => { :page => 'candidates' }
147 @election = Election.find( params[:id] )
151 @election = Election.find(params[:id])
152 @candidate = Candidate.new(params[:candidate])
153 @election.candidates << @candidate
156 # check to see if they've uploaded a picture
157 if params[:picture][:uploaded_data]
158 picture = Picture.new(params[:picture])
159 @candidate.picture = picture if picture.save
162 @candidate = Candidate.new
163 redirect_to :action => 'edit_candidates', :id => @election.id
165 render :action => 'edit_candidates', :id => @election.id
170 candidate = Candidate.find( params[:id] )
174 def candidate_picture
175 candidate = Candidate.find( params[:id] )
176 send_data( candidate.picture.data,
177 :filename => candidate.picture.filename,
178 :type => candidate.picture.filetype,
179 :disposition => 'inline' )
182 ## methods for displaying, adding, deleting, and manipulating voters
183 ## for a particular election
184 ####################################################################
186 redirect_to :action => 'edit_voters', :id => params[:id]
190 @sidebar_content = render_to_string :partial => 'progress',
191 :locals => { :page => 'voters' }
193 @election = Election.find( params[:id] )
194 if params.has_key?( :raw_voter_list )
195 process_incoming_voters( params[:raw_voter_list] )
197 @raw_voter_list = RawVoterList.new
201 voter = Voter.find( params[:id] )
205 def toggle_authenticated
206 @election = Election.find(params[:id])
207 if params[:authenticated] == "1"
208 @election.authenticated = true
210 @election.authenticated = false
215 ## methods for computing and printing results
216 ####################################################################
218 @election = Election.find( params[:id] )
221 @election.voters.each do |voter|
222 if voter.vote and voter.vote.confirmed?
223 votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
227 @voteobj = CloneproofSSDVote.new(votes)
228 @resultobj = @voteobj.result
229 @winners = @resultobj.winners
231 @candidates_by_id = {}
232 @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
243 @election.voters.each do |voter|
244 if voter.vote and voter.vote.confirmed?
245 @voter_list << voter.email
246 @vote_list << voter.vote
251 @vote_list.sort! { |a,b| a.token <=> b.token }
255 ####################################################################
258 def process_incoming_voters(raw_voter_list)
259 incoming_voters = RawVoterList.new( raw_voter_list )
261 unless incoming_voters.entries.empty?
262 incoming_voters.each do |new_voter|
263 new_voter.email.strip! # There's a trailing \r on all but the last in
266 # the new voter should be in good shape. save add to the election
267 @election.voters << new_voter
270 # TODO: Can we do some kind of AJAX error message for the voter being invalid?
275 # reset the next time to have a the same default value for emailing
276 @raw_voter_list = RawVoterList.new
277 @raw_voter_list.email = incoming_voters.email
280 def email_voter(voter=nil)
282 VoterNotify.deliver_votestart(voter)