Skip to content

Commit

Permalink
server-side template rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
catwell committed Mar 24, 2012
1 parent 9e1b476 commit 29f1982
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 65 deletions.
4 changes: 3 additions & 1 deletion server/dcz.rb
Expand Up @@ -2,7 +2,9 @@ module Dcz

require "as-extensions"

ASE::need %w{ ohm ohm/contrib rufus/verbs sinatra/base time-ago-in-words pony }
ASE::need %w{
mustache ohm ohm/contrib pony rufus/verbs sinatra/base time-ago-in-words
}

root = File.expand_path(File.dirname(__FILE__))

Expand Down
61 changes: 1 addition & 60 deletions server/dcz/api/out.rb
Expand Up @@ -6,67 +6,8 @@ module Dcz class Api
jsonp(Vote.get_by_voxe_id(vid).candidates.map_m(:info))
end

def scoremap_to_results(scoremap)
scoremap.map do |x|
x.first.info.merge(score: x.second,unit: "")
end
end

def normalized_scoremap_to_results(scoremap)
s = scoremap.inject(0){|s,x| s+x.second}
scoremap.map do |x|
x.first.info.merge(score: ((100*x.second.to_f)/s).round,unit: "%")
end
end

get("/:vid/results/?") do |vid|

vote = Vote.get_by_voxe_id(vid)

uninominal_one_turn = {
electionType: "Système actuel, 1er tour",
electionId: 1,
results: normalized_scoremap_to_results(vote.solve_uninominal_one_turn_bias),
}

uninominal_two_turns = {
electionType: "Système actuel, 2e tour",
electionId: 2,
results: normalized_scoremap_to_results(vote.solve_uninominal_two_turns_bias),
}

elimination = {
electionType: "Scrutin par élimination",
electionId: 3,
results: scoremap_to_results(vote.solve_elimination),
}

borda = {
electionType: "Méthode de Borda",
electionId: 4,
results: scoremap_to_results(vote.solve_borda),
}

ago = (Time.at vote.condorcet_generation).ago_in_words

condorcet = {
electionType: "Méthode de Condorcet (updated #{ago})",
electionId: 5,
results: normalized_scoremap_to_results(vote.solve_condorcet_cached),
}

r = {
results: [
uninominal_one_turn,
uninominal_two_turns,
elimination,
borda,
condorcet
],
nb_votes: vote.opinions.all.size,
}

jsonp(r)
jsonp(Vote.get_by_voxe_id(vid)results_info)
end

end end
62 changes: 62 additions & 0 deletions server/dcz/ohm/vote.rb
@@ -1,3 +1,5 @@
# encoding: utf-8

module Dcz class Vote

attribute(:name,String); index(:name)
Expand Down Expand Up @@ -310,6 +312,66 @@ def solve_condorcet_cached
[[condorcet_winner,100]]
end

# RESULTS

def scoremap_to_results(scoremap)
scoremap.map do |x|
x.first.info.merge(score: x.second,unit: "")
end
end

def normalized_scoremap_to_results(scoremap)
s = scoremap.inject(0){|s,x| s+x.second}
scoremap.map do |x|
x.first.info.merge(score: ((100*x.second.to_f)/s).round,unit: "%")
end
end

def results_info
uninominal_one_turn = {
electionType: "Système actuel, 1er tour",
electionId: 1,
results: normalized_scoremap_to_results(solve_uninominal_one_turn_bias),
}

uninominal_two_turns = {
electionType: "Système actuel, 2e tour",
electionId: 2,
results: normalized_scoremap_to_results(solve_uninominal_two_turns_bias),
}

elimination = {
electionType: "Scrutin par élimination",
electionId: 3,
results: scoremap_to_results(solve_elimination),
}

borda = {
electionType: "Méthode de Borda",
electionId: 4,
results: scoremap_to_results(solve_borda),
}

ago = (Time.at condorcet_generation).ago_in_words

