diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/application_controller.rb | 29 | ||||
-rw-r--r-- | app/controllers/general_controller.rb | 23 | ||||
-rw-r--r-- | app/controllers/public_body_controller.rb | 103 |
3 files changed, 91 insertions, 64 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9382e077f..91754e2ba 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -12,11 +12,28 @@ class ApplicationController < ActionController::Base # Standard headers, footers and navigation for whole site layout "default" - # set locale + include FastGettext::Translation # make functions like _, n_, N_ etc available) before_filter :set_gettext_locale + + # scrub sensitive parameters from the logs + filter_parameter_logging :password + + + def set_gettext_locale + requested_locale = params[:locale] || session[:locale] || cookies[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'] + session[:locale] = FastGettext.set_locale(requested_locale) + end + # scrub sensitive parameters from the logs filter_parameter_logging :password + helper_method :site_name, :locale_from_params + def site_name + # XXX should come from database: + site_name = "WhatDoTheyKnow" + return site_name + end + # Help work out which request causes RAM spike. # http://www.codeweblog.com/rails-to-monitor-the-process-of-memory-leaks-skills/ # This shows the memory use increase of the Ruby process due to the request. @@ -131,6 +148,16 @@ class ApplicationController < ActionController::Base f.write(content) end end + + # get the local locale + def locale_from_params(*args) + if params[:show_locale] + params[:show_locale] + else + I18n.locale.to_s + end + end + private # Check the user is logged in diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index 21e715424..cf28208a0 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -22,22 +22,7 @@ class GeneralController < ApplicationController def frontpage behavior_cache do # This is too slow - #@popular_bodies = PublicBody.find(:all, :select => "*, (select count(*) from info_requests where info_requests.public_body_id = public_bodies.id) as c", :order => "c desc", :limit => 32) - - # Just hardcode some popular authorities for now - # ('tgq', 'atbra' is for debugging on Francis's development environment) - @popular_bodies = PublicBody.find(:all, :conditions => ["url_name in ( - 'bbc', - 'dwp', - 'dh', - 'snh', - 'royal_mail_group', - 'mod', - 'kent_county_council', - 'wirral_borough_council' - /* , 'tgq', 'atbra' */ - )"]).sort_by { |pb| pb.url_name }.reverse # just an order that looks better - + @popular_bodies = PublicBody.find(:all, :select => "*, (select count(*) from info_requests where info_requests.public_body_id = public_bodies.id) as c", :order => "c desc", :limit => 32) # Get some successful requests # begin query = 'variety:response (status:successful OR status:partially_successful)' @@ -168,5 +153,11 @@ class GeneralController < ApplicationController render :text => "awake\n" end + def custom_css + @locale = self.locale_from_params() + render(:layout => false, :content_type => 'text/css') + end + + end diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 17eba911f..cf64046ea 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -16,87 +16,96 @@ class PublicBodyController < ApplicationController return end - @public_body = PublicBody.find_by_url_name_with_historic(params[:url_name]) - raise "None found" if @public_body.nil? # XXX proper 404 + @locale = self.locale_from_params() + PublicBody.with_locale(@locale) do + @public_body = PublicBody.find_by_url_name_with_historic(params[:url_name]) + raise "None found" if @public_body.nil? # XXX proper 404 - # If found by historic name, redirect to new name - redirect_to show_public_body_url(:url_name => @public_body.url_name) if - @public_body.url_name != params[:url_name] + # If found by historic name, redirect to new name + redirect_to show_public_body_url(:url_name => @public_body.url_name) if + @public_body.url_name != params[:url_name] - set_last_body(@public_body) + set_last_body(@public_body) - top_url = main_url("/") - @searched_to_send_request = false - referrer = request.env['HTTP_REFERER'] - if !referrer.nil? && referrer.match(%r{^#{top_url}search/.*/bodies$}) - @searched_to_send_request = true - end + top_url = main_url("/") + @searched_to_send_request = false + referrer = request.env['HTTP_REFERER'] + if !referrer.nil? && referrer.match(%r{^#{top_url}search/.*/bodies$}) + @searched_to_send_request = true + end - # Use search query for this so can collapse and paginate easily - # XXX really should just use SQL query here rather than Xapian. - begin - @xapian_requests = perform_search([InfoRequestEvent], 'requested_from:' + @public_body.url_name, 'newest', 'request_collapse') - if (@page > 1) - @page_desc = " (page " + @page.to_s + ")" - else - @page_desc = "" + # Use search query for this so can collapse and paginate easily + # XXX really should just use SQL query here rather than Xapian. + begin + @xapian_requests = perform_search([InfoRequestEvent], 'requested_from:' + @public_body.url_name, 'newest', 'request_collapse') + if (@page > 1) + @page_desc = " (page " + @page.to_s + ")" + else + @page_desc = "" + end + rescue + @xapian_requests = nil end - rescue - @xapian_requests = nil - end - @track_thing = TrackThing.create_track_for_public_body(@public_body) - @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ] + @track_thing = TrackThing.create_track_for_public_body(@public_body) + @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ] - respond_to do |format| - format.html { @has_json = true } - format.json { render :json => @public_body.json_for_api } + respond_to do |format| + format.html { @has_json = true } + format.json { render :json => @public_body.json_for_api } + end end end def view_email @public_bodies = PublicBody.find(:all, :conditions => [ "url_name = ?", params[:url_name] ]) @public_body = @public_bodies[0] - - if params[:submitted_view_email] - if verify_recaptcha - flash.discard(:error) - render :template => "public_body/view_email" - return + PublicBody.with_locale(self.locale_from_params()) do + if params[:submitted_view_email] + if verify_recaptcha + flash.discard(:error) + render :template => "public_body/view_email" + return + end + flash.now[:error] = "There was an error with the words you entered, please try again." end - flash.now[:error] = "There was an error with the words you entered, please try again." + render :template => "public_body/view_email_captcha" end - render :template => "public_body/view_email_captcha" end def list # XXX move some of these tag SQL queries into has_tag_string.rb @tag = params[:tag] + @locale = self.locale_from_params() + locale_condition = 'public_body_translations.locale = ?' if @tag.nil? @tag = "all" - conditions = [] + conditions = [locale_condition, @locale] elsif @tag == 'other' category_list = PublicBodyCategories::CATEGORIES.map{|c| "'"+c+"'"}.join(",") - conditions = ['(select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id + conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' - and has_tag_string_tags.name in (' + category_list + ')) = 0'] + and has_tag_string_tags.name in (' + category_list + ')) = 0', @locale] elsif @tag.size == 1 @tag.upcase! - conditions = ['first_letter = ?', @tag] + conditions = [locale_condition + ' AND public_body_translations.first_letter = ?', @locale, @tag] elsif @tag.include?(":") name, value = HasTagString::HasTagStringTag.split_tag_into_name_value(@tag) - conditions = ['(select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id + conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' - and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', name, value] + and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', @locale, name, value] else - conditions = ['(select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id + conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id and has_tag_string_tags.model = \'PublicBody\' - and has_tag_string_tags.name = ?) > 0', @tag] + and has_tag_string_tags.name = ?) > 0', @locale, @tag] end - @public_bodies = PublicBody.paginate( - :order => "public_bodies.name", :page => params[:page], :per_page => 1000, # fit all councils on one page - :conditions => conditions + PublicBody.with_locale(@locale) do + @public_bodies = PublicBody.paginate( + :order => "public_body_translations.name", :page => params[:page], :per_page => 1000, # fit all councils on one page + :conditions => conditions, + :joins => :translations ) + end if @tag.size == 1 @description = "beginning with '" + @tag + "'" else |