@@ -3,9 +3,6 @@ require 'sinatra' | |||||
require 'sinatra-helpers/haml/partials' | require 'sinatra-helpers/haml/partials' | ||||
require 'haml' | require 'haml' | ||||
require 'pat' | require 'pat' | ||||
require 'dm-core' | |||||
require 'dm-validations' | |||||
require 'dm-timestamps' | |||||
require 'lib/models' | require 'lib/models' | ||||
get '/' do | get '/' do | ||||
@@ -14,33 +11,26 @@ end | |||||
get '/wards/:id' do | get '/wards/:id' do | ||||
@ward = Ward.get(params[:id]) | @ward = Ward.get(params[:id]) | ||||
@council_candidates = Councilcandidate.all( :ward_id => @ward.id, :order => [ 'surname' ] ) | |||||
@parly_candidates = Parliamentcandidate.all( :constituency_id => @ward.constituency.id, :order => [ 'surname' ]) | |||||
haml :wards | haml :wards | ||||
end | end | ||||
get '/wards' do | get '/wards' do | ||||
@postcode = params[:postcode].strip.upcase | @postcode = params[:postcode].strip.upcase | ||||
# Postcode not found/invalid | |||||
# Postcode valid but not in LB Sutton | |||||
result = Pat.get(@postcode) | result = Pat.get(@postcode) | ||||
# Invalid postcode | |||||
if result.code == 404 | if result.code == 404 | ||||
redirect '/error' | redirect '/error' | ||||
end | end | ||||
@district_name = result['administrative']['district']['title'] | |||||
if @district_name != "Sutton London Borough Council" | |||||
# Postcode valid but not in LB Sutton | |||||
if result['administrative']['district']['title'] != "Sutton London Borough Council" | |||||
redirect '/aliens' | redirect '/aliens' | ||||
end | end | ||||
@ward_name = result['administrative']['ward']['title'] | |||||
@ward = Ward.first( { :name => @ward_name } ) | |||||
@council_candidates = Councilcandidate.all( :ward_id => @ward.id, :order => [ 'surname' ]) | |||||
@parly_candidates = Parliamentcandidate.all( :constituency_id => @ward.constituency.id, :order => [ 'surname' ]) | |||||
# Postcode in LB Sutton | |||||
@ward = Ward.first( :name => result['administrative']['ward']['title'] ) | |||||
haml :wards | haml :wards | ||||
end | end | ||||
@@ -1,3 +1,7 @@ | |||||
require 'dm-core' | |||||
require 'dm-validations' | |||||
require 'dm-timestamps' | |||||
class Postcode | class Postcode | ||||
include DataMapper::Resource | include DataMapper::Resource | ||||
@@ -19,9 +23,14 @@ class Ward | |||||
property :ons_id, String, :required => true | property :ons_id, String, :required => true | ||||
property :name, String, :required => true | property :name, String, :required => true | ||||
property :constituency_id, Integer, :required => true | property :constituency_id, Integer, :required => true | ||||
has n, :councilcandidates | |||||
has n, :councilcandidates, :order => 'surname' | |||||
belongs_to :constituency | belongs_to :constituency | ||||
def self.slugify(name) | |||||
name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase | |||||
end | |||||
end | end | ||||
class Party | class Party | ||||
@@ -30,8 +39,8 @@ class Party | |||||
property :id, Serial | property :id, Serial | ||||
property :name, String, :required => true | property :name, String, :required => true | ||||
has n, :councilcandidates | |||||
has n, :parliamentcandidates | |||||
has n, :councilcandidates, :order => 'surname' | |||||
has n, :parliamentcandidates, :order => 'surname' | |||||
end | end | ||||
class Councilcandidate | class Councilcandidate | ||||
@@ -70,8 +79,8 @@ class Constituency | |||||
property :id, Serial | property :id, Serial | ||||
property :name, String, :required => true | property :name, String, :required => true | ||||
has n, :wards | |||||
has n, :parliamentcandidates | |||||
has n, :wards, :order => 'name' | |||||
has n, :parliamentcandidates, :order => 'surname' | |||||
end | end | ||||
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db.sqlite3") | DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db.sqlite3") |
@@ -9,4 +9,5 @@ | |||||
You can get a registration form at the | You can get a registration form at the | ||||
%a{ :href => "http://www.aboutmyvote.co.uk/" } About My Vote | %a{ :href => "http://www.aboutmyvote.co.uk/" } About My Vote | ||||
website or by phoning Sutton Council on | website or by phoning Sutton Council on | ||||
%span.phone 020 8770 4888. | |||||
%span.phone 020 8770 4888. | |||||
@@ -3,4 +3,5 @@ | |||||
%p This site only has information about elections in the London Borough of Sutton. | %p This site only has information about elections in the London Borough of Sutton. | ||||
%p Your postcode is outside Sutton. Sorry, we can't help you further. | |||||
%p Your postcode is outside Sutton. Sorry, we can't help you further. | |||||
@@ -1,4 +1,5 @@ | |||||
.grid_12 | .grid_12 | ||||
%h1 Invalid postcode | %h1 Invalid postcode | ||||
%p | %p | ||||
%a{ :href => "/" }Please go back and try again | |||||
%a{ :href => "/" }Please go back and try again | |||||
@@ -44,7 +44,7 @@ | |||||
.clear | .clear | ||||
.grid_6 | .grid_6 | ||||
- for candidate in @parly_candidates | |||||
- for candidate in @ward.constituency.parliamentcandidates | |||||
%p.vcard | %p.vcard | ||||
%span.candidate_name.fn | %span.candidate_name.fn | ||||
= candidate.forenames.split(' ')[0] | = candidate.forenames.split(' ')[0] | ||||
@@ -61,7 +61,7 @@ | |||||
MP. | MP. | ||||
.grid_6 | .grid_6 | ||||
- for candidate in @council_candidates | |||||
- for candidate in @ward.councilcandidates | |||||
%p.vcard | %p.vcard | ||||
%span.candidate_name.fn | %span.candidate_name.fn | ||||
= candidate.forenames.split(' ')[0] | = candidate.forenames.split(' ')[0] | ||||