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.
 
 
 
 

264 rivejä
8.2 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 DeletedCandidate
  72. include DataMapper::Resource
  73. property :old_candidate_id, Integer, :key => true # ID of candidate that has been merged/deleted
  74. property :candidate_id, Integer, :required => true # ID of candidate that has been kept
  75. end
  76. class Candidacy
  77. include DataMapper::Resource
  78. property :id, Serial
  79. property :election_id, Integer, :required => true
  80. property :candidate_id, Integer, :required => true
  81. property :party_id, Integer
  82. property :district_id, Integer, :required => true
  83. property :votes, Integer
  84. property :address, String, :length => 200
  85. property :postcode, String
  86. property :position, Integer # Position of this candidate in this district. (1..n)
  87. property :seats, Integer # Number of seats won by this candidacy (0 or 1)
  88. property :labcoop, Boolean, :default => false # Candidacy is for joint Labour/Co-op party
  89. belongs_to :election
  90. belongs_to :candidate
  91. belongs_to :party
  92. belongs_to :district
  93. end
  94. class Campaign
  95. include DataMapper::Resource
  96. property :party_id, Integer, :key => true
  97. property :election_id, Integer, :key => true
  98. property :party_url, String, :length => 255
  99. property :manifesto_html_url, String, :length => 255
  100. property :manifesto_pdf_url, String, :length => 255
  101. belongs_to :party
  102. belongs_to :election
  103. end
  104. class Election
  105. include DataMapper::Resource
  106. property :id, Serial
  107. property :body_id, Integer, :required => true
  108. property :d, Date, :required => true, :index => true
  109. property :reason, String, :length => 255
  110. property :kind, String, :length => 255
  111. has n, :candidacies
  112. has n, :polls
  113. belongs_to :body
  114. has n, :campaigns
  115. def self.past
  116. self.all(:d.lt => Time.now.to_s, :order => [ :d.desc ])
  117. end
  118. def self.future
  119. self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ])
  120. end
  121. end
  122. class District
  123. include DataMapper::Resource
  124. property :id, Serial
  125. property :body_id, Integer, :required => true
  126. property :name, String, :length => 255, :required => true
  127. property :slug, String
  128. property :seats, Integer
  129. property :ons_district_code, String
  130. belongs_to :body
  131. has n, :postcodes, :child_key => [:ward_id]
  132. has n, :polls
  133. def self.slugify(name)
  134. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  135. end
  136. end
  137. class Body
  138. include DataMapper::Resource
  139. property :id, Serial
  140. property :name, String, :length => 255, :required => true
  141. property :district_name, String, :length => 255, :required => true # singular
  142. property :districts_name, String, :length => 255, :required => true # plural
  143. property :slug, String, :length => 255
  144. has n, :elections
  145. has n, :districts
  146. end
  147. class Party
  148. include DataMapper::Resource
  149. property :id, Serial
  150. property :name, String, :required => true
  151. property :colour, String
  152. has n, :candidacies
  153. has n, :campaigns
  154. end
  155. # These models are now redundant
  156. class Ward
  157. include DataMapper::Resource
  158. property :id, Serial
  159. property :slug, String, :required => true
  160. property :ons_id, String, :required => true
  161. property :name, String, :required => true
  162. property :constituency_id, Integer, :required => true
  163. has n, :councilcandidates, :order => ['surname']
  164. belongs_to :constituency
  165. def self.slugify(name)
  166. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  167. end
  168. end
  169. class Councilcandidate
  170. include DataMapper::Resource
  171. property :id, Serial
  172. property :ward_id, Integer, :required => true
  173. property :party_id, Integer, :required => true
  174. property :forenames, String, :required => true
  175. property :surname, String, :required => true
  176. property :address, String, :length => 200
  177. property :postcode, String, :required => true
  178. property :votes_2010, Integer
  179. belongs_to :party
  180. belongs_to :ward
  181. end
  182. class Parliamentcandidate
  183. include DataMapper::Resource
  184. property :id, Serial
  185. property :constituency_id, Integer, :required => true
  186. property :party_id, Integer, :required => true
  187. property :forenames, String, :required => true
  188. property :surname, String, :required => true
  189. property :address, String, :length => 200
  190. property :postcode, String
  191. property :votes_2010, Integer
  192. property :votes_2005, Integer
  193. property :percent_2005, Float
  194. belongs_to :party
  195. belongs_to :constituency
  196. end
  197. class Constituency
  198. include DataMapper::Resource
  199. property :id, Serial
  200. property :name, String, :required => true
  201. has n, :wards, :order => ['name']
  202. has n, :parliamentcandidates, :order => ['surname']
  203. end
  204. DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres@localhost:5432/suttonelections")
  205. DataMapper.auto_upgrade!