aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/incoming_message.rb1
-rw-r--r--app/models/info_request.rb19
-rw-r--r--app/models/info_request_event.rb42
-rw-r--r--app/models/outgoing_message.rb5
-rw-r--r--app/models/post_redirect.rb2
-rw-r--r--app/models/profile_photo.rb2
-rw-r--r--app/models/public_body.rb25
-rw-r--r--app/models/purge_request.rb39
-rw-r--r--app/models/raw_email.rb2
-rw-r--r--app/models/request_mailer.rb2
-rw-r--r--app/models/track_thing.rb18
-rw-r--r--app/models/user.rb7
12 files changed, 116 insertions, 48 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index cbbcf5aa6..2896de68a 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -816,7 +816,6 @@ class IncomingMessage < ActiveRecord::Base
:filename => _get_part_file_name(leaf),
:charset => leaf.charset,
:within_rfc822_subject => within_rfc822_subject,
- :display_size => "0K",
:body => body)
attachment.save!
attachments << attachment.id
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index b5a1cd833..e570150bb 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -23,6 +23,9 @@
require 'digest/sha1'
class InfoRequest < ActiveRecord::Base
+ include ActionView::Helpers::UrlHelper
+ include ActionController::UrlWriter
+
strip_attributes!
validates_presence_of :title, :message => N_("Please enter a summary of your request")
@@ -453,7 +456,6 @@ public
# An annotation (comment) is made
def add_comment(body, user)
comment = Comment.new
-
ActiveRecord::Base.transaction do
comment.body = body
comment.user = user
@@ -1042,6 +1044,21 @@ public
end
return ret
end
+
+ before_save :purge_in_cache
+ def purge_in_cache
+ if !MySociety::Config.get('VARNISH_HOST').nil? && !self.id.nil?
+ # we only do this for existing info_requests (new ones have a nil id)
+ path = url_for(:controller => 'request', :action => 'show', :url_title => self.url_title, :only_path => true, :locale => :none)
+ req = PurgeRequest.find_by_url(path)
+ if req.nil?
+ req = PurgeRequest.new(:url => path,
+ :model => self.class.base_class.to_s,
+ :model_id => self.id)
+ end
+ req.save()
+ end
+ end
end
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index 99f34cf9e..cb49596cb 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -36,25 +36,29 @@ class InfoRequestEvent < ActiveRecord::Base
has_many :track_things_sent_emails
validates_presence_of :event_type
- validates_inclusion_of :event_type, :in => [
- 'sent',
- 'resent',
- 'followup_sent',
- 'followup_resent',
-
- 'edit', # title etc. edited (in admin interface)
- 'edit_outgoing', # outgoing message edited (in admin interface)
- 'edit_comment', # comment edited (in admin interface)
- 'destroy_incoming', # deleted an incoming message (in admin interface)
- 'destroy_outgoing', # deleted an outgoing message (in admin interface)
- 'redeliver_incoming', # redelivered an incoming message elsewhere (in admin interface)
- 'move_request', # changed user or public body (in admin interface)
- 'manual', # you did something in the db by hand
-
- 'response',
- 'comment',
- 'status_update'
- ]
+
+ def self.enumerate_event_types
+ [
+ 'sent',
+ 'resent',
+ 'followup_sent',
+ 'followup_resent',
+
+ 'edit', # title etc. edited (in admin interface)
+ 'edit_outgoing', # outgoing message edited (in admin interface)
+ 'edit_comment', # comment edited (in admin interface)
+ 'destroy_incoming', # deleted an incoming message (in admin interface)
+ 'destroy_outgoing', # deleted an outgoing message (in admin interface)
+ 'redeliver_incoming', # redelivered an incoming message elsewhere (in admin interface)
+ 'move_request', # changed user or public body (in admin interface)
+ 'manual', # you did something in the db by hand
+
+ 'response',
+ 'comment',
+ 'status_update',
+ ]
+ end
+ validates_inclusion_of :event_type, :in => enumerate_event_types
# user described state (also update in info_request)
validate :must_be_valid_state
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index cc561b21d..de3c916aa 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -267,7 +267,10 @@ class OutgoingMessage < ActiveRecord::Base
end
end
-
+ after_save(:purge_in_cache)
+ def purge_in_cache
+ self.info_request.purge_in_cache
+ end
end
diff --git a/app/models/post_redirect.rb b/app/models/post_redirect.rb
index 59cc86799..c9a6229a4 100644
--- a/app/models/post_redirect.rb
+++ b/app/models/post_redirect.rb
@@ -39,7 +39,7 @@ class PostRedirect < ActiveRecord::Base
self.post_params_yaml = params.to_yaml
end
def post_params
- if self.post_params_yaml.nil?
+ if self.post_params_yaml.nil?
return {}
end
YAML.load(self.post_params_yaml)
diff --git a/app/models/profile_photo.rb b/app/models/profile_photo.rb
index 43dbbbf0a..798094d90 100644
--- a/app/models/profile_photo.rb
+++ b/app/models/profile_photo.rb
@@ -17,8 +17,6 @@
#
# $Id: profile_photo.rb,v 1.2 2009-09-17 21:10:05 francis Exp $
#
-require 'mahoro'
-require 'RMagick'
class ProfilePhoto < ActiveRecord::Base
WIDTH = 96
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 961fa3cbb..54af547bd 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -240,7 +240,7 @@ class PublicBody < ActiveRecord::Base
# Return the short name if present, or else long name
def short_or_long_name
if self.short_name.nil? || self.short_name.empty? # 'nil' can happen during construction
- self.name
+ self.name.nil? ? "" : self.name
else
self.short_name
end
@@ -282,19 +282,11 @@ class PublicBody < ActiveRecord::Base
# Guess home page from the request email, or use explicit override, or nil
# if not known.
def calculated_home_page
- # manual override for ones we calculate wrongly
- if self.home_page != ''
- return self.home_page
+ if home_page && !home_page.empty?
+ home_page[URI::regexp(%w(http https))] ? home_page : "http://#{home_page}"
+ elsif request_email_domain
+ "http://www.#{request_email_domain}"
end
-
- # extract the domain name from the FOI request email
- url = self.request_email_domain
- if url.nil?
- return nil
- end
-
- # add standard URL prefix
- return "http://www." + url
end
# Are all requests to this body under the Environmental Information Regulations?
@@ -527,7 +519,7 @@ class PublicBody < ActiveRecord::Base
end
def has_notes?
- return self.notes != ""
+ return !self.notes.nil? && self.notes != ""
end
def notes_as_html
self.notes
@@ -555,6 +547,11 @@ class PublicBody < ActiveRecord::Base
}
end
+ after_save(:purge_in_cache)
+ def purge_in_cache
+ self.info_requests.each {|x| x.purge_in_cache}
+ end
+
end
diff --git a/app/models/purge_request.rb b/app/models/purge_request.rb
new file mode 100644
index 000000000..088d5b84b
--- /dev/null
+++ b/app/models/purge_request.rb
@@ -0,0 +1,39 @@
+# models/purge_request.rb:
+# A queue of URLs to purge
+#
+# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
+# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
+#
+
+class PurgeRequest < ActiveRecord::Base
+ def self.purge_all
+ done_something = false
+ for item in PurgeRequest.all()
+ item.purge
+ done_something = true
+ end
+ return done_something
+ end
+
+ def self.purge_all_loop
+ # Run purge_all in an endless loop, sleeping when there is nothing to do
+ while true
+ sleep_seconds = 1
+ while !purge_all
+ sleep sleep_seconds
+ sleep_seconds *= 2
+ sleep_seconds = 30 if sleep_seconds > 30
+ end
+ end
+ end
+
+ def purge
+ config = MySociety::Config.load_default()
+ varnish_url = config['VARNISH_HOST']
+ result = quietly_try_to_purge(varnish_url, self.url)
+ self.delete()
+ end
+end
+
+
+
diff --git a/app/models/raw_email.rb b/app/models/raw_email.rb
index 3e12a6feb..1feb9c70b 100644
--- a/app/models/raw_email.rb
+++ b/app/models/raw_email.rb
@@ -27,7 +27,7 @@ class RawEmail < ActiveRecord::Base
def directory
request_id = self.incoming_message.info_request.id.to_s
if ENV["RAILS_ENV"] == "test"
- return File.join(RAILS_ROOT, 'files/raw_email_test')
+ return File.join(Rails.root, 'files/raw_email_test')
else
return File.join(MySociety::Config.get('RAW_EMAILS_LOCATION',
'files/raw_emails'),
diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb
index 83cce9045..177a39241 100644
--- a/app/models/request_mailer.rb
+++ b/app/models/request_mailer.rb
@@ -40,7 +40,7 @@ class RequestMailer < ApplicationMailer
:filename => "original.eml", :transfer_encoding => '7bit', :content_disposition => 'inline'
@body = {
:info_request => info_request,
- :contact_email => MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost')
+ :contact_email => MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost')
}
end
diff --git a/app/models/track_thing.rb b/app/models/track_thing.rb
index 58d70ed86..bdcd87e4f 100644
--- a/app/models/track_thing.rb
+++ b/app/models/track_thing.rb
@@ -108,7 +108,7 @@ class TrackThing < ActiveRecord::Base
end
descriptions = []
if varieties.include? _("requests")
- descriptions << _("requests which are {{list_of_statuses}}", :list_of_statuses => Array(statuses).join(_(' or ')))
+ descriptions << _("requests which are {{list_of_statuses}}", :list_of_statuses => Array(statuses).sort.join(_(' or ')))
varieties -= [_("requests")]
end
if descriptions.empty? and varieties.empty?
@@ -116,7 +116,7 @@ class TrackThing < ActiveRecord::Base
end
descriptions += Array(varieties)
parsed_text = parsed_text.strip
- descriptions = descriptions.join(_(" or "))
+ descriptions = descriptions.sort.join(_(" or "))
if !parsed_text.empty?
descriptions += _("{{list_of_things}} matching text '{{search_query}}'", :list_of_things => "", :search_query => parsed_text)
end
@@ -146,11 +146,15 @@ class TrackThing < ActiveRecord::Base
return track_thing
end
- def TrackThing.create_track_for_public_body(public_body)
+ def TrackThing.create_track_for_public_body(public_body, event_type = nil)
track_thing = TrackThing.new
track_thing.track_type = 'public_body_updates'
track_thing.public_body = public_body
- track_thing.track_query = "requested_from:" + public_body.url_name
+ query = "requested_from:" + public_body.url_name
+ if InfoRequestEvent.enumerate_event_types.include?(event_type)
+ query += " variety:" + event_type
+ end
+ track_thing.track_query = query
return track_thing
end
@@ -171,10 +175,10 @@ class TrackThing < ActiveRecord::Base
query += " variety:sent"
when "users"
query += " variety:user"
- when "authorities"
- query += " variety:authority"
+ when "bodies"
+ query += " variety:authority"
end
- end
+ end
track_thing.track_query = query
# XXX should extract requested_by:, request:, requested_from:
# and stick their values into the respective relations.
diff --git a/app/models/user.rb b/app/models/user.rb
index 59a84b7aa..73d65a8ca 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -422,5 +422,12 @@ class User < ActiveRecord::Base
end
return true
end
+
+ after_save(:purge_in_cache)
+ def purge_in_cache
+ # XXX should only be if specific attributes have changed
+ self.info_requests.each {|x| x.purge_in_cache}
+ end
+
end