Election results in the London Borough of Sutton.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

242 line
7.5 KiB

  1. require 'data_mapper'
  2. class Poll
  3. include DataMapper::Resource
  4. property :district_id, Integer, :key => true
  5. property :election_id, Integer, :key => true
  6. property :electorate, Integer # The number of people eligible to vote in this district in this election
  7. property :ballot_papers_issued, Integer # The number of ballot papers issued (includes spoiled ballots)
  8. property :seats, Integer, :required => true # The number of seats to be elected in this district in this election
  9. def turnout_percent
  10. @ballot_papers_issued.to_f / @electorate.to_f * 100.0
  11. end
  12. belongs_to :election
  13. belongs_to :district
  14. end
  15. class Postcode
  16. include DataMapper::Resource
  17. # Postcode natural key, uppercase with space, eg. "SM1 1EA"
  18. # Column names derived from Ordnance Survey CodePoint Open
  19. property :postcode, String, :key => true
  20. property :positional_quality_indicator, Integer
  21. property :eastings, Integer, :required => true
  22. property :northings, Integer, :required => true
  23. property :country_code, String, :required => true
  24. property :nhs_regional_ha_code, String, :required => true
  25. property :nhs_ha_code, String, :required => true
  26. property :admin_county_code, String # NULL within Greater London
  27. property :admin_district_code, String, :required => true # e.g. London Borough of Sutton
  28. property :admin_ward_code, String, :required => true # e.g. Sutton Central
  29. property :lat, Float, :required => true
  30. property :lng, Float, :required => true
  31. property :ward_id, Integer, :required => true # Sutton Council
  32. property :constituency_id, Integer, :required => false # UK Parliament
  33. belongs_to :district, :child_key => [:ward_id]
  34. def self.finder(postcode)
  35. postcode = postcode.strip.upcase
  36. if o = self.get(postcode)
  37. return o
  38. end
  39. result = Pat.get(postcode)
  40. unless result.code == 404
  41. # cache API result
  42. self.create(
  43. :postcode => postcode,
  44. :lat => result['geo']['lat'],
  45. :lng => result['geo']['lng'],
  46. :district_name => result['administrative']['district']['title'],
  47. :district_code => result['administrative']['district']['uri'].match(/.+\/(.+)$/)[1],
  48. :ward_name => result['administrative']['ward']['title'],
  49. :ward_code => result['administrative']['ward']['uri'].match(/.+\/(.+)$/)[1]
  50. )
  51. else
  52. # invalid postcode
  53. nil
  54. end
  55. end
  56. end
  57. class Candidate
  58. include DataMapper::Resource
  59. property :id, Serial
  60. property :forenames, String, :required => true
  61. property :surname, String, :required => true, :index => true
  62. property :sex, String
  63. has n, :candidacies
  64. def short_name
  65. @forenames.split(' ')[0] + ' ' + @surname
  66. end
  67. def name
  68. @forenames + ' ' + @surname
  69. end
  70. end
  71. class Candidacy
  72. include DataMapper::Resource
  73. property :id, Serial
  74. property :election_id, Integer, :required => true
  75. property :candidate_id, Integer, :required => true
  76. property :party_id, Integer
  77. property :district_id, Integer, :required => true
  78. property :votes, Integer
  79. property :address, String, :length => 200
  80. property :postcode, String
  81. property :position, Integer # Position of this candidate in this district. (1..n)
  82. property :seats, Integer # Number of seats won by this candidacy (0 or 1)
  83. property :labcoop, Boolean, :default => false # Candidacy is for joint Labour/Co-op party
  84. belongs_to :election
  85. belongs_to :candidate
  86. belongs_to :party
  87. belongs_to :district
  88. end
  89. class Election
  90. include DataMapper::Resource
  91. property :id, Serial
  92. property :body_id, Integer, :required => true
  93. property :d, Date, :required => true, :index => true
  94. property :reason, String, :length => 255
  95. property :kind, String, :length => 255
  96. has n, :candidacies
  97. has n, :polls
  98. belongs_to :body
  99. def self.past
  100. self.all(:d.lt => Time.now.to_s, :order => [ :d.desc ])
  101. end
  102. def self.future
  103. self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ])
  104. end
  105. end
  106. class District
  107. include DataMapper::Resource
  108. property :id, Serial
  109. property :body_id, Integer, :required => true
  110. property :name, String, :length => 255, :required => true
  111. property :slug, String
  112. property :seats, Integer
  113. property :ons_district_code, String
  114. belongs_to :body
  115. has n, :postcodes, :child_key => [:ward_id]
  116. has n, :polls
  117. def self.slugify(name)
  118. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  119. end
  120. end
  121. class Body
  122. include DataMapper::Resource
  123. property :id, Serial
  124. property :name, String, :length => 255, :required => true
  125. property :district_name, String, :length => 255, :required => true # singular
  126. property :districts_name, String, :length => 255, :required => true # plural
  127. property :slug, String, :length => 255
  128. has n, :elections
  129. has n, :districts
  130. end
  131. class Party
  132. include DataMapper::Resource
  133. property :id, Serial
  134. property :name, String, :required => true
  135. property :colour, String
  136. has n, :candidacies
  137. end
  138. # These models are now redundant
  139. class Ward
  140. include DataMapper::Resource
  141. property :id, Serial
  142. property :slug, String, :required => true
  143. property :ons_id, String, :required => true
  144. property :name, String, :required => true
  145. property :constituency_id, Integer, :required => true
  146. has n, :councilcandidates, :order => ['surname']
  147. belongs_to :constituency
  148. def self.slugify(name)
  149. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  150. end
  151. end
  152. class Councilcandidate
  153. include DataMapper::Resource
  154. property :id, Serial
  155. property :ward_id, Integer, :required => true
  156. property :party_id, Integer, :required => true
  157. property :forenames, String, :required => true
  158. property :surname, String, :required => true
  159. property :address, String, :length => 200
  160. property :postcode, String, :required => true
  161. property :votes_2010, Integer
  162. belongs_to :party
  163. belongs_to :ward
  164. end
  165. class Parliamentcandidate
  166. include DataMapper::Resource
  167. property :id, Serial
  168. property :constituency_id, Integer, :required => true
  169. property :party_id, Integer, :required => true
  170. property :forenames, String, :required => true
  171. property :surname, String, :required => true
  172. property :address, String, :length => 200
  173. property :postcode, String
  174. property :votes_2010, Integer
  175. property :votes_2005, Integer
  176. property :percent_2005, Float
  177. belongs_to :party
  178. belongs_to :constituency
  179. end
  180. class Constituency
  181. include DataMapper::Resource
  182. property :id, Serial
  183. property :name, String, :required => true
  184. has n, :wards, :order => ['name']
  185. has n, :parliamentcandidates, :order => ['surname']
  186. end
  187. DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres@localhost:5432/suttonelections")
  188. DataMapper.auto_upgrade!