Can upload images on the edit_general_information page as well, but has no checks...
[selectricity-live] / app / controllers / election_controller.rb
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
4 #
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.
9 #
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.
14 #
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/>.
18
19 class ElectionController < ApplicationController
20   require_dependency "raw_voter_list"
21   require_dependency "voter"
22   require_dependency "vote"
23   require_dependency "candidate"
24   layout 'main'
25
26   ## methods for displaying, creating,
27   ## and manipulating election overview data
28   ####################################################################
29
30   def new
31     redirect_to :action => 'general_information'
32   end
33   
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'
39   end
40   
41   def create_election
42     @election = Election.new(params[:election])
43     
44     # default options
45     @election.user = session[:user]
46     @election.anonymous = 1
47     @election.startdate = Time.now
48     
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)
53     end
54     
55     if @election.save
56       flash[:notice] = 'Election was successfully created.'
57       redirect_to :action => 'edit_candidates', :id => @election.id
58     else
59       render :action => 'general_information'
60     end
61   end
62   
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])
67   end
68   
69   def update_general_information
70     @election = Election.find(params[:id])
71     
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       add_theme(@election.embed_custom_string)
74     end
75     
76     if @election.update_attributes(params[:election])
77       flash[:notice] = 'Election was successfully updated.'
78       redirect_to :action => 'show', :id => @election
79     else
80       render :action => 'edit'
81     end
82   end
83   
84   def add_theme(prefix)
85     unless params[:top_bar][:uploaded_data].to_s.empty?
86       top_bar = SkinPicture.new(params[:top_bar])
87       top_bar.filename = prefix + "top_bar"
88       top_bar.save!
89     end
90     unless params[:default_image][:uploaded_data].to_s.empty?
91       default_image = SkinPicture.new(params[:default_image])
92       default_image.filename = prefix + "default_image"
93       default_image.save!
94     end
95     unless params[:bg1][:uploaded_data].to_s.empty?
96       bg1 = SkinPicture.new(params[:bg1])
97       bg1.filename = prefix + "bg1"
98       bg1.save!
99     end
100     unless params[:bg2][:uploaded_data].to_s.empty?
101       bg2 = SkinPicture.new(params[:bg2])
102       bg2.filename = prefix + "bg2"
103       bg2.save!
104     end
105     unless params[:bottom_bar][:uploaded_data].to_s.empty?
106       bottom_bar = SkinPicture.new(params[:bottom_bar])
107       bottom_bar.filename = prefix + "bottom_bar"
108       bottom_bar.save!
109     end
110         
111   end
112   
113   def show
114     @sidebar_content = render_to_string :partial => 'progress',
115                                         :locals => { :page => 'review' }
116
117     @election = Election.find(params[:id])
118     if @election.type == QuickVote
119       redirect_to(:controller => 'quickvote', :action => 'index', :ident => @election.id)
120     end
121       
122   end
123
124   def start_election
125     @election = Election.find(params[:id])
126     
127     @election.voters.each do |voter|
128       voter.vote = Vote.new
129       email_voter voter unless voter.email.nil?
130     end
131
132     @election.activate!
133     redirect_to :action => 'show', :id => @election.id
134   end
135
136   # methods fod display, adding, deleting, and manipulating candidate
137   # information for elections
138   ####################################################################
139   def edit_candidates
140     @sidebar_content = render_to_string :partial => 'progress',
141                                         :locals => { :page => 'candidates' }
142     @election = Election.find( params[:id] )
143   end
144
145   def add_candidate
146     @election = Election.find(params[:id])
147     @candidate = Candidate.new(params[:candidate])
148     @election.candidates << @candidate
149
150     if @candidate.save
151       # check to see if they've uploaded a picture
152       if params[:picture][:uploaded_data]
153         picture = Picture.new(params[:picture])
154         @candidate.picture = picture if picture.save
155       end
156
157       @candidate = Candidate.new
158       redirect_to :action => 'edit_candidates', :id => @election.id
159     else
160       render :action => 'edit_candidates', :id => @election.id
161     end
162   end
163   
164   def delete_candidate
165     candidate = Candidate.find( params[:id] )
166     candidate.destroy
167   end
168
169   def candidate_picture
170     candidate = Candidate.find( params[:id] )
171     send_data( candidate.picture.data,
172                :filename => candidate.picture.filename,
173                :type => candidate.picture.filetype,
174                :disposition => 'inline' )
175   end
176
177   ## methods for displaying, adding, deleting, and manipulating voters
178   ## for a particular election
179   ####################################################################
180   def new_voters
181     redirect_to :action => 'edit_voters', :id => params[:id]
182   end
183   
184   def edit_voters
185     @sidebar_content = render_to_string :partial => 'progress',
186                                         :locals => { :page => 'voters' }
187
188     @election = Election.find( params[:id] )
189     if params.has_key?( :raw_voter_list )
190       process_incoming_voters( params[:raw_voter_list] )
191     end
192     @raw_voter_list = RawVoterList.new
193   end
194   
195   def delete_voter
196     voter = Voter.find( params[:id] )
197     voter.destroy
198   end
199
200   def toggle_authenticated
201     @election = Election.find(params[:id])
202     if params[:authenticated] == "1"
203       @election.authenticated = true
204     else
205       @election.authenticated = false
206     end
207     @election.save
208   end
209   
210   ## methods for computing and printing results
211   ####################################################################
212   def results
213     @election = Election.find( params[:id] )
214     votes = []
215     
216     @election.voters.each do |voter|
217       if voter.vote and voter.vote.confirmed?
218         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
219       end
220     end
221     
222     @voteobj = CloneproofSSDVote.new(votes)
223     @resultobj = @voteobj.result
224     @winners = @resultobj.winners
225     
226     @candidates_by_id = {}
227     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
228     
229   end
230   
231   def detailed_results
232    
233     self.results
234
235     @voter_list = []
236     @vote_list = []
237     
238     @election.voters.each do |voter|
239       if voter.vote and voter.vote.confirmed?
240         @voter_list << voter.email
241               @vote_list << voter.vote
242       end
243     end
244
245     @vote_list.sort!
246     @vote_list.sort! { |a,b| a.token <=> b.token }
247   end
248
249   ## private methods
250   ####################################################################
251   private
252
253     def process_incoming_voters(raw_voter_list)
254       incoming_voters = RawVoterList.new( raw_voter_list )
255
256       unless incoming_voters.entries.empty?
257         incoming_voters.each do |new_voter|
258           new_voter.email.strip! # There's a trailing \r on all but the last in
259                                  # the list!
260           if new_voter.valid?
261             # the new voter should be in good shape. save add to the election
262             @election.voters << new_voter
263                   new_voter.save
264           end
265           # TODO: Can we do some kind of AJAX error message for the voter being invalid?
266         end
267         @election.save
268       end
269  
270       # reset the next time to have a the same default value for emailing
271       @raw_voter_list = RawVoterList.new
272       @raw_voter_list.email = incoming_voters.email
273     end
274
275     def email_voter(voter=nil)
276       if voter
277         VoterNotify.deliver_votestart(voter)
278         voter.contacted=1
279         voter.save
280       end
281     end
282
283 end

Benjamin Mako Hill || Want to submit a patch?