condorcet = {
electionType: "Méthode de Condorcet (updated #{ago})",
electionId: 5,
results: normalized_scoremap_to_results(solve_condorcet_cached),
}

r = {
results: [
uninominal_one_turn,
uninominal_two_turns,
elimination,
borda,
condorcet
],
nb_votes: opinions.all.size,
}
end

end end

#vote.solve_condorcet # TODO REMOVE
27 changes: 23 additions & 4 deletions server/dcz/www.rb
Expand Up @@ -11,12 +11,31 @@ module Dcz; class Www < Sinatra::Base
content_type(:html)
end

ASE::require_part %w{ mail }
TPL = {}

# Helpers

def render(tpl,h={})
unless TPL[tpl]
TPL[tpl] = String::from(File.join(CFG[:path][:tpl],"#{tpl}.mustache"))
end
Mustache.render(TPL[tpl],h)
end

def vote_p2012
Vote.get_by_voxe_id("4f16fe2299c7a10001000012")
end

def cd_info
vote_p2012.candidates.map_m(:info)
end

# / Helpers

ASE::require_part %w{ mail pages }

get("/") do
content_type(:html)
pf = CFG[:sinatra_settings][:public_folder]
String::from("#{pf}/index.html")
render(:index)
end

end end
74 changes: 74 additions & 0 deletions server/dcz/www/pages.rb
@@ -0,0 +1,74 @@
module Dcz class Www

get("/info/?") do |x|
render(:info)
end

get("/results/?") do |x|
r = ""
vote_p2012.results_info.each do |rs|
maxPc = rs[:results].map{|x| x.score}.max
r += <<-EOD
<div class="scrutin" id="#{rs[:electionId]}">
<div class="name"><img src="img/info32.png" class="infoscrutin"/>#{rs[:electionType]}</div>
<div class="winner"><img src="#{rs[:results].first[:piclink_anim]}" /></div>
<div class="resultats">
EOD
rs[:results].each do |cd|
r += <<-EOD
<div class="head"><img src="#{cd[:piclink_anim]}" /></div>
<div class="bar">
<div class="load" style="width: #{(cd[:score]*100/maxPc).round}%;"></div>
<div class="name">#{cd[:name]}</div>
<div class="pc">#{cd[:score]}#{cd[:unit]}</div>
</div><br/>
EOD
end
r += '</div></div>'
end
r += '<div class="clear"></div>'
render(:results,{results: r})
end

get("/first_turn/?") do |x|
r = ""
k = 0
cd_info.each do |cd|
r += <<-EOD
<div class="candidat" id="#{cd[:voxe_id]}">
<div class="pic" style="background: url(#{cd[:piclink72]});"></div>
<div>#{cd[:name]}</div>
<div class="infocandidatdiv">
<a target="#{cd[:voxe_id]}" href="#{cd[:urlinfo]}">
<img src="img/info32.png" class="infocandidat"/>
</a>
</div>
</div>
EOD
end
if k == 2
r += '<div class="clear"></div>'
k = 0
else
k += 1
end
r += '<div class="clear"></div>'
render(:first_turn,{candidates: r})
end

get("/order_candidates/?") do |x|
r = ""
cd_info.each do |cd|
r += <<-EOD
<li id="#{cd[:voxe_id]}" class="ui-state-default">
<img align="left" src="#{cd[:piclink72]}" />
<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
<div>#{cd[:name]}</div>
</li>
EOD
end
vote_p2012.candidates.each.map_m(:info) do |cd|
render(:order_candidates,{candidates: r})
end

end end
43 changes: 43 additions & 0 deletions tpl/first_turn.mustache
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta property="og:title" content="Democrazy"/>
<meta property="og:type" content="article"/>
<meta property="og:image" content="http://democrazy.fr/img/logo1024.png"/>
<meta property="og:url" content="http://democrazy.fr"/>
<meta property="og:site_name" content="democrazy">

<link rel="icon" type="image/png" href="favicon.png">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">

<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>democrazy</title>

