diff options
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | Gemfile.lock | 6 | ||||
-rw-r--r-- | app/controllers/public_body_controller.rb | 28 | ||||
-rw-r--r-- | app/models/public_body.rb | 8 | ||||
m--------- | commonlib | 0 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 18 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 58 | ||||
-rw-r--r-- | spec/spec_helper.rb | 8 |
8 files changed, 86 insertions, 42 deletions
@@ -46,7 +46,7 @@ gem 'fast_gettext' gem 'gettext_i18n_rails' gem 'gettext' -gem 'globalize3', :git => 'git://github.com/mysociety/globalize.git', :branch => 'henare' +gem 'globalize3', :git => 'git://github.com/globalize/globalize.git', :ref => '5fd95f2389dff1' gem 'locale' gem 'routing-filter' gem 'unicode' diff --git a/Gemfile.lock b/Gemfile.lock index 385056255..989920a72 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT - remote: git://github.com/mysociety/globalize.git - revision: 2931f559cbed8843ab7d16278d70ce99a0239132 - branch: henare + remote: git://github.com/globalize/globalize.git + revision: 5fd95f2389dff13c9368fb2e08c96c8a48798c72 + ref: 5fd95f2389dff1 specs: globalize3 (0.3.0) activemodel (>= 3.0.0) diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 9c3e46ded..308d38e4c 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -7,6 +7,7 @@ require 'fastercsv' require 'confidence_intervals' +require 'tempfile' class PublicBodyController < ApplicationController # XXX tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL @@ -191,9 +192,32 @@ class PublicBodyController < ApplicationController end def list_all_csv - send_data(PublicBody.export_csv, :type=> 'text/csv; charset=utf-8; header=present', + # FIXME: this is just using the download directory for zip + # archives, since we know that is allowed for X-Sendfile and + # the filename can't clash with the numeric subdirectory names + # used for the zips. However, really there should be a + # generically named downloads directory that contains all + # kinds of downloadable assets. + download_directory = File.join(InfoRequest.download_zip_dir(), + 'download') + FileUtils.mkdir_p download_directory + output_leafname = 'all-authorities.csv' + output_filename = File.join download_directory, output_leafname + # Create a temporary file in the same directory, so we can + # rename it atomically to the intended filename: + tmp = Tempfile.new output_leafname, download_directory + tmp.close + # Export all the public bodies to that temporary path and make + # it readable: + PublicBody.export_csv tmp.path + FileUtils.chmod 0644, tmp.path + # Rename into place and send the file: + File.rename tmp.path, output_filename + send_file(output_filename, + :type => 'text/csv; charset=utf-8; header=present', :filename => 'all-authorities.csv', - :disposition =>'attachment', :encoding => 'utf8') + :disposition =>'attachment', + :encoding => 'utf8') end diff --git a/app/models/public_body.rb b/app/models/public_body.rb index fbe2956e3..db6359f6b 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -514,10 +514,8 @@ class PublicBody < ActiveRecord::Base end # Returns all public bodies (except for the internal admin authority) as csv - def self.export_csv - public_bodies = PublicBody.visible.find(:all, :order => 'url_name', - :include => [:translations, :tags]) - FasterCSV.generate() do |csv| + def self.export_csv(output_filename) + CSV.open(output_filename, "w") do |csv| csv << [ 'Name', 'Short name', @@ -532,7 +530,7 @@ class PublicBody < ActiveRecord::Base 'Updated at', 'Version', ] - public_bodies.each do |public_body| + PublicBody.visible.find_each(:include => [:translations, :tags]) do |public_body| # Skip bodies we use only for site admin next if public_body.has_tag?('site_administration') csv << [ diff --git a/commonlib b/commonlib -Subproject 77a6b09daa5da3808be4431799521f8bee5ab21 +Subproject 8070e4c27c903d886963d662db40bb91d56f8c5 diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 593d51683..116dbe07a 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -73,11 +73,10 @@ describe GeneralController, "when showing the frontpage" do end it "should render the front page with default language" do - old_default_locale = I18n.default_locale - I18n.default_locale = "es" - get :frontpage - response.should have_selector('html[lang="es"]') - I18n.default_locale = old_default_locale + with_default_locale("es") do + get :frontpage + response.should have_selector('html[lang="es"]') + end end it "should render the front page with default language and ignore the browser setting" do @@ -85,11 +84,10 @@ describe GeneralController, "when showing the frontpage" do config['USE_DEFAULT_BROWSER_LANGUAGE'] = false accept_language = "en-GB,en-US;q=0.8,en;q=0.6" request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language - old_default_locale = I18n.default_locale - I18n.default_locale = "es" - get :frontpage - response.should have_selector('html[lang="es"]') - I18n.default_locale = old_default_locale + with_default_locale("es") do + get :frontpage + response.should have_selector('html[lang="es"]') + end end it "should render the front page with browser-selected language when there's no default set" do diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 6800765f2..63989baaa 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -82,21 +82,23 @@ describe PublicBodyController, "when listing bodies" do def make_single_language_example(locale) result = nil - I18n.with_locale(locale) do - case locale - when :en - result = PublicBody.new(:name => 'English only', - :short_name => 'EO') - when :es - result = PublicBody.new(:name => 'Español Solamente', - :short_name => 'ES') - else - raise StandardError.new "Unknown locale #{locale}" + with_default_locale(locale) do + I18n.with_locale(locale) do + case locale + when :en + result = PublicBody.new(:name => 'English only', + :short_name => 'EO') + when :es + result = PublicBody.new(:name => 'Español Solamente', + :short_name => 'ES') + else + raise StandardError.new "Unknown locale #{locale}" + end + result.request_email = "#{locale}@example.org" + result.last_edit_editor = 'test' + result.last_edit_comment = '' + result.save end - result.request_email = "#{locale}@example.org" - result.last_edit_editor = 'test' - result.last_edit_comment = '' - result.save end result end @@ -188,13 +190,13 @@ describe PublicBodyController, "when listing bodies" do end it "should list bodies in alphabetical order with different locale" do - I18n.default_locale = :es - get :list - response.should render_template('list') - assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body), public_bodies(:humpadink_public_body) ] - assigns[:tag].should == "all" - assigns[:description].should == "" - I18n.default_locale = :en + with_default_locale(:es) do + get :list + response.should render_template('list') + assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body), public_bodies(:humpadink_public_body) ] + assigns[:tag].should == "all" + assigns[:description].should == "" + end end it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do @@ -273,6 +275,20 @@ describe PublicBodyController, "when showing JSON version for API" do end +describe PublicBodyController, "when asked to export public bodies as CSV" do + + it "should return a valid CSV file with the right number of rows" do + get :list_all_csv + all_data = CSV.parse response.body + all_data.length.should == 8 + # Check that the header has the right number of columns: + all_data[0].length.should == 11 + # And an actual line of data: + all_data[1].length.should == 11 + end + +end + describe PublicBodyController, "when showing public body statistics" do it "should render the right template with the right data" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9d16f6387..d22f3c0ff 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -183,6 +183,14 @@ Spork.prefork do end end + def with_default_locale(locale) + original_default_locale = I18n.default_locale + I18n.default_locale = locale + yield + ensure + I18n.default_locale = original_default_locale + end + def load_test_categories PublicBodyCategories.add(:en, [ "Local and regional", |