aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/application_mailer.rb4
-rw-r--r--app/models/info_request.rb3
-rw-r--r--app/models/info_request_event.rb26
-rw-r--r--app/models/public_body.rb90
-rw-r--r--app/models/track_thing.rb31
5 files changed, 100 insertions, 54 deletions
diff --git a/app/models/application_mailer.rb b/app/models/application_mailer.rb
index 9628d7339..e9f82a2c3 100644
--- a/app/models/application_mailer.rb
+++ b/app/models/application_mailer.rb
@@ -15,8 +15,8 @@ class ApplicationMailer < ActionMailer::Base
self.raise_delivery_errors = true
def contact_from_name_and_email
- contact_name = MySociety::Config.get("CONTACT_NAME", 'contact@localhost')
- contact_email = MySociety::Config.get("CONTACT_EMAIL", 'Alaveteli')
+ contact_name = MySociety::Config.get("CONTACT_NAME", 'Alaveteli')
+ contact_email = MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost')
return "#{contact_name} <#{contact_email}>"
end
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 67ebd01b0..978e2cd08 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -451,7 +451,7 @@ public
self.log_event("response", params)
self.save!
end
-
+ self.info_request_events.each { |event| event.xapian_mark_needs_index } # for the "waiting_classification" index
RequestMailer.deliver_new_response(self, incoming_message)
end
@@ -564,6 +564,7 @@ public
def calculate_event_states
curr_state = nil
for event in self.info_request_events.reverse
+ event.xapian_mark_needs_index # we need to reindex all events in order to update their latest_* terms
if curr_state.nil?
if !event.described_state.nil?
curr_state = event.described_state
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index 5ae1f5b2d..4ea89bf81 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -94,7 +94,7 @@ class InfoRequestEvent < ActiveRecord::Base
[ :created_at_numeric, 1, "created_at", :number ], # for sorting
[ :described_at_numeric, 2, "described_at", :number ], # XXX using :number for lack of :datetime support in Xapian values
[ :request, 3, "request_collapse", :string ],
- [ :request_title_collapse, 4, "request_title_collapse", :string ]
+ [ :request_title_collapse, 4, "request_title_collapse", :string ],
],
:terms => [ [ :calculated_state, 'S', "status" ],
[ :requested_by, 'B', "requested_by" ],
@@ -102,6 +102,9 @@ class InfoRequestEvent < ActiveRecord::Base
[ :commented_by, 'C', "commented_by" ],
[ :request, 'R', "request" ],
[ :variety, 'V', "variety" ],
+ [ :latest_variety, 'K', "latest_variety" ],
+ [ :latest_status, 'L', "latest_status" ],
+ [ :waiting_classification, 'W', "waiting_classification" ],
[ :filetype, 'T', "filetype" ],
[ :tags, 'U', "tag" ]
],
@@ -129,6 +132,27 @@ class InfoRequestEvent < ActiveRecord::Base
def request
self.info_request.url_title
end
+
+ def latest_variety
+ for event in self.info_request.info_request_events.reverse
+ if !event.variety.nil? and !event.variety.empty?
+ return event.variety
+ end
+ end
+ end
+
+ def latest_status
+ for event in self.info_request.info_request_events.reverse
+ if !event.calculated_state.nil? and !event.calculated_state.empty?
+ return event.calculated_state
+ end
+ end
+ end
+
+ def waiting_classification
+ self.info_request.awaiting_description == true ? "yes" : "no"
+ end
+
def request_title_collapse
url_title = self.info_request.url_title
# remove numeric section from the end, use this to group lots
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index b4d1b6704..81149e3c2 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -206,7 +206,7 @@ class PublicBody < ActiveRecord::Base
return self.created_at.strftime("%Y%m%d%H%M%S")
end
def variety
- "authority"
+ return "authority"
end
# if the URL name has changed, then all requested_from: queries
@@ -344,9 +344,10 @@ class PublicBody < ActiveRecord::Base
# Import from CSV. Just tests things and returns messages if dry_run is true.
# Returns an array of [array of errors, array of notes]. If there are errors,
# always rolls back (as with dry_run).
- def self.import_csv(csv, tag, dry_run, editor, additional_locales = [])
+ def self.import_csv(csv, tag, dry_run, editor, available_locales = [])
errors = []
notes = []
+ available_locales = [I18n.default_locale] if available_locales.empty?
begin
ActiveRecord::Base.transaction do
@@ -363,7 +364,7 @@ class PublicBody < ActiveRecord::Base
end
set_of_importing = Set.new()
- field_names = { 'name'=>1, 'email'=>2 } # Default values in case no field list is given
+ field_names = { 'name'=>1, 'request_email'=>2 } # Default values in case no field list is given
line = 0
CSV::Reader.parse(csv) do |row|
line = line + 1
@@ -378,56 +379,61 @@ class PublicBody < ActiveRecord::Base
fields = {}
field_names.each{|name, i| fields[name] = row[i]}
- name = fields['name']
- email = fields['email']
+ name = row[field_names['name']]
+ email = row[field_names['request_email']]
next if name.nil?
- if email.nil?
- email = '' # unknown/bad contact is empty string
- end
name.strip!
email.strip!
- if email != "" && !MySociety::Validate.is_valid_email(email)
- errors.push "error: line " + line.to_s + ": invalid email " + email + " for authority '" + name + "'"
+ if !email.nil? && !email.empty? && !MySociety::Validate.is_valid_email(email)
+ errors.push "error: line #{line.to_s}: invalid email '#{email}' for authority '#{name}'"
next
end
-
- if bodies_by_name[name]
- # Already have the public body, just update email
- public_body = bodies_by_name[name]
- if public_body.request_email != email
- notes.push "line " + line.to_s + ": updating email for '" + name + "' from " + public_body.request_email + " to " + email
- public_body.request_email = email
- public_body.last_edit_editor = editor
- public_body.last_edit_comment = 'Updated from spreadsheet'
- public_body.save!
- end
-
- additional_locales.each do |locale|
- localized_name = fields["name.#{locale}"]
- PublicBody.with_locale(locale) do
- if !localized_name.nil? and public_body.name != localized_name
- notes.push "line " + line.to_s + ": updating name for '#{name}' from '#{public_body.name}' to '#{localized_name}' (locale: #{locale})."
- public_body.name = localized_name
+
+ field_list = ['name', 'short_name', 'request_email', 'notes', 'publication_scheme', 'home_page']
+
+ if public_body = bodies_by_name[name]
+ available_locales.each do |locale|
+ PublicBody.with_locale(locale) do
+ changed = {}
+ field_list.each do |field_name|
+ localized_field_name = (locale === I18n.default_locale) ? field_name : "#{field_name}.#{locale}"
+ localized_value = field_names[localized_field_name] && row[field_names[localized_field_name]]
+ if !localized_value.nil? and public_body.send(field_name) != localized_value
+ changed[field_name] = "#{public_body.send(field_name)}: #{localized_value}"
+ public_body.send("#{field_name}=", localized_value)
+ end
+ end
+
+ unless changed.empty?
+ notes.push "line #{line.to_s}: updating authority '#{name}' (locale: #{locale}):\n\t#{changed.to_json}"
+ public_body.last_edit_editor = editor
+ public_body.last_edit_comment = 'Updated from spreadsheet'
public_body.save!
end
end
end
- else
- # New public body
- notes.push "line " + line.to_s + ": new authority '" + name + "' with email " + email
- public_body = PublicBody.new(:name => name, :request_email => email, :short_name => "", :home_page => "", :publication_scheme => "", :notes => "", :last_edit_editor => editor, :last_edit_comment => 'Created from spreadsheet')
- public_body.tag_string = tag
- public_body.save!
-
- additional_locales.each do |locale|
- localized_name = fields["name.#{locale}"]
- if !localized_name.nil?
- PublicBody.with_locale(locale) do
- notes.push "line " + line.to_s + ": (aka '#{localized_name}' in locale #{locale})"
- public_body.name = localized_name
- public_body.publication_scheme = ""
+ else # New public body
+ public_body = PublicBody.new(:name=>name, :short_name=>"", :request_email=>"")
+ available_locales.each do |locale|
+ PublicBody.with_locale(locale) do
+ changed = {}
+ field_list.each do |field_name|
+ localized_field_name = (locale === I18n.default_locale) ? field_name : "#{field_name}.#{locale}"
+ localized_value = field_names[localized_field_name] && row[field_names[localized_field_name]]
+ if !localized_value.nil? and public_body.send(field_name) != localized_value
+ changed[field_name] = localized_value
+ public_body.send("#{field_name}=", localized_value)
+ end
+ end
+
+ unless changed.empty?
+ notes.push "line #{line.to_s}: creating new authority '#{name}' (locale: #{locale}):\n\t#{changed.to_json}"
+ public_body.publication_scheme = public_body.publication_scheme || ""
+ public_body.tag_string = tag
+ public_body.last_edit_editor = editor
+ public_body.last_edit_comment = 'Created from spreadsheet'
public_body.save!
end
end
diff --git a/app/models/track_thing.rb b/app/models/track_thing.rb
index 16a0dab87..82848341d 100644
--- a/app/models/track_thing.rb
+++ b/app/models/track_thing.rb
@@ -105,10 +105,25 @@ class TrackThing < ActiveRecord::Base
return track_thing
end
- def TrackThing.create_track_for_search_query(query)
+ def TrackThing.create_track_for_search_query(query, variety_postfix = nil)
track_thing = TrackThing.new
track_thing.track_type = 'search_query'
+ if !(query =~ /variety:/)
+ case variety_postfix
+ when "requests"
+ query += " variety:sent"
+ when "users"
+ query += " variety:user"
+ when "authorities"
+ query += " variety:authority"
+ end
+ end
track_thing.track_query = query
+ # XXX should extract requested_by:, request:, requested_from:
+ # and stick their values into the respective relations.
+ # Should also update "params" to make the list_description
+ # nicer and more generic. It will need to do some clever
+ # parsing of the query to do this nicely
return track_thing
end
@@ -203,15 +218,15 @@ class TrackThing < ActiveRecord::Base
@params = {
# Website
:list_description => "'<a href=\"/search/" + CGI.escapeHTML(self.track_query) + "/newest\">" + CGI.escapeHTML(self.track_query) + "</a>' in new requests/responses", # XXX yeuch, sometimes I just want to call view helpers from the model, sorry! can't work out how
- :verb_on_page => _("Track things matching '{{query}}' by email", :query=>CGI.escapeHTML(self.track_query)),
- :verb_on_page_already => _("You are already tracking things matching '{{query}}' by email", :query=>CGI.escapeHTML(self.track_query)),
+ :verb_on_page => _("Track things matching this search by email"),
+ :verb_on_page_already => _("You are already tracking things matching this search by email"),
# Email
- :title_in_email => _("Requests or responses matching '{{query}}'", :query=>self.track_query),
- :title_in_rss => _("Requests or responses matching '{{query}}'", :query=>self.track_query),
+ :title_in_email => _("Requests or responses matching your saved search"),
+ :title_in_rss => _("Requests or responses matching your saved search"),
# Authentication
- :web => _("To follow requests and responses matching '{{query}}'", :query=>CGI.escapeHTML(self.track_query)),
- :email => _("Then you will be emailed whenever a new request or response matches '{{query}}'.", :query=>CGI.escapeHTML(self.track_query)),
- :email_subject => _("Confirm you want to be emailed about new requests or responses matching '{{query}}'", :query=>self.track_query),
+ :web => _("To follow requests and responses matching your search"),
+ :email => _("Then you will be emailed whenever a new request or response matches your search."),
+ :email_subject => _("Confirm you want to be emailed about new requests or responses matching your search"),
# RSS sorting - XXX hmmm, we don't really know which to use
# here for sorting. Might be a query term (e.g. 'cctv'), in
# which case newest is good, or might be something like