aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/info_request.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/info_request.rb')
-rw-r--r--app/models/info_request.rb187
1 files changed, 107 insertions, 80 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index c203f75c3..3ce0e3cd2 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -1,4 +1,4 @@
-# encoding: utf-8
+# -*- encoding : utf-8 -*-
# == Schema Information
# Schema version: 20131024114346
#
@@ -28,8 +28,11 @@
require 'digest/sha1'
class InfoRequest < ActiveRecord::Base
+ include AdminColumn
include Rails.application.routes.url_helpers
+ @non_admin_columns = %w(title url_title)
+
strip_attributes!
validates_presence_of :title, :message => N_("Please enter a summary of your request")
@@ -50,6 +53,7 @@ class InfoRequest < ActiveRecord::Base
has_many :info_request_events, :order => 'created_at'
has_many :user_info_request_sent_alerts
has_many :track_things, :order => 'created_at desc'
+ has_many :widget_votes
has_many :comments, :order => 'created_at'
has_many :censor_rules, :order => 'created_at desc'
has_many :mail_server_logs, :order => 'mail_server_log_done_id'
@@ -193,23 +197,12 @@ class InfoRequest < ActiveRecord::Base
rescue MissingSourceFile, NameError
end
- # only check on create, so existing models with mixed case are allowed
- def validate_on_create
- if !self.title.nil? && !MySociety::Validate.uses_mixed_capitals(self.title, 10)
- errors.add(:title, _('Please write the summary using a mixture of capital and lower case letters. This makes it easier for others to read.'))
- end
- if !self.title.nil? && title.size > 200
- errors.add(:title, _('Please keep the summary short, like in the subject of an email. You can use a phrase, rather than a full sentence.'))
- end
- if !self.title.nil? && self.title =~ /^(FOI|Freedom of Information)\s*requests?$/i
- errors.add(:title, _('Please describe more what the request is about in the subject. There is no need to say it is an FOI request, we add that on anyway.'))
- end
- end
-
OLD_AGE_IN_DAYS = 21.days
def visible_comments
- self.comments.find(:all, :conditions => 'visible')
+ warn %q([DEPRECATION] InfoRequest#visible_comments will be replaced with
+ InfoRequest#comments.visible as of 0.23).squish
+ comments.visible
end
# If the URL name has changed, then all request: queries will break unless
@@ -345,7 +338,7 @@ public
# only they are sent the email address with the has in it. (We don't check
# the prefix and domain, as sometimes those change, or might be elided by
# copying an email, and that doesn't matter)
- def InfoRequest.find_by_incoming_email(incoming_email)
+ def self.find_by_incoming_email(incoming_email)
id, hash = InfoRequest._extract_id_hash_from_email(incoming_email)
if hash_from_id(id) == hash
# Not using find(id) because we don't exception raised if nothing found
@@ -355,7 +348,7 @@ public
# Return list of info requests which *might* be right given email address
# e.g. For the id-hash email addresses, don't match the hash.
- def InfoRequest.guess_by_incoming_email(incoming_message)
+ def self.guess_by_incoming_email(incoming_message)
guesses = []
# 1. Try to guess based on the email address(es)
incoming_message.addresses.each do |address|
@@ -367,7 +360,7 @@ public
end
# Internal function used by find_by_magic_email and guess_by_incoming_email
- def InfoRequest._extract_id_hash_from_email(incoming_email)
+ def self._extract_id_hash_from_email(incoming_email)
# Match case insensitively, FOI officers often write Request with capital R.
incoming_email = incoming_email.downcase
@@ -394,7 +387,7 @@ public
# repeated requests, say once a quarter for time information, then might need to do that.
# TODO: this *should* also check outgoing message joined to is an initial
# request (rather than follow up)
- def InfoRequest.find_existing(title, public_body_id, body)
+ def self.find_existing(title, public_body_id, body)
return InfoRequest.find(:first, :conditions => [ "title = ? and public_body_id = ? and outgoing_messages.body = ?", title, public_body_id, body ], :include => [ :outgoing_messages ] )
end
@@ -534,7 +527,7 @@ public
# The "holding pen" is a special request which stores incoming emails whose
# destination request is unknown.
- def InfoRequest.holding_pen_request
+ def self.holding_pen_request
ir = InfoRequest.find_by_url_title("holding_pen")
if ir.nil?
ir = InfoRequest.new(
@@ -549,7 +542,7 @@ public
:status => 'ready',
:message_type => 'initial_request',
:body => 'This is the holding pen request. It shows responses that were sent to invalid addresses, and need moving to the correct request by an adminstrator.',
- :last_sent_at => Time.now(),
+ :last_sent_at => Time.now,
:what_doing => 'normal_sort'
})
@@ -564,7 +557,7 @@ public
# states which require administrator action (hence email administrators
# when they are entered, and offer state change dialog to them)
- def InfoRequest.requires_admin_states
+ def self.requires_admin_states
return ['requires_admin', 'error_message', 'attention_requested']
end
@@ -665,11 +658,11 @@ public
if !curr_state.nil? && event.event_type == 'response'
if event.calculated_state != curr_state
event.calculated_state = curr_state
- event.last_described_at = Time.now()
+ event.last_described_at = Time.now
event.save!
end
if event.last_described_at.nil? # TODO: actually maybe this isn't needed
- event.last_described_at = Time.now()
+ event.last_described_at = Time.now
event.save!
end
curr_state = nil
@@ -681,7 +674,7 @@ public
# indexed.
if event.calculated_state != event.described_state
event.calculated_state = event.described_state
- event.last_described_at = Time.now()
+ event.last_described_at = Time.now
event.save!
end
@@ -698,7 +691,7 @@ public
# case there is a preceding response that the described state should be applied to.
if event.calculated_state != event.described_state
event.calculated_state = event.described_state
- event.last_described_at = Time.now()
+ event.last_described_at = Time.now
event.save!
end
end
@@ -783,7 +776,14 @@ public
end
def public_response_events
- self.info_request_events.select{|e| e.response? && e.incoming_message.all_can_view? }
+ condition = <<-SQL
+ info_request_events.event_type = ?
+ AND incoming_messages.prominence = ?
+ SQL
+
+ info_request_events.
+ joins(:incoming_message).
+ where(condition, 'response', 'normal')
end
# The last public response is the default one people might want to reply to
@@ -810,8 +810,9 @@ public
# Text from the the initial request, for use in summary display
def initial_request_text
- return '' if outgoing_messages.empty? # mainly for use with incomplete fixtures
- outgoing_messages.first.get_text_for_indexing
+ return '' if outgoing_messages.empty?
+ body_opts = { :censor_rules => applicable_censor_rules }
+ outgoing_messages.first.try(:get_text_for_indexing, true, body_opts) or ''
end
# Returns index of last event which is described or nil if none described.
@@ -873,7 +874,7 @@ public
# Display version of status
- def InfoRequest.get_status_description(status)
+ def self.get_status_description(status)
descriptions = {
'waiting_classification' => _("Awaiting classification."),
'waiting_response' => _("Awaiting response."),
@@ -908,21 +909,24 @@ public
# Completely delete this request and all objects depending on it
def fully_destroy
- self.track_things.each do |track_thing|
+ track_things.each do |track_thing|
track_thing.track_things_sent_emails.each { |a| a.destroy }
track_thing.destroy
end
- self.user_info_request_sent_alerts.each { |a| a.destroy }
- self.info_request_events.each do |info_request_event|
+ user_info_request_sent_alerts.each { |a| a.destroy }
+ info_request_events.each do |info_request_event|
info_request_event.track_things_sent_emails.each { |a| a.destroy }
info_request_event.destroy
end
- self.mail_server_logs.each do |mail_server_log|
+ mail_server_logs.each do |mail_server_log|
mail_server_log.destroy
end
- self.outgoing_messages.each { |a| a.destroy }
- self.incoming_messages.each { |a| a.destroy }
- self.destroy
+ outgoing_messages.each { |a| a.destroy }
+ incoming_messages.each { |a| a.destroy }
+ comments.each { |comment| comment.destroy }
+ censor_rules.each{ |censor_rule| censor_rule.destroy }
+
+ destroy
end
# Called by incoming_email - and used to be called to generate separate
@@ -932,7 +936,7 @@ public
return InfoRequest.magic_email_for_id(prefix_part, self.id)
end
- def InfoRequest.magic_email_for_id(prefix_part, id)
+ def self.magic_email_for_id(prefix_part, id)
magic_email = AlaveteliConfiguration::incoming_email_prefix
magic_email += prefix_part + id.to_s
magic_email += "-" + InfoRequest.hash_from_id(id)
@@ -946,7 +950,7 @@ public
self.idhash = InfoRequest.hash_from_id(self.id)
end
- def InfoRequest.create_from_attributes(info_request_atts, outgoing_message_atts, user=nil)
+ def self.create_from_attributes(info_request_atts, outgoing_message_atts, user=nil)
info_request = new(info_request_atts)
default_message_params = {
:status => 'ready',
@@ -960,12 +964,12 @@ public
info_request
end
- def InfoRequest.hash_from_id(id)
+ def self.hash_from_id(id)
return Digest::SHA1.hexdigest(id.to_s + AlaveteliConfiguration::incoming_email_secret)[0,8]
end
# Used to find when event last changed
- def InfoRequest.last_event_time_clause(event_type=nil, join_table=nil, join_clause=nil)
+ def self.last_event_time_clause(event_type=nil, join_table=nil, join_clause=nil)
event_type_clause = ''
event_type_clause = " AND info_request_events.event_type = '#{event_type}'" if event_type
tables = ['info_request_events']
@@ -980,20 +984,20 @@ public
LIMIT 1)"
end
- def InfoRequest.last_public_response_clause()
+ def self.last_public_response_clause
join_clause = "incoming_messages.id = info_request_events.incoming_message_id
AND incoming_messages.prominence = 'normal'"
last_event_time_clause('response', 'incoming_messages', join_clause)
end
- def InfoRequest.old_unclassified_params(extra_params, include_last_response_time=false)
- last_response_created_at = last_public_response_clause()
+ def self.old_unclassified_params(extra_params, include_last_response_time=false)
+ last_response_created_at = last_public_response_clause
age = extra_params[:age_in_days] ? extra_params[:age_in_days].days : OLD_AGE_IN_DAYS
params = { :conditions => ["awaiting_description = ?
AND #{last_response_created_at} < ?
AND url_title != 'holding_pen'
AND user_id IS NOT NULL",
- true, Time.now() - age] }
+ true, Time.now - age] }
if include_last_response_time
params[:select] = "*, #{last_response_created_at} AS last_response_time"
params[:order] = 'last_response_time'
@@ -1001,29 +1005,21 @@ public
return params
end
- def InfoRequest.count_old_unclassified(extra_params={})
+ def self.count_old_unclassified(extra_params={})
params = old_unclassified_params(extra_params)
- if extra_params[:conditions]
- condition_string = extra_params[:conditions].shift
- params[:conditions][0] += " AND #{condition_string}"
- params[:conditions] += extra_params[:conditions]
- end
+ add_conditions_from_extra_params(params, extra_params)
count(:all, params)
end
- def InfoRequest.get_random_old_unclassified(limit, extra_params)
+ def self.get_random_old_unclassified(limit, extra_params)
params = old_unclassified_params({})
- if extra_params[:conditions]
- condition_string = extra_params[:conditions].shift
- params[:conditions][0] += " AND #{condition_string}"
- params[:conditions] += extra_params[:conditions]
- end
+ add_conditions_from_extra_params(params, extra_params)
params[:limit] = limit
params[:order] = "random()"
find(:all, params)
end
- def InfoRequest.find_old_unclassified(extra_params={})
+ def self.find_old_unclassified(extra_params={})
params = old_unclassified_params(extra_params, include_last_response_time=true)
[:limit, :include, :offset].each do |extra|
params[extra] = extra_params[extra] if extra_params[extra]
@@ -1032,15 +1028,11 @@ public
params[:order] = extra_params[:order]
params.delete(:select)
end
- if extra_params[:conditions]
- condition_string = extra_params[:conditions].shift
- params[:conditions][0] += " AND #{condition_string}"
- params[:conditions] += extra_params[:conditions]
- end
+ add_conditions_from_extra_params(params, extra_params)
find(:all, params)
end
- def InfoRequest.download_zip_dir()
+ def self.download_zip_dir
File.join(Rails.root, "cache", "zips", "#{Rails.env}")
end
@@ -1058,7 +1050,7 @@ public
end
def request_dirs
- first_three_digits = id.to_s()[0..2]
+ first_three_digits = id.to_s[0..2]
File.join(first_three_digits.to_s, id.to_s)
end
@@ -1067,7 +1059,7 @@ public
end
def make_zip_cache_path(user)
- cache_file_dir = File.join(InfoRequest.download_zip_dir(),
+ cache_file_dir = File.join(InfoRequest.download_zip_dir,
"download",
request_dirs,
last_update_hash)
@@ -1191,7 +1183,7 @@ public
end
# This is called from cron regularly.
- def InfoRequest.stop_new_responses_on_old_requests
+ def self.stop_new_responses_on_old_requests
# 6 months since last change to request, only allow new incoming messages from authority domains
InfoRequest.update_all "allow_new_responses_from = 'authority_only' where updated_at < (now() - interval '6 months') and allow_new_responses_from = 'anybody' and url_title <> 'holding_pen'"
# 1 year since last change requests, don't allow any new incoming messages
@@ -1240,7 +1232,7 @@ public
:model => self.class.base_class.to_s,
:model_id => self.id)
end
- req.save()
+ req.save
end
end
@@ -1270,13 +1262,6 @@ public
PublicBody.set_callback(:save, :after, :purge_in_cache)
end
- def for_admin_column
- self.class.content_columns.map{|c| c unless %w(title url_title).include?(c.name) }.compact.each do |column|
- yield(column.human_name, self.send(column.name), column.type.to_s, column.name)
- end
- end
-
-
# Get requests that have similar important terms
def similar_requests(limit=10)
xapian_similar = nil
@@ -1292,7 +1277,7 @@ public
return [xapian_similar, xapian_similar_more]
end
- def InfoRequest.request_list(filters, page, per_page, max_results)
+ def self.request_list(filters, page, per_page, max_results)
xapian_object = ActsAsXapian::Search.new([InfoRequestEvent],
InfoRequestEvent.make_query_from_params(filters),
:offset => (page - 1) * per_page,
@@ -1309,7 +1294,7 @@ public
:show_no_more_than => show_no_more_than }
end
- def InfoRequest.recent_requests
+ def self.recent_requests
request_events = []
request_events_all_successful = false
# Get some successful requests
@@ -1356,12 +1341,45 @@ public
return [request_events, request_events_all_successful]
end
- def InfoRequest.find_in_state(state)
+ def self.find_in_state(state)
select("*, #{ last_event_time_clause } as last_event_time").
where(:described_state => state).
order('last_event_time')
end
+ def move_to_public_body(destination_public_body, opts = {})
+ old_body = public_body
+ editor = opts.fetch(:editor)
+
+ attrs = { :public_body => destination_public_body }
+
+ if destination_public_body
+ attrs.merge!({
+ :law_used => destination_public_body.law_only_short.downcase
+ })
+ end
+
+ if update_attributes(attrs)
+ log_event('move_request',
+ :editor => editor,
+ :public_body_url_name => public_body.url_name,
+ :old_public_body_url_name => old_body.url_name)
+
+ reindex_request_events
+
+ public_body
+ end
+ end
+
+ # The DateTime of the last InfoRequestEvent belonging to the InfoRequest
+ # Only available if the last_event_time attribute has been set. This is
+ # currentlt only set through .find_in_state
+ #
+ # Returns a DateTime
+ def last_event_time
+ attributes['last_event_time'].try(:to_datetime)
+ end
+
private
def set_defaults
@@ -1370,11 +1388,12 @@ public
self.described_state = 'waiting_response'
end
rescue ActiveModel::MissingAttributeError
- # this should only happen on Model.exists?() call. It can be safely ignored.
+ # this should only happen on Model.exists? call. It can be safely ignored.
# See http://www.tatvartha.com/2011/03/activerecordmissingattributeerror-missing-attribute-a-bug-or-a-features/
end
+
# FOI or EIR?
- if !self.public_body.nil? && self.public_body.eir_only?
+ if new_record? && public_body && public_body.eir_only?
self.law_used = 'eir'
end
end
@@ -1390,5 +1409,13 @@ public
errors.add(:title, _('Please describe more what the request is about in the subject. There is no need to say it is an FOI request, we add that on anyway.'))
end
end
+
+ def self.add_conditions_from_extra_params(params, extra_params)
+ if extra_params[:conditions]
+ condition_string = extra_params[:conditions].shift
+ params[:conditions][0] += " AND #{condition_string}"
+ params[:conditions] += extra_params[:conditions]
+ end
+ end
end