diff options
-rw-r--r-- | app/controllers/admin_public_body_controller.rb | 29 | ||||
-rw-r--r-- | app/models/public_body.rb | 8 | ||||
-rw-r--r-- | app/views/admin_public_body/import_csv.rhtml | 39 | ||||
-rw-r--r-- | app/views/admin_public_body/list.rhtml | 1 |
4 files changed, 73 insertions, 4 deletions
diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb index dbaba324a..4c7b63f24 100644 --- a/app/controllers/admin_public_body_controller.rb +++ b/app/controllers/admin_public_body_controller.rb @@ -4,7 +4,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: admin_public_body_controller.rb,v 1.11 2008-04-11 15:53:57 francis Exp $ +# $Id: admin_public_body_controller.rb,v 1.12 2008-04-14 12:09:38 francis Exp $ class AdminPublicBodyController < ApplicationController layout "admin" @@ -66,6 +66,33 @@ class AdminPublicBodyController < ApplicationController redirect_to admin_url('body/list') end + def import_csv + if params[:csv_file] + # Try with dry run first + csv_contents = params[:csv_file].read + en = PublicBody.import_csv(csv_contents, params[:tag], true) + errors = en[0] + notes = en[1] + + if errors.size == 0 + # And if OK, with real run + en = PublicBody.import_csv(csv_contents, params[:tag], false) + errors = en[0] + notes = en[1] + if errors.size != 0 + raise "dry run mismatched real run" + end + notes.push("Import was successful.") + end + @errors = errors.join("\n") + @notes = notes.join("\n") + else + @errors = "" + @notes = "" + end + + end + private end diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 54f27caed..c7d9b65cf 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -21,7 +21,7 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: public_body.rb,v 1.56 2008-04-11 15:53:57 francis Exp $ +# $Id: public_body.rb,v 1.57 2008-04-14 12:09:38 francis Exp $ require 'csv' require 'set' @@ -149,7 +149,9 @@ class PublicBody < ActiveRecord::Base class ImportCSVDryRun < StandardError end - # Import from CSV + # Import from CSV. Just tests things and returns messages if dry_run is true. + # Returns an array of [array of errors, array of notes]. If there are errors, + # always rolls back (as with dry_run). def self.import_csv(csv, tag, dry_run = false) errors = [] notes = [] @@ -222,7 +224,7 @@ class PublicBody < ActiveRecord::Base # Ignore end - return errors.join("\n") + notes.join("\n") + return [errors, notes] end end diff --git a/app/views/admin_public_body/import_csv.rhtml b/app/views/admin_public_body/import_csv.rhtml new file mode 100644 index 000000000..9c0ead145 --- /dev/null +++ b/app/views/admin_public_body/import_csv.rhtml @@ -0,0 +1,39 @@ +<% @title = "Upload CSV of public authorities" %> + +<h1><%=@title%></h1> + +<% if not @notes.empty? %> +<pre id="notice"><%=@notes %></pre> +<% end %> +<% if not @errors.empty? %> +<pre id="error"><%=@errors %></pre> +<% end %> + + +<% form_tag 'import_csv', :multipart => true do %> + <p> + <label for="tag">Tag to add entries to (maybe you want: + <% for category, description in PublicBody.categories_by_tag %> + <% if category != "other" %> + <strong><%= category %></strong>=<%= description %>; + <% end %> + <% end %> + ) + </label> + <br/> + <%= text_field_tag 'tag', params[:tag] %> + </p> + + <p> + <label for="csv_file">CSV file (no header rows; unused first column; name second column; email third column):</label> + <br/> + <%= file_field_tag :csv_file, :size => 60 %> + </p> + + <p><strong>Note:</strong> This will report errors in the CSV file. If there are no errors, it will make updates. + Any changes since last import will be overwritten - e.g. email addresses changed back. Changes and additions + are reported on a successful import for you to check. + + <p><%= submit_tag 'Upload' %></p> +<% end %> + diff --git a/app/views/admin_public_body/list.rhtml b/app/views/admin_public_body/list.rhtml index 0210725e0..a6b2dd65c 100644 --- a/app/views/admin_public_body/list.rhtml +++ b/app/views/admin_public_body/list.rhtml @@ -4,6 +4,7 @@ <p> <%= link_to 'New public authority', 'new' %> + | <%= link_to 'Import from CSV file', 'import_csv' %> </p> <% form_tag("", :method => "get") do %> |