From a63e598b9e0ddfa7ac695b1be41aaace6c0eb628 Mon Sep 17 00:00:00 2001 From: David Cabo Date: Thu, 11 Aug 2011 03:50:10 +0200 Subject: Move log-in point to before Preview and fix modal sign-in and sign-up redirects --- app/controllers/application_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5f18be2e5..4e6ad5efb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -192,7 +192,9 @@ class ApplicationController < ActionController::Base post_redirect = PostRedirect.new(:uri => request.request_uri, :post_params => params, :reason_params => reason_params) post_redirect.save! - redirect_to signin_url(:token => post_redirect.token) + # 'modal' controls whether the sign-in form will be displayed in the typical full-blown + # page or on its own, useful for pop-ups + redirect_to signin_url(:token => post_redirect.token, :modal => params[:modal]) return false end return true -- cgit v1.2.3 From 284808d259b3ba8ba1e6c106d949fffeb110a5a7 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 24 Aug 2011 11:50:10 +0100 Subject: First stab at filtering on the "View requests" page --- app/controllers/application_controller.rb | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0df3e22da..e16f9f5bb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -349,6 +349,61 @@ class ApplicationController < ActionController::Base session[:last_body_id] = public_body.id end + def alter_query_from_params(query) + # various forms are used to customise queries and hide + # xapian's complexity. This parses the form fields and turns + # them into a xapian query string + query = "" if query.nil? + sortby = "newest" + if params[:request_variety] && !(query =~ /variety:/) + sortby = "described" + varieties = [] + if params[:request_variety].include? "sent" + varieties << ['variety:sent', 'variety:followup_sent'] + end + if params[:request_variety].include? "response" + varieties << ['variety:response'] + end + if params[:request_variety].include? "comment" + varieties << ['variety:comment'] + end + query += " (#{varieties.join(' OR ')})" + end + case params[:request_status] + when "recent", "all" + if !(query =~ /variety:/) + query += " (variety:sent)" + end + when "successful" + query += ' (latest_status:successful OR latest_status:partially_successful)' + sortby = "described" + when "unsuccessful" + query += ' (latest_status:rejected OR latest_status:not_held)' + sortby = "described" + when "awaiting" + if query.empty? + query += 'variety:sent ' + end + query += ' NOT (latest_status:successful OR latest_status:partially_successful OR latest_status:rejected OR latest_status:not_held OR latest_status:gone_postal)' + sortby = "described" + when "internal_review" + query += ' (latest_status:internal_review)' + sortby = "described" + end + + if !params[:request_date_after].nil? && params[:request_date_before].nil? + params[:request_date_before] = Date.now.strftime("%d/%m/%Y") + query += " #{params[:request_date_after]}..#{params[:request_date_before]}" + elsif params[:request_date_after].nil? && !params[:request_date_before].nil? + params[:request_date_after] = "01/01/2008" + end + if params[:request_date_after] + query = "#{params[:request_date_after]}..#{params[:request_date_before]} " + query + end + return query, sortby + + end + # URL generating functions are needed by all controllers (for redirects), # views (for links) and mailers (for use in emails), so include them into # all of all. -- cgit v1.2.3 From 45fc8c3e18dc5e43e98fc1fae5b519f1440086ea Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 24 Aug 2011 16:48:55 +0100 Subject: more work in progress on search forms --- app/controllers/application_controller.rb | 72 +++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 23 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e16f9f5bb..31a6ef3db 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -349,10 +349,18 @@ class ApplicationController < ActionController::Base session[:last_body_id] = public_body.id end - def alter_query_from_params(query) + def param_exists(item) + return params[item] && !params[item].empty? + end + + def alter_query_from_params # various forms are used to customise queries and hide # xapian's complexity. This parses the form fields and turns # them into a xapian query string + if params[:latest_status].nil? + params[:latest_status] = params[:view] || "all" + end + query = params[:query] query = "" if query.nil? sortby = "newest" if params[:request_variety] && !(query =~ /variety:/) @@ -369,35 +377,53 @@ class ApplicationController < ActionController::Base end query += " (#{varieties.join(' OR ')})" end - case params[:request_status] - when "recent", "all" - if !(query =~ /variety:/) - query += " (variety:sent)" + if params[:latest_status] && !(query =~ /latest_status:/) + statuses = [] + if params[:latest_status].class == String + params[:latest_status] = [params[:latest_status]] end - when "successful" - query += ' (latest_status:successful OR latest_status:partially_successful)' - sortby = "described" - when "unsuccessful" - query += ' (latest_status:rejected OR latest_status:not_held)' - sortby = "described" - when "awaiting" - if query.empty? - query += 'variety:sent ' + if params[:latest_status].include?("recent") || params[:latest_status].include?("all") + if !(query =~ /variety:/) + query += " (variety:sent)" + end end - query += ' NOT (latest_status:successful OR latest_status:partially_successful OR latest_status:rejected OR latest_status:not_held OR latest_status:gone_postal)' - sortby = "described" - when "internal_review" - query += ' (latest_status:internal_review)' - sortby = "described" - end + if params[:latest_status].include? "successful" + statuses << ['latest_status:successful', 'latest_status:partially_successful'] + sortby = "described" + end + if params[:latest_status].include? "unsuccessful" + statuses << ['latest_status:rejected', 'latest_status:not_held'] + sortby = "described" + end + if params[:latest_status].include? "awaiting" + statuses << ['latest_status:waiting_response', 'latest_status:waiting_clarification', 'waiting_classification:true'] + sortby = "described" + end + if params[:latest_status].include? "internal_review" + statuses << ['status:internal_review'] + sortby = "described" + end + if params[:latest_status].include? "other" + statuses << ['latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin', 'latest_status:user_withdrawn'] + sortby = "described" + end + if params[:latest_status].include? "gone_postal" + statuses << ['latest_status:gone_postal'] + sortby = "described" + end + query += " (#{statuses.join(' OR ')})" - if !params[:request_date_after].nil? && params[:request_date_before].nil? + end + if query.empty? + query = "variety:sent" + end + if param_exists(:request_date_after) && !param_exists(:request_date_before) params[:request_date_before] = Date.now.strftime("%d/%m/%Y") query += " #{params[:request_date_after]}..#{params[:request_date_before]}" - elsif params[:request_date_after].nil? && !params[:request_date_before].nil? + elsif !param_exists(:request_date_after) && param_exists(:request_date_before) params[:request_date_after] = "01/01/2008" end - if params[:request_date_after] + if param_exists(:request_date_after) query = "#{params[:request_date_after]}..#{params[:request_date_before]} " + query end return query, sortby -- cgit v1.2.3 From 5f0c0d59ded4301efa085c4103b84a42a9fa61f6 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Fri, 26 Aug 2011 09:48:01 +0100 Subject: Further work in progress on better search functionality --- app/controllers/application_controller.rb | 81 +++++++++++++++++++------------ 1 file changed, 49 insertions(+), 32 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 31a6ef3db..caf613f8d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -351,22 +351,15 @@ class ApplicationController < ActionController::Base def param_exists(item) return params[item] && !params[item].empty? - end - - def alter_query_from_params - # various forms are used to customise queries and hide - # xapian's complexity. This parses the form fields and turns - # them into a xapian query string - if params[:latest_status].nil? - params[:latest_status] = params[:view] || "all" - end - query = params[:query] - query = "" if query.nil? + end + + def get_request_variety_from_params + query = "" sortby = "newest" - if params[:request_variety] && !(query =~ /variety:/) - sortby = "described" - varieties = [] + varieties = [] + if params[:request_variety] && !(query =~ /variety:/) if params[:request_variety].include? "sent" + varieties -= ['variety:sent', 'variety:followup_sent', 'variety:response', 'variety:comment'] varieties << ['variety:sent', 'variety:followup_sent'] end if params[:request_variety].include? "response" @@ -375,48 +368,50 @@ class ApplicationController < ActionController::Base if params[:request_variety].include? "comment" varieties << ['variety:comment'] end - query += " (#{varieties.join(' OR ')})" end - if params[:latest_status] && !(query =~ /latest_status:/) + if !varieties.empty? + query = " (#{varieties.join(' OR ')})" + end + return query + end + + def get_status_from_params + query = "" + if params[:latest_status] statuses = [] if params[:latest_status].class == String params[:latest_status] = [params[:latest_status]] end if params[:latest_status].include?("recent") || params[:latest_status].include?("all") - if !(query =~ /variety:/) - query += " (variety:sent)" - end + query += " (variety:sent)" end if params[:latest_status].include? "successful" statuses << ['latest_status:successful', 'latest_status:partially_successful'] - sortby = "described" end if params[:latest_status].include? "unsuccessful" statuses << ['latest_status:rejected', 'latest_status:not_held'] - sortby = "described" end if params[:latest_status].include? "awaiting" statuses << ['latest_status:waiting_response', 'latest_status:waiting_clarification', 'waiting_classification:true'] - sortby = "described" end if params[:latest_status].include? "internal_review" statuses << ['status:internal_review'] - sortby = "described" end if params[:latest_status].include? "other" statuses << ['latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin', 'latest_status:user_withdrawn'] - sortby = "described" end if params[:latest_status].include? "gone_postal" statuses << ['latest_status:gone_postal'] - sortby = "described" end - query += " (#{statuses.join(' OR ')})" - - end - if query.empty? - query = "variety:sent" + if !statuses.empty? + query = " (#{statuses.join(' OR ')})" + end end + return query + end + + def get_date_range_from_params + query = "" if param_exists(:request_date_after) && !param_exists(:request_date_before) params[:request_date_before] = Date.now.strftime("%d/%m/%Y") query += " #{params[:request_date_after]}..#{params[:request_date_before]}" @@ -424,10 +419,32 @@ class ApplicationController < ActionController::Base params[:request_date_after] = "01/01/2008" end if param_exists(:request_date_after) - query = "#{params[:request_date_after]}..#{params[:request_date_before]} " + query + query = " #{params[:request_date_after]}..#{params[:request_date_before]}" end - return query, sortby + return query + end + def get_tags_from_params + query = "" + tags = [] + if param_exists(:tags) + params[:tags].split().each do |tag| + tags << "tag:#{tag}" + end + end + if !tags.empty? + query = " (#{tags.join(' OR ')})" + end + return query + end + + def make_query_from_params + query = params[:query] || "" if query.nil? + query += get_date_range_from_params + query += get_request_variety_from_params + query += get_status_from_params + query += get_tags_from_params + return query end # URL generating functions are needed by all controllers (for redirects), -- cgit v1.2.3 From 33bd0e919528e1d7eda4be4200db91b042984c97 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Fri, 26 Aug 2011 13:54:05 +0100 Subject: Fixes to get tests to pass following addition of new search / filter functionality --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index caf613f8d..cb7f3b23d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -383,7 +383,7 @@ class ApplicationController < ActionController::Base params[:latest_status] = [params[:latest_status]] end if params[:latest_status].include?("recent") || params[:latest_status].include?("all") - query += " (variety:sent)" + query += " variety:sent" end if params[:latest_status].include? "successful" statuses << ['latest_status:successful', 'latest_status:partially_successful'] -- cgit v1.2.3 From b41edc7ae069e6071f7ff7223c1e60cca5e75e8c Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Mon, 29 Aug 2011 13:13:59 +0100 Subject: Add tests & fixes for new search/filtering functionality --- app/controllers/application_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cb7f3b23d..cb64cb922 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -413,10 +413,10 @@ class ApplicationController < ActionController::Base def get_date_range_from_params query = "" if param_exists(:request_date_after) && !param_exists(:request_date_before) - params[:request_date_before] = Date.now.strftime("%d/%m/%Y") + params[:request_date_before] = Time.now.strftime("%d/%m/%Y") query += " #{params[:request_date_after]}..#{params[:request_date_before]}" elsif !param_exists(:request_date_after) && param_exists(:request_date_before) - params[:request_date_after] = "01/01/2008" + params[:request_date_after] = "01/01/2001" end if param_exists(:request_date_after) query = " #{params[:request_date_after]}..#{params[:request_date_before]}" -- cgit v1.2.3 From a8d0c217e68fdac0331c0d80df511e5340a67fb7 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Tue, 30 Aug 2011 13:29:25 +0100 Subject: Present a reCaptcha on the signup form to foreign visitors (judging from their IP address). Fixes #157 (at least as a starter). --- app/controllers/application_controller.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cb64cb922..cae3cb213 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -447,6 +447,17 @@ class ApplicationController < ActionController::Base return query end + def country_from_ip + gaze = MySociety::Config.get('GAZE_URL', '') + default = MySociety::Config.get('ISO_COUNTRY_CODE', '') + country = "" + if !gaze.empty? + country = open("#{gaze}/gaze-rest?f=get_country_from_ip;ip=#{request.remote_ip}").read.strip + end + country = default if country.empty? + return country + end + # URL generating functions are needed by all controllers (for redirects), # views (for links) and mailers (for use in emails), so include them into # all of all. -- cgit v1.2.3 From 0420098e50996a033552335e94e35ade781357af Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Tue, 30 Aug 2011 13:32:13 +0100 Subject: Additional changes omitted from commit 9d8388c03d0faeaca29d233a340c58bd65f28a97 (distinguish 404s and 500s), fixes #161. --- app/controllers/application_controller.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cb64cb922..49b7fd7f1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,6 +8,7 @@ # # $Id: application.rb,v 1.59 2009-09-17 13:01:56 francis Exp $ +require 'open-uri' class ApplicationController < ActionController::Base # Standard headers, footers and navigation for whole site @@ -101,11 +102,17 @@ class ApplicationController < ActionController::Base # Make sure expiry time for session is set (before_filters are # otherwise missed by this override) session_remember_me - + case exception + when ActiveRecord::RecordNotFound, ActionController::UnknownAction, ActionController::RoutingError + @status = 404 + else + @status = 500 + end # Display user appropriate error message @exception_backtrace = exception.backtrace.join("\n") @exception_class = exception.class.to_s - render :template => "general/exception_caught.rhtml", :status => 404 + @exception_message = exception.message + render :template => "general/exception_caught.rhtml", :status => @status end # For development sites. -- cgit v1.2.3 From 204e5c3a739a2bedf927b2f6aa82c373731bbda8 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Sat, 3 Sep 2011 11:44:34 +0100 Subject: Store user's locale against profile, so we can send them localised track emails. Also internationalize more strings at the same time. Fixes #163. --- app/controllers/application_controller.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6d14d0d7a..a2a628f10 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,8 +14,14 @@ class ApplicationController < ActionController::Base # Standard headers, footers and navigation for whole site layout "default" include FastGettext::Translation # make functions like _, n_, N_ etc available) + + # Note: a filter stops the chain if it redirects or renders something + before_filter :authentication_check before_filter :set_gettext_locale + before_filter :check_in_post_redirect + before_filter :session_remember_me before_filter :set_vary_header + # scrub sensitive parameters from the logs filter_parameter_logging :password @@ -48,7 +54,14 @@ class ApplicationController < ActionController::Base else requested_locale = params[:locale] || session[:locale] || cookies[:locale] || I18n.default_locale end + requested_locale = FastGettext.best_locale_in(requested_locale) session[:locale] = FastGettext.set_locale(requested_locale) + if !@user.nil? + if @user.locale != requested_locale + @user.locale = session[:locale] + @user.save! + end + end end # scrub sensitive parameters from the logs @@ -85,7 +98,6 @@ class ApplicationController < ActionController::Base # Set cookie expiry according to "remember me" checkbox, as per "An easier # and more flexible hack" on this page: # http://wiki.rubyonrails.org/rails/pages/HowtoChangeSessionOptions - before_filter :session_remember_me def session_remember_me # Reset the "sliding window" session expiry time. if request.env['rack.session.options'] @@ -263,7 +275,6 @@ class ApplicationController < ActionController::Base end # If we are in a faked redirect to POST request, then set post params. - before_filter :check_in_post_redirect def check_in_post_redirect if params[:post_redirect] and session[:post_redirect_token] post_redirect = PostRedirect.find_by_token(session[:post_redirect_token]) @@ -272,7 +283,6 @@ class ApplicationController < ActionController::Base end # Default layout shows user in corner, so needs access to it - before_filter :authentication_check def authentication_check if session[:user_id] @user = authenticated_user -- cgit v1.2.3 From 7d7d8506021352b5b52a95528e63b2a61761c8f6 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Fri, 16 Sep 2011 13:16:30 +0100 Subject: Only store last visited public body for logged in users. Otherwise it breaks caching rather a lot, for little benefit. --- app/controllers/application_controller.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3794043fb..f34f6e388 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -358,14 +358,18 @@ class ApplicationController < ActionController::Base return (params[:page] || "1").to_i end - # Store last visited pages, for contact form + # Store last visited pages, for contact form; but only for logged in users, as otherwise this breaks caching def set_last_request(info_request) - session[:last_request_id] = info_request.id - session[:last_body_id] = nil + if !session[:user_id].nil? + session[:last_request_id] = info_request.id + session[:last_body_id] = nil + end end def set_last_body(public_body) - session[:last_request_id] = nil - session[:last_body_id] = public_body.id + if !session[:user_id].nil? + session[:last_request_id] = nil + session[:last_body_id] = public_body.id + end end def param_exists(item) -- cgit v1.2.3 From 992a0b8acc4556fa1687bdf7bc66d63a51d410b3 Mon Sep 17 00:00:00 2001 From: David Cabo Date: Tue, 20 Sep 2011 02:11:53 +0200 Subject: Ugly hack to fix _() in production when themes define controllers (kind of) --- app/controllers/application_controller.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f34f6e388..239145944 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,7 +14,7 @@ class ApplicationController < ActionController::Base # Standard headers, footers and navigation for whole site layout "default" include FastGettext::Translation # make functions like _, n_, N_ etc available) - + # Note: a filter stops the chain if it redirects or renders something before_filter :authentication_check before_filter :set_gettext_locale @@ -488,6 +488,20 @@ class ApplicationController < ActionController::Base # Site-wide access to configuration settings include ConfigHelper + + # XXX: patch to improve usability of gettext's _(), which by default accepts only + # one parameter. This is normally done in a monkey patch file named 'i18n_fixes.rb'. + # For some reason - and only when running in production -, after adding a new controller + # in a theme, the monkey patch in 'i18n_fixes.rb' doesn't seem to take effect. + # But it works just fine in the views. + # It's probably related to the loading order of classes, but including the + # monkey patch before or after the theme makes no difference. Even more bizarrely, + # require'ing or load'ing the patch file here doesn't work (!?), I need to redefine + # the method explicitely. I'm going crazy... + def _(key, options = {}) + translation = FastGettext._(key) || key + gettext_interpolate(translation, options) + end end -- cgit v1.2.3 From 7ee0ae2e506d1b50132264744da3f46b346998e1 Mon Sep 17 00:00:00 2001 From: David Cabo Date: Fri, 23 Sep 2011 02:17:55 +0200 Subject: Remove temporary patch, found a way of adding code from themes without new controller classes --- app/controllers/application_controller.rb | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 239145944..b7457c48e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -488,20 +488,6 @@ class ApplicationController < ActionController::Base # Site-wide access to configuration settings include ConfigHelper - - # XXX: patch to improve usability of gettext's _(), which by default accepts only - # one parameter. This is normally done in a monkey patch file named 'i18n_fixes.rb'. - # For some reason - and only when running in production -, after adding a new controller - # in a theme, the monkey patch in 'i18n_fixes.rb' doesn't seem to take effect. - # But it works just fine in the views. - # It's probably related to the loading order of classes, but including the - # monkey patch before or after the theme makes no difference. Even more bizarrely, - # require'ing or load'ing the patch file here doesn't work (!?), I need to redefine - # the method explicitely. I'm going crazy... - def _(key, options = {}) - translation = FastGettext._(key) || key - gettext_interpolate(translation, options) - end end -- cgit v1.2.3