An open source, stand-alone, customisable public spending data web app.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

import.rb 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. require 'lib/models'
  2. require 'csv'
  3. # Before running this script with a CSV file, prepare it so:
  4. # - There is only a single line of column headings on the first line of the file
  5. # - There are no spaces before or after the column headings
  6. # - The column headings correspond with the key names in the columns{} hash below
  7. # - The data starts on line 2
  8. columns =
  9. {
  10. 'Directorate' => nil,
  11. 'Updated' => nil,
  12. 'Service' => nil,
  13. 'Supplier' => nil,
  14. 'Amount' => nil
  15. }
  16. directorate_replacements =
  17. [
  18. [ "Childrens Services", "Children's Services" ],
  19. [ "Policy,Performance and Planning", "Policy, Performance and Planning" ]
  20. ]
  21. service_replacements =
  22. [
  23. [ "Corporate Performance and Developmt", "Corporate Performance and Development" ],
  24. [ "ISB", "ISB - Individual Schools Budget" ],
  25. [ "Library and Information Services", "Libraries and Information Services" ],
  26. [ "On Street Parking", "On-Street Parking" ]
  27. ]
  28. count = 0
  29. if ARGV[0].nil?
  30. puts "Specify the filename of the CSV file to import on the command line"
  31. exit
  32. end
  33. date_format = ARGV[1].upcase
  34. if date_format != 'DMY' && date_format != 'MDY'
  35. puts "Specify the date format as DMY or MDY as the second argument on the command line"
  36. exit
  37. end
  38. CSV::Reader.parse(File.open(ARGV[0], 'rb')) do |row|
  39. count += 1
  40. if (count > 1) # skip first line that doesn't contain data
  41. p row
  42. directorate_name = row[columns['Directorate']].strip.gsub(/&/, "and")
  43. service_name = row[columns['Service']].strip.gsub(/&/, "and")
  44. supplier_name = row[columns['Supplier']].strip.gsub(/&/, "and")
  45. for replacement in directorate_replacements
  46. if directorate_name == replacement[0]
  47. directorate_name = replacement[1]
  48. end
  49. end
  50. for replacement in service_replacements
  51. if service_name == replacement[0]
  52. service_name = replacement[1]
  53. end
  54. end
  55. directorate = Directorate.first_or_create(:name => directorate_name)
  56. service = Service.first_or_create(:name => service_name, :directorate => directorate)
  57. supplier = Supplier.first_or_create(:name => supplier_name)
  58. dt = row[columns['Updated']].strip.split('/')
  59. # Date.new takes YMD params
  60. if date_format == 'DMY'
  61. d = Date.new(dt[2].to_i, dt[1].to_i, dt[0].to_i)
  62. elsif date_format == 'MDY'
  63. d = Date.new(dt[2].to_i, dt[0].to_i, dt[1].to_i)
  64. elsif date_format == 'YMD'
  65. d = Date.new(dt[0].to_i, dt[1].to_i, dt[2].to_i)
  66. end
  67. payment = Payment.first_or_new(
  68. 'service' => service,
  69. 'supplier' => supplier,
  70. 'amount' => row[columns['Amount']].strip.gsub(/,/, ''),
  71. 'd' => d
  72. )
  73. unless payment.save
  74. puts "ERROR: Failed to save payment"
  75. payment.errors.each do |e|
  76. puts e
  77. end
  78. end
  79. else
  80. # Get the column headings
  81. position = 0
  82. for column in row
  83. columns[column] = position
  84. position += 1
  85. end
  86. puts columns.inspect
  87. end
  88. end