add filters for excluding home projects and debuginfo packages from search results
[opensuse:software-o-o.git] / app / controllers / application_controller.rb
1 # Filters added to this controller apply to all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3
4 class ApplicationController < ActionController::Base
5
6   before_filter :set_distributions
7   before_filter :set_language
8   helper :all # include all helpers, all the time
9
10   init_gettext('software')
11
12   def rescue_action_in_public(exception)
13     @message = exception.message
14     if request.xhr?
15       render :template => "error", :layout => false, :status => 400
16     else
17       render :template => 'error', :layout => "application", :status => 400
18     end
19   end
20
21   private
22
23   def set_language
24     if params[:lang].nil?
25       lang = request.compatible_language_from(LANGUAGES) || "en"
26     else
27       lang = params[:lang][0].gsub(/\-/, '_')
28     end
29     @lang = lang
30     GetText.locale = lang
31   end
32
33
34   def set_distributions
35     @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
36       load_distributions
37     end
38   end
39
40
41   # load available distributions
42   def load_distributions
43     uri = URI.parse("http://#{API_HOST}/distributions")
44     request = Net::HTTP::Get.new(uri.path)
45     logger.debug "Loading distributions from #{uri}"
46     @distributions = Array.new
47     begin
48       Net::HTTP.start(uri.host, uri.port) do |http|
49         http.read_timeout = 30
50         response = http.request(request)
51         unless( response.kind_of? Net::HTTPSuccess )
52           logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
53         else
54           doc = REXML::Document.new response.body
55           doc.elements.each("distributions/distribution") { |element|
56             dist = [element.elements['name'].text, element.elements['project'].text]
57             @distributions << dist
58           }
59         end
60       end
61       @distributions << ["ALL", 'ALL']
62       return @distributions
63     rescue Exception => e
64       logger.error "Error while loading distributions from '#{uri}': " + e.to_s
65     end
66     return nil
67   end
68
69
70 end