<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
    @view = params[:view] || params[:view_type]
    @observable_list = observable_list(@list)
    handle_place_based_list(@observable_list) if place_based_list?(@observable_list)
    @unpaginated_listed_taxa ||= ListedTaxon.filter_by_list(@list.id)
    @allow_batch_adding = allow_batch_adding(@list, current_user)
    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

        if @q = params[:q]
          @search_taxon_ids = Taxon.search_for_ids(@q, :per_page => 1000)
          @find_options[:conditions] = update_conditions(
            @find_options[:conditions], ["AND listed_taxa.taxon_id IN (?)", @search_taxon_ids])
        end

        @taxon_names_by_taxon_id = set_taxon_names_by_taxon_id
        @iconic_taxon_counts = get_iconic_taxon_counts(@list, @iconic_taxa, @unpaginated_listed_taxa)
        @total_listed_taxa ||= @listed_taxa.count 
        @total_observed_taxa ||= @listed_taxa.confirmed.count
        @view = PHOTO_VIEW unless LIST_VIEWS.include?(@view)

        # preload all primary listed taxa. Would be nicer to do this in the
        # CheckListsController, but it needs to happen after set_scopes
        if @list.is_a?(CheckList)
          primary_listed_taxa = @listed_taxa.select(&:primary_listing)
          non_primary_listed_taxa = @listed_taxa - primary_listed_taxa
          primary_listed_taxa += ListedTaxon.
            where(:primary_listing => true, :place_id => @list.place_id).
            where("taxon_id IN (?)", non_primary_listed_taxa.map(&:taxon_id))
          @primary_listed_taxa_by_taxon_id = primary_listed_taxa.index_by(&:taxon_id)
        end

        case @view
        when TAXONOMIC_VIEW
          @unclassified = @listed_taxa.select {|lt| !lt.taxon.grafted? }
          @listed_taxa = @listed_taxa.delete_if {|lt| !lt.taxon.grafted? }
          ancestor_ids = @listed_taxa.map{|lt| lt.taxon.ancestor_ids[1..-1]}.flatten.uniq
          ancestors = Taxon.find_all_by_id(ancestor_ids, :include => :taxon_names)
          taxa_to_arrange = (ancestors + @listed_taxa.map(&:taxon)).sort_by{|t| "#{t.ancestry}/#{t.id}"}
          @arranged_taxa = Taxon.arrange_nodes(taxa_to_arrange)
          @listed_taxa_by_taxon_id = @listed_taxa.index_by(&:taxon_id)

        # Default to plain view
        else
          @grouped_listed_taxa = @listed_taxa.group_by do |lt|
            @iconic_taxa_by_id[lt.taxon.iconic_taxon_id]
          end
        end

        if job_id = Rails.cache.read("add_taxa_from_observations_job_#{@list.id}")
          @add_taxa_from_observations_job = Delayed::Job.find_by_id(job_id)
        end

        @listed_taxa_editble_by_current_user = @list.listed_taxa_editable_by?(current_user)
        @taxon_rule = @list.rules.detect{|lr| lr.operator == 'in_taxon?' && lr.operand.is_a?(Taxon)}

        if @list.show_obs_photos
          load_listed_taxon_photos
        end
        
        if logged_in?
          @current_user_lists = current_user.lists.all(:limit => 100)
        end

        if @representative_listed_taxon = @list.listed_taxa.order("listed_taxa.observations_count DESC").includes(:taxon => {:taxon_photos => :photo}).first
          @representative_photo = if @photos_by_listed_taxon_id && (p = @photos_by_listed_taxon_id[@representative_listed_taxon.id])
            p
          else
            @representative_listed_taxon.taxon.default_photo
          end
        end
      end
      
      format.csv do
        path_for_taxonomic_csv = "public/lists/#{@list.to_param}.taxonomic.csv"
        path_for_normal_csv = "public/lists/#{@list.to_param}.csv"
        if @list.listed_taxa.count < 1000
          path = if @view == "taxonomic"
            @list.generate_csv(:path => path_for_taxonomic_csv, :taxonomic => true)
          else
            @list.generate_csv(:path => path_for_normal_csv)
          end
          render :file => path
        else
          job_id = Rails.cache.read(@list.generate_csv_cache_key(:view => @view))
          job = Delayed::Job.find_by_id(job_id)
          if job
            # Still working
          else
            # no job id, no job, let's get this party started
            Rails.cache.delete(@list.generate_csv_cache_key(:view => @view))
            job = if @view == "taxonomic"
              @list.delay.generate_csv(:path => path_for_taxonomic_csv, :taxonomic => true)
            else
              @list.delay.generate_csv(:path => path_for_normal_csv)
            end
            Rails.cache.write(@list.generate_csv_cache_key(:view => @view), job.id, :expires_in => 1.hour)
          end
          prevent_caching
          render :status => :accepted, :text => "This file takes a little while to generate.  It should be ready shortly at #{request.url}"
        end
      end
      
      format.json do
        @find_options[:order] = "listed_taxa.observations_count DESC" if params[:order_by].blank?
        @listed_taxa ||= set_scopes
        @listed_taxa ||= @list.listed_taxa.paginate(@find_options)
        render :json => {<commit_msg>this is a better starting point<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
    @view = params[:view] || params[:view_type]
    @observable_list = observable_list(@list)
    handle_place_based_list(@observable_list) if place_based_list?(@observable_list)
    @unpaginated_listed_taxa ||= ListedTaxon.filter_by_list(@list.id)
    @allow_batch_adding = allow_batch_adding(@list, current_user)
    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

        if @q = params[:q]
          @search_taxon_ids = Taxon.search_for_ids(@q, :per_page => 1000)
          @find_options[:conditions] = update_conditions(
            @find_options[:conditions], ["AND listed_taxa.taxon_id IN (?)", @search_taxon_ids])
        end

        @listed_taxa ||= set_scopes

        @taxon_names_by_taxon_id = set_taxon_names_by_taxon_id
        @iconic_taxon_counts = get_iconic_taxon_counts(@list, @iconic_taxa, @unpaginated_listed_taxa)
        @total_listed_taxa ||= @listed_taxa.count 
        @total_observed_taxa ||= @listed_taxa.confirmed.count
        @view = PHOTO_VIEW unless LIST_VIEWS.include?(@view)

        # preload all primary listed taxa. Would be nicer to do this in the
        # CheckListsController, but it needs to happen after set_scopes
        if @list.is_a?(CheckList)
          primary_listed_taxa = @listed_taxa.select(&:primary_listing)
          non_primary_listed_taxa = @listed_taxa - primary_listed_taxa
          primary_listed_taxa += ListedTaxon.
            where(:primary_listing => true, :place_id => @list.place_id).
            where("taxon_id IN (?)", non_primary_listed_taxa.map(&:taxon_id))
          @primary_listed_taxa_by_taxon_id = primary_listed_taxa.index_by(&:taxon_id)
        end

        case @view
        when TAXONOMIC_VIEW
          @unclassified = @listed_taxa.select {|lt| !lt.taxon.grafted? }
          @listed_taxa = @listed_taxa.delete_if {|lt| !lt.taxon.grafted? }
          ancestor_ids = @listed_taxa.map{|lt| lt.taxon.ancestor_ids[1..-1]}.flatten.uniq
          ancestors = Taxon.find_all_by_id(ancestor_ids, :include => :taxon_names)
          taxa_to_arrange = (ancestors + @listed_taxa.map(&:taxon)).sort_by{|t| "#{t.ancestry}/#{t.id}"}
          @arranged_taxa = Taxon.arrange_nodes(taxa_to_arrange)
          @listed_taxa_by_taxon_id = @listed_taxa.index_by(&:taxon_id)

        # Default to plain view
        else
          @grouped_listed_taxa = @listed_taxa.group_by do |lt|
            @iconic_taxa_by_id[lt.taxon.iconic_taxon_id]
          end
        end

        if job_id = Rails.cache.read("add_taxa_from_observations_job_#{@list.id}")
          @add_taxa_from_observations_job = Delayed::Job.find_by_id(job_id)
        end

        @listed_taxa_editble_by_current_user = @list.listed_taxa_editable_by?(current_user)
        @taxon_rule = @list.rules.detect{|lr| lr.operator == 'in_taxon?' && lr.operand.is_a?(Taxon)}

        if @list.show_obs_photos
          load_listed_taxon_photos
        end
        
        if logged_in?
          @current_user_lists = current_user.lists.all(:limit => 100)
        end

        if @representative_listed_taxon = @list.listed_taxa.order("listed_taxa.observations_count DESC").includes(:taxon => {:taxon_photos => :photo}).first
          @representative_photo = if @photos_by_listed_taxon_id && (p = @photos_by_listed_taxon_id[@representative_listed_taxon.id])
            p
          else
            @representative_listed_taxon.taxon.default_photo
          end
        end
      end
      
      format.csv do
        path_for_taxonomic_csv = "public/lists/#{@list.to_param}.taxonomic.csv"
        path_for_normal_csv = "public/lists/#{@list.to_param}.csv"
        if @list.listed_taxa.count < 1000
          path = if @view == "taxonomic"
            @list.generate_csv(:path => path_for_taxonomic_csv, :taxonomic => true)
          else
            @list.generate_csv(:path => path_for_normal_csv)
          end
          render :file => path
        else
          job_id = Rails.cache.read(@list.generate_csv_cache_key(:view => @view))
          job = Delayed::Job.find_by_id(job_id)
          if job
            # Still working
          else
            # no job id, no job, let's get this party started
            Rails.cache.delete(@list.generate_csv_cache_key(:view => @view))
            job = if @view == "taxonomic"
              @list.delay.generate_csv(:path => path_for_taxonomic_csv, :taxonomic => true)
            else
              @list.delay.generate_csv(:path => path_for_normal_csv)
            end
            Rails.cache.write(@list.generate_csv_cache_key(:view => @view), job.id, :expires_in => 1.hour)
          end
          prevent_caching
          render :status => :accepted, :text => "This file takes a little while to generate.  It should be ready shortly at #{request.url}"
        end
      end
      
      format.json do
        @find_options[:order] = "listed_taxa.observations_count DESC" if params[:order_by].blank?
        set_scopes unless @listed_taxa.present?
        @listed_taxa ||= @list.listed_taxa.paginate(@find_options)
        render :json => {
{'fraction_non_alphanumeric': 0.11525214165857105, 'fraction_numerical': 0.0024728428861609114, 'mean_word_length': 2.780968280467446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '5510623', 'n_tokens_mistral': 3874, 'n_tokens_neox': 3754, 'n_words': 632}