<!-- add font from google -->
<link href='http://fonts.googleapis.com/css?family=Cutive&subset=latin,latin-ext' rel='stylesheet' type='text/css'>

<link rel="stylesheet" href="./css/style.css">
<script data-main="js/boot.js" src="js/require.js"></script>
</head>
<body>
<div id="page1" class="page" align="center" style="margin: auto; position:relative">
<div class="startBlock">premier_tour</div>
<div class="actions">
<a href="/" title="se déconnecter" alt="se déconnecter" class="logout"></a>
<span class="customtitle">Scrutin à un tour</a>
</div>
<div class="title customtitle">
Pour quel candidat voteriez vous au premier tour ?
</div>
<div class="candidats" align="center">
{{{candidates}}}
</div>

<div class="send">
<input type="submit" value="Envoyer" class="ui-button ui-widget ui-state-default ui-corner-all" />
</div>
<div class="footer" ></div>
</div>
</body>
</html>
68 changes: 68 additions & 0 deletions tpl/index.mustache
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta property="og:title" content="Democrazy"/>
<meta property="og:type" content="article"/>
<meta property="og:image" content="http://democrazy.fr/img/logo1024.png"/>
<meta property="og:url" content="http://democrazy.fr"/>
<meta property="og:site_name" content="democrazy">

<link rel="icon" type="image/png" href="favicon.png">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">

<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>democrazy</title>

<!-- add font from google -->
<link href='http://fonts.googleapis.com/css?family=Cutive&subset=latin,latin-ext' rel='stylesheet' type='text/css'>

<link rel="stylesheet" href="./css/style.css">
<script data-main="js/boot.js" src="js/require.js"></script>
</head>
<body>
<div id="page0" class="page" align="center" style="margin: auto;">
<div class="startBlock">login</div>
<img class="bgimage" src="img/carteelecbg.jpg"/>
<div class="UserIdf">

<div class="acroche">Vous élisez,<br>les règles du jeu décident.</div>


<div>Pour voter, entrer votre mail<br><input class="rounded" type="text" name="mail" placeholder="Votre mail" /></div>
<div class="send" style="padding-bottom:10px;">
<input type="submit" value="Envoyer" class="ui-button ui-widget ui-state-default ui-corner-all" />
</div>
<div align="center" class="ptttxt">
<br>
<font class="titre">Democrazy est une expérimentation de différents modes de scrutins</font><br>
&bull; Scrutin à un tour &bull;<br>
&bull; Scrutin majoritaire à deux tours (système actuel) &bull;<br>
&bull; Scrutin par élimination &bull;<br>
&bull; Scrutin plurinominal majoritaire (dit de Borda) &bull;<br>
&bull; Méthode de Condorcet &bull;<br>

</div>

<p style="margin:0; line-height:20pt;">
<a style="font-size:11pt; line-height:20pt; background-color:transparent;" href="info" target="info">en savoir plus...</a>
<br>
<a style="line-height:20pt; font-size:11pt; background-color:transparent;" href="results" target="_self" >les résultats...</a>
</p>
<div class="logo">
<a href="first_turn"><img class="letter" src="img/letter.jpg" style="width: 70px; display: inline;" /></a>
<img class="urne" src="img/urne.png" style="width: 280px;" />
</div>
<div class="share">
<div class="fb-like facebook" data-send="false" data-layout="button_count" data-width="100" data-show-faces="false" data-href="https://www.facebook.com/Democrazy2012"></div><div id="fb-root" class="facebook"></div>
<div class="g-plusone" data-size="small"></div>
<a href="https://twitter.com/share" class="twitter-share-button" data-text="Je viens de voter sur" data-lang="fr" data-hashtags="voxe2012" data-url="http://democrazy.fr" data-via="democrazy2012" class="twitter">Tweeter</a>
<script type="text/javascript" src="https://apis.google.com/js/plusone.js">{lang: 'fr'}</script>
<script>(function(d,s,id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
</div>
</div>
</body>
</html>

0 comments on commit 29f1982

Please sign in to comment.