<commit_before>module Shared::ListsModule    
  PLAIN_VIEW = 'plain'
  TAXONOMIC_VIEW = 'taxonomic'
  PHOTO_VIEW = 'photo'
  LIST_VIEWS = [PLAIN_VIEW, TAXONOMIC_VIEW, PHOTO_VIEW]
  
  ## RESTful actions ###########################################################
  def index
    if logged_in?
      redirect_to lists_by_login_path(current_user.login)
    else
      redirect_to :controller => 'welcome'
    end
  end
  
  def show
    @find_options = set_find_options
    @view = params[:view] || params[:view_type]
    @observable_list = observable_list(@list)
    @q = params[:q]
    @search_taxon_ids = set_search_taxon_ids(@q)
    if place_based_list?(@list)
      @place = set_place(@observable_list)
      @other_check_lists = set_other_check_lists(@observable_list, @place)
    end
    if place_based_list?(@observable_list) && @observable_list.is_default?
      observable_listed_taxa_query = handle_default_checklist_setup(@observable_list, @q, @search_taxon_ids) 
    end
    listed_taxa_query = ListedTaxon.filter_by_list(@list.id)

    if @q 
      @find_options[:conditions] = update_conditions(@find_options[:conditions], ["AND listed_taxa.taxon_id IN (?)", @search_taxon_ids])
    end

    main_list = build_main_query(@list, @filter_taxon, listed_taxa_query, observable_listed_taxa_query)

    if place_based_project_list?(@list)
      taxon_for_rule_filter = Taxon.find(@list.project.project_observation_rules.find{|por| por.operator == "in_taxon?"}.operand_id) rescue nil
      if taxon_for_rule_filter
        acceptable_taxa_from_list = taxon_for_rule_filter.descendant_ids + [taxon_for_rule_filter.id]
      else
        acceptable_taxa_from_list = nil
      end

      project_based_list = main_list.to_a
      unless @observed == 't' #don't add any listed taxa if not observed
        supplemental_list = set_scopes(@list, @filter_taxon, observable_listed_taxa_query)
        supplemental_list.each do |listed_taxon|
          project_based_list.push(listed_taxon) unless main_list.detect { |main_list_lt| 
            if acceptable_taxa_from_list
              main_list_lt.taxon_id.to_s == listed_taxon.taxon_id.to_s  || !acceptable_taxa_from_list.include?(listed_taxon.taxon_id.to_i)
            else
              main_list_lt.taxon_id.to_s == listed_taxon.taxon_id.to_s 
            end
          }
        end
        project_based_list = project_based_list.sort!{|a, b| b.observations_count <=> a.observations_count }
        if @rank == "leaves"
            project_based_list = project_based_list.inject(new_list = project_based_list) do |new_list, lt| 
            #remove all ancestors appearing on the list
            new_list.reject do |possible_ancestor| 
              lt.taxon_ancestor_ids.include?(possible_ancestor.taxon_id.to_s)
            end
            new_list
          end
        end
      end
      main_list = project_based_list 

      @total_listed_taxa = main_list.count
      @total_observed_taxa = main_list.count do |lt| 
        lt.last_observation && lt.list.type == "ProjectList"
      end
      @iconic_taxon_counts = get_iconic_taxon_counts_for_place_based_project(main_list)
    end

    # default_checklist_query = build_query(@list, observable_listed_taxa_query, @filter_taxon)
    @listed_taxa ||= main_list.paginate(@find_options) 

    respond_to do |format|
      format.html do
        # Make sure request is being handled by the right controller
        if @list.is_a?(CheckList) && params[:controller] != CheckList.to_s.underscore.pluralize
          return redirect_to @list
        end
        @allow_batch_adding = allow_batch_adding(@list, current_user)


        @taxon_names_by_taxon_id = set_taxon_names_by_taxon_id(@listed_taxa, @iconic_taxa, @taxa)
        @iconic_taxon_counts ||= get_iconic_taxon_counts(@list, @iconic_taxa, main_list)
        @total_listed_taxa ||= @listed_taxa.count
        @total_observed_taxa ||= @listed_taxa.confirmed.count
        @view = PHOTO_VIEW unless LIST_VIEWS.include?(@view)<commit_msg>reject non-leaves<commit_after>module Shared::ListsModule    
  PLAIN_VIEW = 'plain'
  TAXONOMIC_VIEW = 'taxonomic'
  PHOTO_VIEW = 'photo'
  LIST_VIEWS = [PLAIN_VIEW, TAXONOMIC_VIEW, PHOTO_VIEW]
  
  ## RESTful actions ###########################################################
  def index
    if logged_in?
      redirect_to lists_by_login_path(current_user.login)
    else
      redirect_to :controller => 'welcome'
    end
  end
  
  def show
    @find_options = set_find_options
    @view = params[:view] || params[:view_type]
    @observable_list = observable_list(@list)
    @q = params[:q]
    @search_taxon_ids = set_search_taxon_ids(@q)
    if place_based_list?(@list)
      @place = set_place(@observable_list)
      @other_check_lists = set_other_check_lists(@observable_list, @place)
    end
    if place_based_list?(@observable_list) && @observable_list.is_default?
      observable_listed_taxa_query = handle_default_checklist_setup(@observable_list, @q, @search_taxon_ids) 
    end
    listed_taxa_query = ListedTaxon.filter_by_list(@list.id)

    if @q 
      @find_options[:conditions] = update_conditions(@find_options[:conditions], ["AND listed_taxa.taxon_id IN (?)", @search_taxon_ids])
    end

    main_list = build_main_query(@list, @filter_taxon, listed_taxa_query, observable_listed_taxa_query)

    if place_based_project_list?(@list)
      taxon_for_rule_filter = Taxon.find(@list.project.project_observation_rules.find{|por| por.operator == "in_taxon?"}.operand_id) rescue nil
      if taxon_for_rule_filter
        acceptable_taxa_from_list = taxon_for_rule_filter.descendant_ids + [taxon_for_rule_filter.id]
      else
        acceptable_taxa_from_list = nil
      end

      project_based_list = main_list.to_a
      unless @observed == 't' #don't add any listed taxa if not observed
        supplemental_list = set_scopes(@list, @filter_taxon, observable_listed_taxa_query)
        supplemental_list.each do |listed_taxon|
          project_based_list.push(listed_taxon) unless main_list.detect { |main_list_lt| 
            if acceptable_taxa_from_list
              main_list_lt.taxon_id.to_s == listed_taxon.taxon_id.to_s  || !acceptable_taxa_from_list.include?(listed_taxon.taxon_id.to_i)
            else
              main_list_lt.taxon_id.to_s == listed_taxon.taxon_id.to_s 
            end
          }
        end
        project_based_list = project_based_list.sort!{|a, b| b.observations_count <=> a.observations_count }
      end
      if (@rank == "leaves" && @observed != 'f')
        ancestors_for_project_based_list = project_based_list.map{ |lt|
          lt.taxon_ancestor_ids.split('/')
        }.flatten.uniq
        project_based_list = project_based_list.reject do |lt| 
          found = false
          if !found
            found = ancestors_for_project_based_list.include?(lt.taxon_id.to_s)
          end
        end
      end
      main_list = project_based_list 

      @total_listed_taxa = main_list.count
      @total_observed_taxa = main_list.count do |lt| 
        lt.last_observation && lt.list.type == "ProjectList"
      end
      @iconic_taxon_counts = get_iconic_taxon_counts_for_place_based_project(main_list)
    end

    # default_checklist_query = build_query(@list, observable_listed_taxa_query, @filter_taxon)
    @listed_taxa ||= main_list.paginate(@find_options) 

    respond_to do |format|
      format.html do
        # Make sure request is being handled by the right controller
        if @list.is_a?(CheckList) && params[:controller] != CheckList.to_s.underscore.pluralize
          return redirect_to @list
        end
        @allow_batch_adding = allow_batch_adding(@list, current_user)


        @taxon_names_by_taxon_id = set_taxon_names_by_taxon_id(@listed_taxa, @iconic_taxa, @taxa)
        @iconic_taxon_counts ||= get_iconic_taxon_counts(@list, @iconic_taxa, main_list)
        @total_listed_taxa ||= @listed_taxa.count
        @total_observed_taxa ||= @listed_taxa.confirmed.count
        @view = PHOTO_VIEW unless LIST_VIEWS.include?(@view)
{'fraction_non_alphanumeric': 0.10288629012192088, 'fraction_numerical': 0.0, 'mean_word_length': 3.508693213684801, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1771817', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2701, 'n_words': 429}