/
public_body_controller.rb
201 lines (166 loc) · 6.82 KB
/
public_body_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# app/controllers/public_body_controller.rb:
# Show information about a public body.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
require 'tempfile'
class PublicBodyController < ApplicationController
skip_before_action :html_response, only: [:show, :list_all_csv]
skip_before_action :redirect_gettext_locale, only: :show
MAX_RESULTS = 500
# TODO: tidy this up with better error messages, and a more standard infrastructure for the redirect to canonical URL
def show
long_cache
@page = get_search_page_from_params
requests_per_page = 25
# Later pages are very expensive to load
if @page > MAX_RESULTS / requests_per_page
raise ActiveRecord::RecordNotFound, "Sorry. No pages after #{MAX_RESULTS / requests_per_page}."
end
if MySociety::Format.simplify_url_part(params[:url_name], 'body') != params[:url_name]
redirect_to url_name: MySociety::Format.simplify_url_part(params[:url_name], 'body'), status: :moved_permanently
return
end
@locale = AlaveteliLocalization.locale
AlaveteliLocalization.with_locale(@locale) do
@public_body = PublicBody.find_by_url_name_with_historic(params[:url_name])
raise ActiveRecord::RecordNotFound, "None found" if @public_body.nil?
if @public_body.url_name.nil?
redirect_back(fallback_location: root_path)
return
end
# If found by historic name, or alternate locale name, redirect to new name
if @public_body.url_name != params[:url_name]
redirect_to url_name: @public_body.url_name
return
end
set_last_body(@public_body)
@number_of_visible_requests = @public_body.info_requests.is_searchable.count
@searched_to_send_request = false
referrer = request.env['HTTP_REFERER']
if !referrer.nil? && referrer.match(%r{^#{frontpage_url}search/.*/bodies$})
@searched_to_send_request = true
end
@view = params[:view]
query = InfoRequestEvent.make_query_from_params(params.merge(latest_status: @view))
query += " requested_from:#{@public_body.url_name}"
# Use search query for this so can collapse and paginate easily
# TODO: really should just use SQL query here rather than Xapian.
sortby = "described"
begin
@xapian_requests = perform_search([InfoRequestEvent], query, sortby, 'request_collapse', requests_per_page)
if @page > 1
@page_desc = " (page #{ @page })"
else
@page_desc = ""
end
rescue
@xapian_requests = nil
end
flash.keep(:search_params)
@track_thing = TrackThing.create_track_for_public_body(@public_body)
@existing_track = TrackThing.find_existing(@user, @track_thing) if @user
@follower_count = TrackThing.where(public_body_id: @public_body.id).count
@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 do
@has_json = true
render template: "public_body/show"
end
format.json { render json: @public_body.json_for_api }
end
end
end
def view_email
@public_body = PublicBody.find_by_url_name_with_historic(params[:url_name])
raise ActiveRecord::RecordNotFound, "None found" if @public_body.nil?
AlaveteliLocalization.with_locale(AlaveteliLocalization.locale) 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 reCAPTCHA. ' \
'Please try again.')
end
render template: "public_body/view_email_captcha"
end
end
def list
long_cache
@tag = params[:tag] || 'all'
@tag = @tag.upcase if @tag.scan(/./mu).size == 1
@country_code = AlaveteliConfiguration.iso_country_code
@locale = AlaveteliLocalization.locale
AlaveteliLocalization.with_locale(@locale) do
@public_bodies = PublicBody.visible.
with_tag(@tag).
with_query(params[:public_body_query], @tag).
paginate(page: params[:page], per_page: 100)
@description =
if @tag == 'all'
n_('Found {{count}} public authority',
'Found {{count}} public authorities',
@public_bodies.total_entries,
count: @public_bodies.total_entries)
elsif @tag.size == 1
n_('Found {{count}} public authority beginning with ' \
'‘{{first_letter}}’',
'Found {{count}} public authorities beginning with ' \
'‘{{first_letter}}’',
@public_bodies.total_entries,
count: @public_bodies.total_entries,
first_letter: @tag)
else
category = PublicBody.category_list.find_by(category_tag: @tag)
if category.nil?
n_('Found {{count}} public authority matching the tag ' \
'‘{{tag_name}}’',
'Found {{count}} public authorities matching the tag ' \
'‘{{tag_name}}’',
@public_bodies.total_entries,
count: @public_bodies.total_entries,
tag_name: @tag)
else
n_('Found {{count}} public authority in the category ' \
'‘{{category_title}}’',
'Found {{count}} public authorities in the category ' \
'‘{{category_title}}’',
@public_bodies.total_entries,
count: @public_bodies.total_entries,
category_title: category.title)
end
end
render template: 'public_body/list'
end
end
# Used so URLs like /local/islington work, for use e.g. writing to a local paper.
def list_redirect
@tag = params[:tag]
redirect_to list_public_bodies_url(tag: @tag)
end
# GET /body/all-authorities.csv
#
# Returns all public bodies (except for the internal admin authority) as CSV
def list_all_csv
output = Rails.root.join('cache', 'all-authorities.csv')
return head :no_content unless output.exist?
# Send the file
send_file(output,
type: 'text/csv; charset=utf-8; header=present',
filename: File.basename(output),
disposition: 'attachment',
encoding: 'utf8')
end
# Type ahead search
def search_typeahead
query = params[:query]
return head :bad_request unless query
flash[:search_params] = params.slice(:query, :bodies, :page)
@xapian_requests = typeahead_search(query, model: PublicBody)
render partial: "public_body/search_ahead"
end
end