aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/incoming_message.rb4
-rw-r--r--app/models/info_request.rb31
-rw-r--r--app/models/info_request_event.rb4
-rw-r--r--app/models/profile_photo.rb12
-rw-r--r--app/models/public_body.rb34
-rw-r--r--app/models/public_body_change_request.rb130
6 files changed, 194 insertions, 21 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index bcf0b6ec9..59e61952e 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -385,6 +385,10 @@ class IncomingMessage < ActiveRecord::Base
multiline_original_message = '(' + '''>>>.* \d\d/\d\d/\d\d\d\d\s+\d\d:\d\d(?::\d\d)?\s*>>>''' + ')'
text.gsub!(/^(#{multiline_original_message}\n.*)$/m, replacement)
+ # On Thu, Nov 28, 2013 at 9:08 AM, A User
+ # <[1]request-7-skm40s2ls@xxx.xxxx> wrote:
+ text.gsub!(/^( On [^\n]+\n\s*\<[^>\n]+\> (wrote|said):\s*\n.*)$/m, replacement)
+
# Single line sections
text.gsub!(/^(>.*\n)/, replacement)
text.gsub!(/^(On .+ (wrote|said):\n)/, replacement)
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 0a073dc79..def319ad4 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -113,8 +113,12 @@ class InfoRequest < ActiveRecord::Base
# Possible reasons that a request could be reported for administrator attention
def report_reasons
- ["Contains defamatory material", "Not a valid request", "Request for personal information",
- "Contains personal information", "Vexatious", "Other"]
+ [_("Contains defamatory material"),
+ _("Not a valid request"),
+ _("Request for personal information"),
+ _("Contains personal information"),
+ _("Vexatious"),
+ _("Other")]
end
def must_be_valid_state
@@ -1228,6 +1232,23 @@ public
return [xapian_similar, xapian_similar_more]
end
+ def InfoRequest.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,
+ :limit => 25,
+ :sort_by_prefix => 'created_at',
+ :sort_by_ascending => true,
+ :collapse_by_prefix => 'request_collapse'
+ )
+ list_results = xapian_object.results.map { |r| r[:model] }
+ matches_estimated = xapian_object.matches_estimated
+ show_no_more_than = [matches_estimated, max_results].min
+ return { :results => list_results,
+ :matches_estimated => matches_estimated,
+ :show_no_more_than => show_no_more_than }
+ end
+
def InfoRequest.recent_requests
request_events = []
request_events_all_successful = false
@@ -1275,6 +1296,12 @@ public
return [request_events, request_events_all_successful]
end
+ def InfoRequest.find_in_state(state)
+ find(:all, :select => '*, ' + last_event_time_clause + ' as last_event_time',
+ :conditions => ["described_state = ?", state],
+ :order => "last_event_time")
+ end
+
private
def set_defaults
diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb
index e268b28ca..5eed5ba83 100644
--- a/app/models/info_request_event.rb
+++ b/app/models/info_request_event.rb
@@ -21,6 +21,9 @@
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
class InfoRequestEvent < ActiveRecord::Base
+
+ extend XapianQueries
+
belongs_to :info_request
validates_presence_of :info_request
@@ -416,4 +419,5 @@ class InfoRequestEvent < ActiveRecord::Base
yield(column.human_name, self.send(column.name), column.type.to_s, column.name)
end
end
+
end
diff --git a/app/models/profile_photo.rb b/app/models/profile_photo.rb
index 322ebe53c..6c3b2cfa0 100644
--- a/app/models/profile_photo.rb
+++ b/app/models/profile_photo.rb
@@ -70,21 +70,25 @@ class ProfilePhoto < ActiveRecord::Base
def data_and_draft_checks
if self.data.nil?
- errors.add(:data, N_("Please choose a file containing your photo."))
+ errors.add(:data, _("Please choose a file containing your photo."))
return
end
if self.image.nil?
- errors.add(:data, N_("Couldn't understand the image file that you uploaded. PNG, JPEG, GIF and many other common image file formats are supported."))
+ errors.add(:data, _("Couldn't understand the image file that you uploaded. PNG, JPEG, GIF and many other common image file formats are supported."))
return
end
if self.image.format != 'PNG'
- errors.add(:data, N_("Failed to convert image to a PNG"))
+ errors.add(:data, _("Failed to convert image to a PNG"))
end
if !self.draft && (self.image.columns != WIDTH || self.image.rows != HEIGHT)
- errors.add(:data, N_("Failed to convert image to the correct size: at {{cols}}x{{rows}}, need {{width}}x{{height}}", :cols => self.image.columns, :rows => self.image.rows, :width => WIDTH, :height => HEIGHT))
+ errors.add(:data, _("Failed to convert image to the correct size: at {{cols}}x{{rows}}, need {{width}}x{{height}}",
+ :cols => self.image.columns,
+ :rows => self.image.rows,
+ :width => WIDTH,
+ :height => HEIGHT))
end
if self.draft && self.user_id
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 933825d2a..a78a6677e 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -346,22 +346,26 @@ class PublicBody < ActiveRecord::Base
# The "internal admin" is a special body for internal use.
def PublicBody.internal_admin_body
- I18n.with_locale(I18n.default_locale) do
- pb = PublicBody.find_by_url_name("internal_admin_authority")
- if pb.nil?
- pb = PublicBody.new(
- :name => 'Internal admin authority',
- :short_name => "",
- :request_email => AlaveteliConfiguration::contact_email,
- :home_page => "",
- :notes => "",
- :publication_scheme => "",
- :last_edit_editor => "internal_admin",
- :last_edit_comment => "Made by PublicBody.internal_admin_body"
- )
- pb.save!
+ # Use find_by_sql to avoid the search being specific to a
+ # locale, since url_name is a translated field:
+ sql = "SELECT * FROM public_bodies WHERE url_name = 'internal_admin_authority'"
+ matching_pbs = PublicBody.find_by_sql sql
+ case
+ when matching_pbs.empty? then
+ I18n.with_locale(I18n.default_locale) do
+ PublicBody.create!(:name => 'Internal admin authority',
+ :short_name => "",
+ :request_email => AlaveteliConfiguration::contact_email,
+ :home_page => "",
+ :notes => "",
+ :publication_scheme => "",
+ :last_edit_editor => "internal_admin",
+ :last_edit_comment => "Made by PublicBody.internal_admin_body")
end
- return pb
+ when matching_pbs.length == 1 then
+ matching_pbs[0]
+ else
+ raise "Multiple public bodies (#{matching_pbs.length}) found with url_name 'internal_admin_authority'"
end
end
diff --git a/app/models/public_body_change_request.rb b/app/models/public_body_change_request.rb
new file mode 100644
index 000000000..c1f395c0c
--- /dev/null
+++ b/app/models/public_body_change_request.rb
@@ -0,0 +1,130 @@
+# == Schema Information
+#
+# Table name: public_body_change_requests
+#
+# id :integer not null, primary key
+# user_email :string(255)
+# user_name :string(255)
+# user_id :integer
+# public_body_name :text
+# public_body_id :integer
+# public_body_email :string(255)
+# source_url :text
+# notes :text
+# is_open :boolean default(TRUE), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+class PublicBodyChangeRequest < ActiveRecord::Base
+
+ belongs_to :user
+ belongs_to :public_body
+ validates_presence_of :public_body_name, :message => N_("Please enter the name of the authority"),
+ :unless => proc{ |change_request| change_request.public_body }
+ validates_presence_of :user_name, :message => N_("Please enter your name"),
+ :unless => proc{ |change_request| change_request.user }
+ validates_presence_of :user_email, :message => N_("Please enter your email address"),
+ :unless => proc{ |change_request| change_request.user }
+ validate :user_email_format, :unless => proc{ |change_request| change_request.user_email.blank? }
+ validate :body_email_format, :unless => proc{ |change_request| change_request.public_body_email.blank? }
+
+ scope :new_body_requests, :conditions => ['public_body_id IS NULL'], :order => 'created_at'
+ scope :body_update_requests, :conditions => ['public_body_id IS NOT NULL'], :order => 'created_at'
+ scope :open, :conditions => ['is_open = ?', true]
+
+ def self.from_params(params, user)
+ change_request = new
+ change_request.update_from_params(params, user)
+ end
+
+ def update_from_params(params, user)
+ if user
+ self.user_id = user.id
+ else
+ self.user_name = params[:user_name]
+ self.user_email = params[:user_email]
+ end
+ self.public_body_name = params[:public_body_name]
+ self.public_body_id = params[:public_body_id]
+ self.public_body_email = params[:public_body_email]
+ self.source_url = params[:source_url]
+ self.notes = params[:notes]
+ self
+ end
+
+ def get_user_name
+ user ? user.name : user_name
+ end
+
+ def get_user_email
+ user ? user.email : user_email
+ end
+
+ def get_public_body_name
+ public_body ? public_body.name : public_body_name
+ end
+
+ def send_message
+ if public_body
+ ContactMailer.update_public_body_email(self).deliver
+ else
+ ContactMailer.add_public_body(self).deliver
+ end
+ end
+
+ def thanks_notice
+ if self.public_body
+ _("Your request to update the address for #{get_public_body_name} has been sent. Thank you for getting in touch! We'll get back to you soon.")
+ else
+ _("Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon.")
+ end
+ end
+
+ def send_response(subject, response)
+ ContactMailer.from_admin_message(get_user_name,
+ get_user_email,
+ subject,
+ response.strip.html_safe).deliver
+ end
+
+ def comment_for_public_body
+ comments = [_("Requested by: #{get_user_name} (#{get_user_email})")]
+ if !source_url.blank?
+ comments << _("Source URL: #{source_url}")
+ end
+ if !notes.blank?
+ comments << _("Notes: #{notes}")
+ end
+ comments.join("\n")
+ end
+
+ def default_response_subject
+ if self.public_body
+ _("Your request to update {{public_body_name}} on {{site_name}}", :site_name => AlaveteliConfiguration::site_name,
+ :public_body_name => public_body.name)
+ else
+ _("Your request to add {{public_body_name}} to {{site_name}}", :site_name => AlaveteliConfiguration::site_name,
+ :public_body_name => public_body_name)
+ end
+ end
+
+ def close!
+ self.is_open = false
+ self.save!
+ end
+
+ private
+
+ def body_email_format
+ unless MySociety::Validate.is_valid_email(self.public_body_email)
+ errors.add(:public_body_email, _("The authority email doesn't look like a valid address"))
+ end
+ end
+
+ def user_email_format
+ unless MySociety::Validate.is_valid_email(self.user_email)
+ errors.add(:user_email, _("Your email doesn't look like a valid address"))
+ end
+ end
+end