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.
 
 
 
 

227 lines
6.7 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. end
  13. class Postcode
  14. include DataMapper::Resource
  15. # Postcode natural key, uppercase with space, eg. "SM1 1EA"
  16. property :postcode, String, :key => true
  17. property :created_at, DateTime
  18. property :updated_at, DateTime
  19. property :lat, Float, :required => true
  20. property :lng, Float, :required => true
  21. property :district_name, String, :required => true
  22. property :district_code, String, :required => true
  23. property :ward_name, String, :required => true
  24. property :ward_code, String, :required => true
  25. def self.finder(postcode)
  26. postcode = postcode.strip.upcase
  27. if o = self.get(postcode)
  28. return o
  29. end
  30. result = Pat.get(postcode)
  31. unless result.code == 404
  32. # cache API result
  33. self.create(
  34. :postcode => postcode,
  35. :lat => result['geo']['lat'],
  36. :lng => result['geo']['lng'],
  37. :district_name => result['administrative']['district']['title'],
  38. :district_code => result['administrative']['district']['uri'].match(/.+\/(.+)$/)[1],
  39. :ward_name => result['administrative']['ward']['title'],
  40. :ward_code => result['administrative']['ward']['uri'].match(/.+\/(.+)$/)[1]
  41. )
  42. else
  43. # invalid postcode
  44. nil
  45. end
  46. end
  47. end
  48. class Candidate
  49. include DataMapper::Resource
  50. property :id, Serial
  51. property :forenames, String, :required => true
  52. property :surname, String, :required => true, :index => true
  53. property :sex, String
  54. has n, :candidacies
  55. def short_name
  56. @forenames.split(' ')[0] + ' ' + @surname
  57. end
  58. def name
  59. @forenames + ' ' + @surname
  60. end
  61. end
  62. class Candidacy
  63. include DataMapper::Resource
  64. property :id, Serial
  65. property :election_id, Integer, :required => true
  66. property :candidate_id, Integer, :required => true
  67. property :party_id, Integer
  68. property :district_id, Integer, :required => true
  69. property :votes, Integer
  70. property :address, String, :length => 200
  71. property :postcode, String
  72. property :position, Integer # Position of this candidate in this district. (1..n)
  73. property :seats, Integer # Number of seats won by this candidacy (0 or 1)
  74. property :labcoop, Boolean # Candidacy is for joint Labour/Co-op party
  75. belongs_to :election
  76. belongs_to :candidate
  77. belongs_to :party
  78. belongs_to :district
  79. end
  80. class Election
  81. include DataMapper::Resource
  82. property :id, Serial
  83. property :body_id, Integer, :required => true
  84. property :d, Date, :required => true, :index => true
  85. property :reason, String, :length => 255
  86. property :kind, String, :length => 255
  87. has n, :candidacies
  88. belongs_to :body
  89. def self.past
  90. self.all(:d.lt => Time.now.to_s, :order => [ :d.desc ])
  91. end
  92. def self.future
  93. self.all(:d.gte => Time.now.to_s, :order => [ :d.desc ])
  94. end
  95. end
  96. class District
  97. include DataMapper::Resource
  98. property :id, Serial
  99. property :body_id, Integer, :required => true
  100. property :name, String, :length => 255, :required => true
  101. property :slug, String
  102. property :seats, Integer
  103. belongs_to :body
  104. def self.slugify(name)
  105. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  106. end
  107. end
  108. class Body
  109. include DataMapper::Resource
  110. property :id, Serial
  111. property :name, String, :length => 255, :required => true
  112. property :district_name, String, :length => 255, :required => true # singular
  113. property :districts_name, String, :length => 255, :required => true # plural
  114. property :slug, String, :length => 255
  115. has n, :elections
  116. has n, :districts
  117. end
  118. class Party
  119. include DataMapper::Resource
  120. property :id, Serial
  121. property :name, String, :required => true
  122. property :colour, String
  123. has n, :candidacies
  124. end
  125. # These models are now redundant
  126. class Ward
  127. include DataMapper::Resource
  128. property :id, Serial
  129. property :slug, String, :required => true
  130. property :ons_id, String, :required => true
  131. property :name, String, :required => true
  132. property :constituency_id, Integer, :required => true
  133. has n, :councilcandidates, :order => ['surname']
  134. belongs_to :constituency
  135. def self.slugify(name)
  136. name.gsub(/[^\w\s-]/, '').gsub(/\s+/, '-').downcase
  137. end
  138. end
  139. class Councilcandidate
  140. include DataMapper::Resource
  141. property :id, Serial
  142. property :ward_id, Integer, :required => true
  143. property :party_id, Integer, :required => true
  144. property :forenames, String, :required => true
  145. property :surname, String, :required => true
  146. property :address, String, :length => 200
  147. property :postcode, String, :required => true
  148. property :votes_2010, Integer
  149. belongs_to :party
  150. belongs_to :ward
  151. end
  152. class Parliamentcandidate
  153. include DataMapper::Resource
  154. property :id, Serial
  155. property :constituency_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
  161. property :votes_2010, Integer
  162. property :votes_2005, Integer
  163. property :percent_2005, Float
  164. belongs_to :party
  165. belongs_to :constituency
  166. end
  167. class Constituency
  168. include DataMapper::Resource
  169. property :id, Serial
  170. property :name, String, :required => true
  171. has n, :wards, :order => ['name']
  172. has n, :parliamentcandidates, :order => ['surname']
  173. end
  174. DataMapper.setup(:default, ENV['DATABASE_URL'] || "postgres://postgres@localhost:5432/suttonelections")
  175. DataMapper.auto_upgrade!