aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/general_controller.rb45
-rw-r--r--app/controllers/public_body_controller.rb28
-rw-r--r--app/controllers/request_controller.rb16
-rw-r--r--app/helpers/application_helper.rb7
-rw-r--r--app/models/info_request.rb63
-rw-r--r--app/models/public_body.rb32
-rw-r--r--app/views/contact_mailer/from_admin_message.text.erb1
-rw-r--r--app/views/general/_frontpage_bodies_list.html.erb5
-rw-r--r--app/views/general/_frontpage_requests_list.html.erb1
-rw-r--r--app/views/general/frontpage.html.erb3
-rw-r--r--app/views/request/_sidebar.html.erb10
11 files changed, 142 insertions, 69 deletions
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb
index beefef4e6..aac078829 100644
--- a/app/controllers/general_controller.rb
+++ b/app/controllers/general_controller.rb
@@ -12,52 +12,7 @@ class GeneralController < ApplicationController
# New, improved front page!
def frontpage
medium_cache
- # get some example searches and public bodies to display
- # either from config, or based on a (slow!) query if not set
- body_short_names = AlaveteliConfiguration::frontpage_publicbody_examples.split(/\s*;\s*/).map{|s| "'%s'" % s.gsub(/'/, "''") }.join(", ")
@locale = self.locale_from_params()
- locale_condition = 'public_body_translations.locale = ?'
- conditions = [locale_condition, @locale]
- I18n.with_locale(@locale) do
- if body_short_names.empty?
- # This is too slow
- @popular_bodies = PublicBody.visible.find(:all,
- :order => "info_requests_count desc",
- :limit => 32,
- :conditions => conditions,
- :joins => :translations
- )
- else
- conditions[0] += " and public_bodies.url_name in (" + body_short_names + ")"
- @popular_bodies = PublicBody.find(:all,
- :conditions => conditions,
- :joins => :translations)
- end
- end
- # Get some successful requests
- begin
- query = 'variety:response (status:successful OR status:partially_successful)'
- sortby = "newest"
- max_count = 5
- xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_title_collapse', max_count)
- @request_events = xapian_object.results.map { |r| r[:model] }
-
- # If there are not yet enough successful requests, fill out the list with
- # other requests
- if @request_events.count < max_count
- @request_events_all_successful = false
- query = 'variety:sent'
- xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_title_collapse', max_count-@request_events.count)
- more_events = xapian_object.results.map { |r| r[:model] }
- @request_events += more_events
- # Overall we still want the list sorted with the newest first
- @request_events.sort!{|e1,e2| e2.created_at <=> e1.created_at}
- else
- @request_events_all_successful = true
- end
- rescue
- @request_events = []
- end
end
# Display blog entries
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/controllers/request_controller.rb b/app/controllers/request_controller.rb
index 388473b51..841950cd5 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -92,15 +92,8 @@ class RequestController < ApplicationController
# Sidebar stuff
@sidebar = true
- # ... requests that have similar imporant terms
- begin
- limit = 10
- @xapian_similar = ActsAsXapian::Similar.new([InfoRequestEvent], @info_request.info_request_events,
- :limit => limit, :collapse_by_prefix => 'request_collapse')
- @xapian_similar_more = (@xapian_similar.matches_estimated > limit)
- rescue
- @xapian_similar = nil
- end
+ @similar_cache_key = cache_key_for_similar_requests(@info_request, @locale)
+
# Track corresponding to this page
@track_thing = TrackThing.create_track_for_request(@info_request)
@feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ]
@@ -971,5 +964,10 @@ class RequestController < ApplicationController
file_info
end
+ def cache_key_for_similar_requests(info_request, locale)
+ "request/similar/#{info_request.id}/#{locale}"
+ end
+
+
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index e3b1e57ac..0c346ab4e 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -116,5 +116,12 @@ module ApplicationHelper
return !session[:using_admin].nil? || (!@user.nil? && @user.super?)
end
+ def cache_if_caching_fragments(*args, &block)
+ if AlaveteliConfiguration::cache_fragments
+ cache(*args) { yield }
+ else
+ yield
+ end
+ end
end
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 9463a236e..0a073dc79 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -1212,6 +1212,69 @@ public
end
end
+
+ # Get requests that have similar important terms
+ def similar_requests(limit=10)
+ xapian_similar = nil
+ xapian_similar_more = false
+ begin
+ xapian_similar = ActsAsXapian::Similar.new([InfoRequestEvent],
+ info_request_events,
+ :limit => limit,
+ :collapse_by_prefix => 'request_collapse')
+ xapian_similar_more = (xapian_similar.matches_estimated > limit)
+ rescue
+ end
+ return [xapian_similar, xapian_similar_more]
+ end
+
+ def InfoRequest.recent_requests
+ request_events = []
+ request_events_all_successful = false
+ # Get some successful requests
+ begin
+ query = 'variety:response (status:successful OR status:partially_successful)'
+ sortby = "newest"
+ max_count = 5
+
+ xapian_object = ActsAsXapian::Search.new([InfoRequestEvent],
+ query,
+ :offset => 0,
+ :limit => 5,
+ :sort_by_prefix => 'created_at',
+ :sort_by_ascending => true,
+ :collapse_by_prefix => 'request_title_collapse'
+ )
+ xapian_object.results
+ request_events = xapian_object.results.map { |r| r[:model] }
+
+ # If there are not yet enough successful requests, fill out the list with
+ # other requests
+ if request_events.count < max_count
+ query = 'variety:sent'
+ xapian_object = ActsAsXapian::Search.new([InfoRequestEvent],
+ query,
+ :offset => 0,
+ :limit => max_count-request_events.count,
+ :sort_by_prefix => 'created_at',
+ :sort_by_ascending => true,
+ :collapse_by_prefix => 'request_title_collapse'
+ )
+ xapian_object.results
+ more_events = xapian_object.results.map { |r| r[:model] }
+ request_events += more_events
+ # Overall we still want the list sorted with the newest first
+ request_events.sort!{|e1,e2| e2.created_at <=> e1.created_at}
+ else
+ request_events_all_successful = true
+ end
+ rescue
+ request_events = []
+ end
+
+ return [request_events, request_events_all_successful]
+ end
+
private
def set_defaults
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index fbe2956e3..d2cfc4b8b 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 << [
@@ -725,6 +723,30 @@ class PublicBody < ActiveRecord::Base
'y_max' => 100,
'totals' => original_totals}
end
+ def self.popular_bodies(locale)
+ # get some example searches and public bodies to display
+ # either from config, or based on a (slow!) query if not set
+ body_short_names = AlaveteliConfiguration::frontpage_publicbody_examples.split(/\s*;\s*/)
+ locale_condition = 'public_body_translations.locale = ?'
+ conditions = [locale_condition, locale]
+ bodies = []
+ I18n.with_locale(locale) do
+ if body_short_names.empty?
+ # This is too slow
+ bodies = visible.find(:all,
+ :order => "info_requests_count desc",
+ :limit => 32,
+ :conditions => conditions,
+ :joins => :translations
+ )
+ else
+ conditions[0] += " and public_bodies.url_name in (?)"
+ conditions << body_short_names
+ bodies = find(:all, :conditions => conditions, :joins => :translations)
+ end
+ end
+ return bodies
+ end
private
diff --git a/app/views/contact_mailer/from_admin_message.text.erb b/app/views/contact_mailer/from_admin_message.text.erb
index 4169d8d3a..3af759c5d 100644
--- a/app/views/contact_mailer/from_admin_message.text.erb
+++ b/app/views/contact_mailer/from_admin_message.text.erb
@@ -1,2 +1 @@
<%= raw @message %>
-
diff --git a/app/views/general/_frontpage_bodies_list.html.erb b/app/views/general/_frontpage_bodies_list.html.erb
index 75daea41d..44321f14a 100644
--- a/app/views/general/_frontpage_bodies_list.html.erb
+++ b/app/views/general/_frontpage_bodies_list.html.erb
@@ -1,10 +1,11 @@
-<% if @popular_bodies.size > 0 %>
+<%- popular_bodies = PublicBody.popular_bodies(@locale) %>
+<% if popular_bodies.size > 0 %>
<div id="examples_0">
<h3><%= _("Who can I request information from?") %></h3>
<%= _("{{site_name}} covers requests to {{number_of_authorities}} authorities, including:",
:site_name => site_name, :number_of_authorities => PublicBody.visible.count) %>
<ul>
- <% for popular_body in @popular_bodies %>
+ <% for popular_body in popular_bodies %>
<li><%=public_body_link(popular_body)%>
<%= n_('{{count}} request', '{{count}} requests', popular_body.info_requests_count, :count => popular_body.info_requests_count) %>
</li>
diff --git a/app/views/general/_frontpage_requests_list.html.erb b/app/views/general/_frontpage_requests_list.html.erb
index fa498dfa7..41a875cab 100644
--- a/app/views/general/_frontpage_requests_list.html.erb
+++ b/app/views/general/_frontpage_requests_list.html.erb
@@ -1,3 +1,4 @@
+<%- @request_events, @request_events_all_successful = InfoRequest.recent_requests %>
<div id="examples_1">
<h3>
<% if @request_events_all_successful %>
diff --git a/app/views/general/frontpage.html.erb b/app/views/general/frontpage.html.erb
index bf5261d15..8bb32bdf2 100644
--- a/app/views/general/frontpage.html.erb
+++ b/app/views/general/frontpage.html.erb
@@ -1,4 +1,4 @@
-<% # TODO: Cache for 5 minutes %>
+<% cache_if_caching_fragments("frontpage-#{@locale}", :expires_in => 5.minutes) do %>
<div id="frontpage_splash">
<div id="left_column">
<%= render :partial => "frontpage_new_request" %>
@@ -17,3 +17,4 @@
<%= render :partial => "frontpage_bodies_list" %>
<%= render :partial => "frontpage_requests_list" %>
</div>
+<% end %>
diff --git a/app/views/request/_sidebar.html.erb b/app/views/request/_sidebar.html.erb
index 8d4a4a2d8..8400cd6ac 100644
--- a/app/views/request/_sidebar.html.erb
+++ b/app/views/request/_sidebar.html.erb
@@ -51,16 +51,18 @@
<%= render :partial => 'request/next_actions' %>
- <% # TODO: Cache for 1 day %>
- <% if !@xapian_similar.nil? && @xapian_similar.results.size > 0 %>
+ <% cache_if_caching_fragments(@similar_cache_key, :expires_in => 1.day) do %>
+ <% xapian_similar, xapian_similar_more = @info_request.similar_requests %>
+ <% if !xapian_similar.nil? && xapian_similar.results.size > 0 %>
<h2><%= _('Similar requests')%></h2>
- <% for result in @xapian_similar.results %>
+ <% for result in xapian_similar.results %>
<%= render :partial => 'request/request_listing_short_via_event', :locals => { :event => result[:model], :info_request => result[:model].info_request } %>
<% end %>
- <% if @xapian_similar_more %>
+ <% if xapian_similar_more %>
<p><%= link_to _("More similar requests"), similar_request_path(@info_request.url_title) %></p>
<% end %>
<% end %>
+ <% end %>
<p><%= link_to _('Event history details'), request_details_path(@info_request) %></p>