aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/incoming_message.rb7
-rw-r--r--app/models/info_request.rb71
-rw-r--r--app/models/info_request_event.rb90
-rw-r--r--app/models/outgoing_message.rb13
-rw-r--r--app/models/public_body.rb6
-rw-r--r--app/models/user.rb8
6 files changed, 123 insertions, 72 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index 8e6783495..83e16d92c 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -17,7 +17,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: incoming_message.rb,v 1.69 2008-03-21 14:45:38 francis Exp $
+# $Id: incoming_message.rb,v 1.70 2008-03-31 17:20:58 francis Exp $
# TODO
@@ -72,11 +72,6 @@ class IncomingMessage < ActiveRecord::Base
has_many :outgoing_message_followups, :class_name => OutgoingMessage
- acts_as_solr :fields => [
- :get_text_for_indexing,
- { :created_at => :date }
- ], :if => "$do_solr_index"
-
# Return the structured TMail::Mail object
# Documentation at http://i.loveruby.net/en/projects/tmail/doc/
def mail
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 353c84105..8c626d2e6 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -22,7 +22,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: info_request.rb,v 1.73 2008-03-25 17:25:09 francis Exp $
+# $Id: info_request.rb,v 1.74 2008-03-31 17:20:59 francis Exp $
require 'digest/sha1'
@@ -64,34 +64,6 @@ class InfoRequest < ActiveRecord::Base
end
# Full text search indexing
- acts_as_solr :fields => [
- :title,
- :initial_request_text,
- { :status => :string },
- { :requested_by => :string },
- { :requested_from => :string },
- { :created_at => :date },
- { :type => :string} # see "def type" below
- ], :if => "$do_solr_index"
- def status # for name in Solr queries
- calculate_status
- end
- def requested_by
- self.user.url_name
- end
- def requested_from
- self.public_body.url_name
- end
- # acts_on_solr indexes things with type: anyway but by default does text (full text)
- # rather than string (flag) indexing for it. The entry in acts_on_solr above forces
- # the type to be string, and this function returns the same value as acts_on_solr would
- # anyway. Also, only needs to happen in this one model, as others are
- # covered automatically by the multi solr search query command, which treats types same
- # across all models.
- def type
- "InfoRequest"
- end
-
$do_solr_index = false
$do_solr_index_marking = false
def InfoRequest.update_solr_index
@@ -106,32 +78,28 @@ class InfoRequest < ActiveRecord::Base
info_request = InfoRequest.find(id, :lock =>true)
do_index = (info_request.prominence != 'backpage')
- if do_index
- if not info_request.solr_save
- raise "failed to solr_save"
- end
- else
- if not info_request.solr_destroy
- raise "failed to solr_destroy"
+ # fill in any missing event states for all responses - so responses which
+ # have not been described get the status of later ones.
+ events = info_request.info_request_events.find(:all, :order => "created_at")
+ curr_state = info_request.described_state
+ for event in events.reverse
+ if event.event_type == 'response'
+ if not event.described_state.nil?
+ curr_state = event.described_state
+ end
+ event.described_state = curr_state
end
end
- for outgoing_message in info_request.outgoing_messages
- # Initial request text is indexed for InfoRequest models -
- # see :initial_request_text in acts_as_solr entry above
- if do_index and outgoing_message.message_type != 'initial_request'
- outgoing_message.solr_save
+ # index all the events
+ for event in events
+ if do_index and event.indexed_by_solr
+ event.solr_save
else
- outgoing_message.solr_destroy
- end
- end
- for incoming_message in info_request.incoming_messages
- if do_index
- incoming_message.solr_save
- else
- incoming_message.solr_destroy
+ event.solr_destroy
end
end
+
$do_solr_index = false # disable indexing again while we save it, or else destroyed things get put back
$do_solr_index_marking = true # but record that we want to set solr_up_to_date to be true, so before_update doesn't clobber it
info_request.solr_up_to_date = true
@@ -141,7 +109,7 @@ class InfoRequest < ActiveRecord::Base
$do_solr_index = true
end
end
- InfoRequest.solr_optimize
+ InfoRequestEvent.solr_optimize
$do_solr_index = false
end
def before_update
@@ -412,8 +380,7 @@ public
return ""
end
messages = self.outgoing_messages.find(:all, :order => "created_at")
- excerpt = messages[0].body
- excerpt.sub!(/Dear .+,/, "")
+ excerpt = messages[0].body_without_salutation
return excerpt
end
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index a6f67c4fd..20340e540 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -16,7 +16,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: info_request_event.rb,v 1.25 2008-03-24 09:35:23 francis Exp $
+# $Id: info_request_event.rb,v 1.26 2008-03-31 17:20:59 francis Exp $
class InfoRequestEvent < ActiveRecord::Base
belongs_to :info_request
@@ -47,6 +47,63 @@ class InfoRequestEvent < ActiveRecord::Base
'requires_admin'
]
+ # Full text search indexing
+ acts_as_solr :fields => [
+ { :solr_text_main => :text },
+ { :title => :text },
+ { :status => :string },
+ { :requested_by => :string },
+ { :requested_from => :string },
+ { :created_at => :date },
+ { :variety => :string }
+ ], :if => "$do_solr_index"
+ def status # for name in Solr queries
+ self.info_request.calculate_status
+ end
+ def requested_by
+ if self.event_type == 'sent'
+ self.info_request.user.url_name
+ else
+ nil
+ end
+ end
+ def requested_from
+ if self.event_type == 'sent'
+ self.info_request.public_body.url_name
+ else
+ nil
+ end
+ end
+ def solr_text_main
+ text = ''
+ if self.event_type == 'sent'
+ text = text + self.outgoing_message.body_without_salutation + "\n\n"
+ elsif self.event_type == 'followup_sent'
+ text = text + self.outgoing_message.body_without_salutation + "\n\n"
+ elsif self.event_type == 'response'
+ text = text + self.incoming_message.get_text_for_indexing + "\n\n"
+ else
+ # nothing
+ end
+ return text
+ end
+ def title
+ if self.event_type == 'sent'
+ return self.info_request.title
+ end
+ return ''
+ end
+ def indexed_by_solr
+ if ['sent', 'followup_sent', 'response'].include?(self.event_type)
+ return true
+ else
+ return false
+ end
+ end
+ def variety
+ self.event_type
+ end
+
# We store YAML version of parameters in the database
def params=(params)
self.params_yaml = params.to_yaml
@@ -59,7 +116,7 @@ class InfoRequestEvent < ActiveRecord::Base
# XXX search for the find below and call this function more instead
def incoming_message
if not ['response'].include?(self.event_type)
- raise "only call incoming_message for response events"
+ return nil
end
if not self.params[:incoming_message_id]
@@ -73,7 +130,7 @@ class InfoRequestEvent < ActiveRecord::Base
# XXX search for the find below and call this function more instead
def outgoing_message
if not [ 'edit_outgoing', 'sent', 'resent', 'followup_sent' ].include?(self.event_type)
- raise "only call outgoing_message for appropriate event types"
+ return nil
end
if not self.params[:outgoing_message_id]
@@ -83,6 +140,33 @@ class InfoRequestEvent < ActiveRecord::Base
return OutgoingMessage.find(self.params[:outgoing_message_id].to_i)
end
+ # Display version of status
+ def display_status
+ if incoming_message.nil?
+ raise "display_status only works for incoming messages right now"
+ end
+ status = self.described_state
+ raise status.to_s
+ if status == 'waiting_response'
+ "Acknowledgement"
+ elsif status == 'waiting_clarification'
+ "Clarification request"
+ elsif status == 'not_held'
+ "Information not held"
+ elsif status == 'rejected'
+ "Rejection"
+ elsif status == 'partially_successful'
+ "Partially successful response"
+ elsif status == 'successful'
+ "Successful response"
+ elsif status == 'requires_admin'
+ "Unusual response"
+ else
+ raise "unknown status " + status
+ end
+ end
+
+
end
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index 20e2d33ec..0d959cb25 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -21,7 +21,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: outgoing_message.rb,v 1.38 2008-03-21 14:45:38 francis Exp $
+# $Id: outgoing_message.rb,v 1.39 2008-03-31 17:20:59 francis Exp $
class OutgoingMessage < ActiveRecord::Base
belongs_to :info_request
@@ -32,11 +32,6 @@ class OutgoingMessage < ActiveRecord::Base
belongs_to :incoming_message_followup, :foreign_key => 'incoming_message_followup_id', :class_name => 'IncomingMessage'
- acts_as_solr :fields => [
- :body,
- { :created_at => :date }
- ], :if => "$do_solr_index"
-
# How the default letter starts and ends
def get_salutation
ret = "Dear "
@@ -55,6 +50,12 @@ class OutgoingMessage < ActiveRecord::Base
end
end
+ def body_without_salutation
+ ret = self.body
+ ret.sub!(/Dear .+,/, "")
+ return ret
+ end
+
# Set default letter
def after_initialize
if self.body.nil?
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 2403a97d8..d900e0852 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -21,7 +21,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: public_body.rb,v 1.47 2008-03-25 17:25:09 francis Exp $
+# $Id: public_body.rb,v 1.48 2008-03-31 17:20:59 francis Exp $
require 'csv'
require 'set'
@@ -73,9 +73,9 @@ class PublicBody < ActiveRecord::Base
{:name => { :boost => 10.0 }},
{:short_name => { :boost => 10.0 }},
{ :created_at => :date },
- { :moo => :string }
+ { :variety => :string }
]
- def moo
+ def variety
"authority"
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 1bbbdb83a..09686f7d9 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -20,7 +20,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: user.rb,v 1.41 2008-03-25 17:25:09 francis Exp $
+# $Id: user.rb,v 1.42 2008-03-31 17:20:59 francis Exp $
require 'digest/sha1'
@@ -41,8 +41,12 @@ class User < ActiveRecord::Base
acts_as_solr :fields => [
{:name => { :boost => 5.0 }},
- { :created_at => :date }
+ { :created_at => :date },
+ { :variety => :string }
]
+ def variety
+ "user"
+ end
def validate
errors.add(:email, "doesn't look like a valid address") unless MySociety::Validate.is_valid_email(self.email)