aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/incoming_message.rb5
-rw-r--r--app/models/info_request.rb2
-rw-r--r--app/models/outgoing_message.rb5
-rw-r--r--app/models/public_body.rb73
4 files changed, 63 insertions, 22 deletions
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index ae7d1201e..b0b02fdda 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -569,7 +569,8 @@ class IncomingMessage < ActiveRecord::Base
text.gsub!(/(Complaints and Corporate Affairs Officer)\s+Westminster Primary Care Trust.+/ms, "\\1")
# Remove WhatDoTheyKnow signup links
- text.gsub!(/http:\/\/www.whatdotheyknow.com\/c\/[^\s]+/, "[WDTK login link]")
+ domain = MySociety::Config.get('DOMAIN')
+ text.gsub!(/http:\/\/#{domain}\/c\/[^\s]+/, "[WDTK login link]")
# Remove Home Office survey links
# e.g. http://www.whatdotheyknow.com/request/serious_crime_act_2007_section_7#incoming-12650
@@ -1134,7 +1135,7 @@ class IncomingMessage < ActiveRecord::Base
elsif content_type == 'text/html'
# lynx wordwraps links in its output, which then don't get formatted properly
# by Alaveteli. We use elinks instead, which doesn't do that.
- external_command("/usr/bin/elinks", "-dump-charset", "utf-8", "-force-html", "-dump",
+ external_command("/usr/bin/elinks", "-eval", "'set document.codepage.assume = \"utf-8\"'", "-dump-charset", "utf-8", "-force-html", "-dump",
tempfile.path, :append_to => text)
elsif content_type == 'application/vnd.ms-excel'
# Bit crazy using /usr/bin/strings - but xls2csv, xlhtml and
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 419546c99..c667e1499 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -651,6 +651,8 @@ public
# days is a very long time.
def date_very_overdue_after
last_sent = last_event_forming_initial_request
+ very_late_days_later = MySociety::Config.get('REPLY_VERY_LATE_AFTER_DAYS', 40)
+ school_very_late_days_later = MySociety::Config.get('SPECIAL_REPLY_VERY_LATE_AFTER_DAYS', 60)
if self.public_body.is_school?
# schools have 60 working days maximum (even over a long holiday)
return Holiday.due_date_from(self.date_initial_request_last_sent_at, 60)
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index c7ba362e0..b7e310b1e 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -54,14 +54,15 @@ class OutgoingMessage < ActiveRecord::Base
# How the default letter starts and ends
def get_salutation
- ret = _("Dear ")
+ ret = ""
if self.message_type == 'followup' && !self.incoming_message_followup.nil? && !self.incoming_message_followup.safe_mail_from.nil? && self.incoming_message_followup.valid_to_reply_to?
ret = ret + OutgoingMailer.name_for_followup(self.info_request, self.incoming_message_followup)
else
ret = ret + self.info_request.public_body.name
end
- return ret + ","
+ salutation = _("Dear {{public_body_name}},", :public_body_name => ret)
end
+
def get_signoff
if self.message_type == 'followup' && !self.incoming_message_followup.nil? && !self.incoming_message_followup.safe_mail_from.nil? && self.incoming_message_followup.valid_to_reply_to?
return _("Yours sincerely,")
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index ef1d60e26..e90050688 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -38,7 +38,7 @@ class PublicBody < ActiveRecord::Base
validates_uniqueness_of :short_name, :message => N_("Short name is already taken"), :if => Proc.new { |pb| pb.short_name != "" }
validates_uniqueness_of :name, :message => N_("Name is already taken")
-
+
has_many :info_requests, :order => 'created_at desc'
has_many :track_things, :order => 'created_at desc'
@@ -46,6 +46,40 @@ class PublicBody < ActiveRecord::Base
translates :name, :short_name, :request_email, :url_name, :notes, :first_letter, :publication_scheme
+ # Convenience methods for creating/editing translations via forms
+ def translation(locale)
+ self.translations.find_by_locale(locale)
+ end
+
+ # XXX - Don't like repeating this!
+ def calculate_cached_fields(t)
+ t.first_letter = t.name.scan(/^./mu)[0].upcase
+ short_long_name = t.name
+ short_long_name = t.short_name if t.short_name and !t.short_name.empty?
+ t.url_name = MySociety::Format.simplify_url_part(short_long_name, 'body')
+ end
+
+ def translated_versions
+ translations
+ end
+
+ def translated_versions=(translation_attrs)
+ if translation_attrs.respond_to? :each_value # Hash => updating
+ translation_attrs.each_value do |attrs|
+ t = translation(attrs[:locale]) || PublicBody::Translation.new
+ t.attributes = attrs
+ calculate_cached_fields(t)
+ t.save!
+ end
+ else # Array => creating
+ translation_attrs.each do |attrs|
+ new_translation = PublicBody::Translation.new(attrs)
+ calculate_cached_fields(new_translation)
+ translations << new_translation
+ end
+ end
+ end
+
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
def after_initialize
@@ -54,15 +88,16 @@ class PublicBody < ActiveRecord::Base
# like find_by_url_name but also search historic url_name if none found
def self.find_by_url_name_with_historic(name)
- @locale = I18n.locale.to_s
- PublicBody.with_locale(@locale) do
+ locale = self.locale || I18n.locale
+ PublicBody.with_locale(locale) do
found = PublicBody.find(:all,
:conditions => ["public_body_translations.url_name='#{name}'"],
:joins => :translations,
:readonly => false)
- return found.first if found.size == 1
- # Shouldn't we just make url_name unique?
- raise "Two bodies with the same URL name: #{name}" if found.size > 1
+ # If many bodies are found (usually because the url_name is the same across
+ # locales) return any of them
+ return found.first if found.size >= 1
+
# If none found, then search the history of short names
old = PublicBody::Version.find_all_by_url_name(name)
# Find unique public bodies in it
@@ -138,7 +173,7 @@ class PublicBody < ActiveRecord::Base
return 'defunct'
elsif self.not_apply?
return 'not_apply'
- elsif self.request_email.empty? or self.request_email == 'blank'
+ elsif self.request_email.nil? or self.request_email.empty? or self.request_email == 'blank'
return 'bad_contact'
else
raise "requestable_failure_reason called with type that has no reason"
@@ -190,7 +225,6 @@ class PublicBody < ActiveRecord::Base
# When name or short name is changed, also change the url name
def short_name=(short_name)
-
globalize.write(self.class.locale || I18n.locale, :short_name, short_name)
self[:short_name] = short_name
self.update_url_name
@@ -203,15 +237,15 @@ class PublicBody < ActiveRecord::Base
end
def update_url_name
- url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
- self.url_name = url_name
+ self.url_name = MySociety::Format.simplify_url_part(self.short_or_long_name, 'body')
end
+
# Return the short name if present, or else long name
def short_or_long_name
- if self.short_name.nil? # can happen during construction
+ if self.short_name.nil? || self.short_name.empty? # 'nil' can happen during construction
self.name
else
- self.short_name.empty? ? self.name : self.short_name
+ self.short_name
end
end
@@ -340,9 +374,12 @@ class PublicBody < ActiveRecord::Base
row.each_with_index {|field, i| field_names[field] = i}
next
end
+
+ fields = {}
+ field_names.each{|name, i| fields[name] = row[i]}
- name = row[field_names['name']]
- email = row[field_names['email']]
+ name = fields['name']
+ email = fields['email']
next if name.nil?
if email.nil?
email = '' # unknown/bad contact is empty string
@@ -355,7 +392,7 @@ class PublicBody < ActiveRecord::Base
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]
@@ -366,9 +403,9 @@ class PublicBody < ActiveRecord::Base
public_body.last_edit_comment = 'Updated from spreadsheet'
public_body.save!
end
-
+
additional_locales.each do |locale|
- localized_name = field_names["name.#{locale}"] && row[field_names["name.#{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})."
@@ -385,7 +422,7 @@ class PublicBody < ActiveRecord::Base
public_body.save!
additional_locales.each do |locale|
- localized_name = field_names["name.#{locale}"] && row[field_names["name.#{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})"