Streamlined and made more consistent the code that checks for custom skin uploads...
[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     holder = create_theme_hash
50     unless holder.values.all? {|v| v.has_value?("")}
51       token_generator = UniqueTokenGenerator.new( 16 )
52       @election.embed_custom_string = token_generator.token
53       add_theme(@election.embed_custom_string)
54     end
55     
56     if @election.save
57       flash[:notice] = 'Election was successfully created.'
58       redirect_to :action => 'edit_candidates', :id => @election.id
59     else
60       render :action => 'general_information'
61     end
62   end
63   
64   def create_theme_hash
65     target = Hash.new
66     params.each do |k,v|
67       target[k] = v if k=="top_bar" or k=="default_image" or k=="bg1" \
68                     or k=="bg2" or k=="bottom_bar"
69     end
70     return target
71   end
72   
73   # TODO add filter to verify that the person working on or looking at
74   # something is the owner
75   def edit_general_information
76     @election = Election.find(params[:id])
77   end
78   
79   def update_general_information
80     @election = Election.find(params[:id])
81     
82     
83     holder = create_theme_hash
84     unless holder.values.all? {|v| v.has_value?("")}
85       unless @election.embed_custom_string
86         token_generator = UniqueTokenGenerator.new( 16 )
87         @election.embed_custom_string = token_generator.token
88       end
89       
90       add_theme(@election.embed_custom_string)
91     end
92     
93     if @election.update_attributes(params[:election])
94       flash[:notice] = 'Election was successfully updated.'
95       redirect_to :action => 'show', :id => @election
96     else
97       render :action => 'edit'
98     end
99   end
100   
101   #Takes care of uploading custom images -- unnecessarily long, how can I compress?
102   def add_theme(prefix)
103     unless params[:top_bar][:uploaded_data].to_s.empty?
104       previous = SkinPicture.find(:first,
105       :conditions => ["filename = ?", @election.embed_custom_string + "top_bar.png"])
106       if previous
107         previous.destroy
108       end
109       top_bar = SkinPicture.new(params[:top_bar])
110       top_bar.filename = prefix + "top_bar." + params[:top_bar][:uploaded_data].content_type[6..-2]
111       top_bar.save
112     end
113     unless params[:default_image][:uploaded_data].to_s.empty?
114       previous = SkinPicture.find(:first,
115       :conditions => ["filename = ?", @election.embed_custom_string + "default_image.png"])
116       if previous
117         previous.destroy
118       end
119       default_image = SkinPicture.new(params[:default_image])
120       default_image.filename = prefix + "default_image." + params[:default_image][:uploaded_data].content_type[6..-2]
121       default_image.save
122     end
123     unless params[:bg1][:uploaded_data].to_s.empty?
124       previous = SkinPicture.find(:first,
125       :conditions => ["filename = ?", @election.embed_custom_string + "bg1.png"])
126       if previous
127         previous.destroy
128       end
129       bg1 = SkinPicture.new(params[:bg1])  
130       bg1.filename = prefix + "bg1." + params[:bg1][:uploaded_data].content_type[6..-2]
131       bg1.save
132     end
133     unless params[:bg2][:uploaded_data].to_s.empty?
134       previous = SkinPicture.find(:first,
135       :conditions => ["filename = ?", @election.embed_custom_string + "bg2.png"])
136       if previous
137         previous.destroy
138       end
139       bg2 = SkinPicture.new(params[:bg2]) 
140       bg2.filename = prefix + "bg2." + params[:bg2][:uploaded_data].content_type[6..-2]
141       bg2.save
142     end
143     unless params[:bottom_bar][:uploaded_data].to_s.empty?
144       previous = SkinPicture.find(:first,
145       :conditions => ["filename = ?", @election.embed_custom_string + "bottom_bar.png"])
146       if previous
147         previous.destroy
148       end
149       bottom_bar = SkinPicture.new(params[:bottom_bar])
150       bottom_bar.filename = prefix + "bottom_bar." + params[:bottom_bar][:uploaded_data].content_type[6..-2]
151       bottom_bar.save
152     end
153         
154   end
155   
156   def show
157     @sidebar_content = render_to_string :partial => 'progress',
158                                         :locals => { :page => 'review' }
159
160     @election = Election.find(params[:id])
161     if @election.type == QuickVote
162       redirect_to(:controller => 'quickvote', :action => 'index', :ident => @election.id)
163     end
164       
165   end
166
167   def start_election
168     @election = Election.find(params[:id])
169     
170     @election.voters.each do |voter|
171       voter.vote = Vote.new
172       email_voter voter unless voter.email.nil?
173     end
174
175     @election.activate!
176     redirect_to :action => 'show', :id => @election.id
177   end
178
179   # methods fod display, adding, deleting, and manipulating candidate
180   # information for elections
181   ####################################################################
182   def edit_candidates
183     @sidebar_content = render_to_string :partial => 'progress',
184                                         :locals => { :page => 'candidates' }
185     @election = Election.find( params[:id] )
186   end
187
188   def add_candidate
189     @election = Election.find(params[:id])
190     @candidate = Candidate.new(params[:candidate])
191     @election.candidates << @candidate
192
193     if @candidate.save
194       # check to see if they've uploaded a picture
195       if params[:picture][:uploaded_data]
196         picture = Picture.new(params[:picture])
197         @candidate.picture = picture if picture.save
198       end
199
200       @candidate = Candidate.new
201       redirect_to :action => 'edit_candidates', :id => @election.id
202     else
203       render :action => 'edit_candidates', :id => @election.id
204     end
205   end
206   
207   def delete_candidate
208     candidate = Candidate.find( params[:id] )
209     candidate.destroy
210   end
211
212   def candidate_picture
213     candidate = Candidate.find( params[:id] )
214     send_data( candidate.picture.data,
215                :filename => candidate.picture.filename,
216                :type => candidate.picture.filetype,
217                :disposition => 'inline' )
218   end
219
220   ## methods for displaying, adding, deleting, and manipulating voters
221   ## for a particular election
222   ####################################################################
223   def new_voters
224     redirect_to :action => 'edit_voters', :id => params[:id]
225   end
226   
227   def edit_voters
228     @sidebar_content = render_to_string :partial => 'progress',
229                                         :locals => { :page => 'voters' }
230
231     @election = Election.find( params[:id] )
232     if params.has_key?( :raw_voter_list )
233       process_incoming_voters( params[:raw_voter_list] )
234     end
235     @raw_voter_list = RawVoterList.new
236   end
237   
238   def delete_voter
239     voter = Voter.find( params[:id] )
240     voter.destroy
241   end
242
243   def toggle_authenticated
244     @election = Election.find(params[:id])
245     if params[:authenticated] == "1"
246       @election.authenticated = true
247     else
248       @election.authenticated = false
249     end
250     @election.save
251   end
252   
253   ## methods for computing and printing results
254   ####################################################################
255   def results
256     @election = Election.find( params[:id] )
257     votes = []
258     
259     @election.voters.each do |voter|
260       if voter.vote and voter.vote.confirmed?
261         votes << voter.vote.rankings.sort.collect {|vote| vote.candidate_id}
262       end
263     end
264     
265     @voteobj = CloneproofSSDVote.new(votes)
266     @resultobj = @voteobj.result
267     @winners = @resultobj.winners
268     
269     @candidates_by_id = {}
270     @election.candidates.each {|cand| @candidates_by_id[cand.id] = cand}
271     
272   end
273   
274   def detailed_results
275    
276     self.results
277
278     @voter_list = []
279     @vote_list = []
280     
281     @election.voters.each do |voter|
282       if voter.vote and voter.vote.confirmed?
283         @voter_list << voter.email
284               @vote_list << voter.vote
285       end
286     end
287
288     @vote_list.sort!
289     @vote_list.sort! { |a,b| a.token <=> b.token }
290   end
291
292   ## private methods
293   ####################################################################
294   private
295
296     def process_incoming_voters(raw_voter_list)
297       incoming_voters = RawVoterList.new( raw_voter_list )
298
299       unless incoming_voters.entries.empty?
300         incoming_voters.each do |new_voter|
301           new_voter.email.strip! # There's a trailing \r on all but the last in
302                                  # the list!
303           if new_voter.valid?
304             # the new voter should be in good shape. save add to the election
305             @election.voters << new_voter
306                   new_voter.save
307           end
308           # TODO: Can we do some kind of AJAX error message for the voter being invalid?
309         end
310         @election.save
311       end
312  
313       # reset the next time to have a the same default value for emailing
314       @raw_voter_list = RawVoterList.new
315       @raw_voter_list.email = incoming_voters.email
316     end
317
318     def email_voter(voter=nil)
319       if voter
320         VoterNotify.deliver_votestart(voter)
321         voter.contacted=1
322         voter.save
323       end
324     end
325
326 end

Benjamin Mako Hill || Want to submit a patch?