From d2e1be54f3e5293fb5291dff7b1f753ddeff3302 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Thu, 24 May 2012 10:52:22 +0100 Subject: External requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add data model support for “external” requests, i.e. requests added via the API rather than made using the site. --- app/models/info_request.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 095a1b1af..2f1270a95 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -33,7 +33,7 @@ class InfoRequest < ActiveRecord::Base validates_format_of :title, :with => /[a-zA-Z]/, :message => N_("Please write a summary with some text in it"), :if => Proc.new { |info_request| !info_request.title.nil? && !info_request.title.empty? } belongs_to :user - #validates_presence_of :user_id # breaks during construction of new ones :( + validate :must_be_internal_or_external belongs_to :public_body validates_presence_of :public_body_id @@ -104,6 +104,22 @@ class InfoRequest < ActiveRecord::Base errors.add(:described_state, "is not a valid state") if !InfoRequest.enumerate_states.include? described_state end + + # The request must either be internal, in which case it has + # a foreign key reference to a User object and no external_url or external_user_name, + # or else be external in which case it has no user_id but does have an external_url, + # and may optionally also have an external_user_name. + # + # External requests are requests that have been added using the API, whereas internal + # requests are requests made using the site. + def must_be_internal_or_external + if user_id.nil? + errors.add(:external_url, "must be provided for an external request") if external_url.nil? + else + errors.add(:external_user_name, "must be null for an internal request") if !external_user_name.nil? + errors.add(:external_url, "must be null for an internal request") if !external_url.nil? + end + end @@custom_states_loaded = false begin -- cgit v1.2.3 From ceefc42a75a06f693fe614d345e619ace5014d23 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 14:49:40 +0100 Subject: API: we can add a response to a request --- app/models/info_request.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 2f1270a95..8d9d92b0d 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -120,6 +120,14 @@ class InfoRequest < ActiveRecord::Base errors.add(:external_url, "must be null for an internal request") if !external_url.nil? end end + + def is_external? + !external_url.nil? + end + + def user_name + is_external? ? external_user_name : user.name + end @@custom_states_loaded = false begin -- cgit v1.2.3 From ba0811b35d41a18a01977ba53147da522541b151 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Wed, 6 Jun 2012 15:37:15 +0100 Subject: Fix indexing of info_request_events Even external requests need to be indexed, for which we need a slug representing the user. --- app/models/info_request.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 8d9d92b0d..99cbe476a 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -128,6 +128,19 @@ class InfoRequest < ActiveRecord::Base def user_name is_external? ? external_user_name : user.name end + + def user_name_slug + if is_external? + if external_user_name.nil? + fake_slug = "anonymous" + else + fake_slug = external_user_name.parameterize + end + public_body.url_name + "_"+fake_slug + else + user.url_name + end + end @@custom_states_loaded = false begin -- cgit v1.2.3 From 67af40e3b1ec14f3da98fd5acc0303476af9d39a Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Wed, 6 Jun 2012 16:26:22 +0100 Subject: Non-logged-in users can create requests Make the info_request validation a bit more forgiving, so that non- logged-in users may create requests. --- app/models/info_request.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 99cbe476a..6cefc3dab 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -113,9 +113,9 @@ class InfoRequest < ActiveRecord::Base # External requests are requests that have been added using the API, whereas internal # requests are requests made using the site. def must_be_internal_or_external - if user_id.nil? - errors.add(:external_url, "must be provided for an external request") if external_url.nil? - else + # We must permit user_id and external_user_name both to be nil, because the system + # allows a request to be created by a non-logged-in user. + if !user_id.nil? errors.add(:external_user_name, "must be null for an internal request") if !external_user_name.nil? errors.add(:external_url, "must be null for an internal request") if !external_url.nil? end -- cgit v1.2.3 From b51b6d503c208daaa1bc4ed99afd6652ccfcd8d7 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Thu, 7 Jun 2012 13:25:44 +0100 Subject: Remove empty unused method --- app/models/info_request.rb | 3 --- 1 file changed, 3 deletions(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 095a1b1af..b02be7ce9 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -515,9 +515,6 @@ public return false end - def can_have_attention_requested? - end - # change status, including for last event for later historical purposes def set_described_state(new_state) ActiveRecord::Base.transaction do -- cgit v1.2.3 From 1ff635e834c004600faa445fc53e757996cde30d Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Thu, 7 Jun 2012 22:04:53 +0100 Subject: WIP API --- app/models/info_request.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models/info_request.rb') diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 6cefc3dab..336d51cee 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -269,7 +269,7 @@ public return self.magic_email("request-") end def incoming_name_and_email - return TMail::Address.address_from_name_and_email(self.user.name, self.incoming_email).to_s + return TMail::Address.address_from_name_and_email(self.user_name, self.incoming_email).to_s end # Subject lines for emails about the request @@ -490,7 +490,7 @@ public 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) + RequestMailer.deliver_new_response(self, incoming_message) if !is_external? end -- cgit v1.2.3