<commit_before># Copyright (c) 2008-2013 <NAME> and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class AccountsController < EntitiesController
  before_filter :get_data_for_sidebar, :only => :index

  # GET /accounts
  #----------------------------------------------------------------------------
  def index
    @accounts = get_accounts(:page => params[:page], :per_page => params[:per_page])

    respond_with @accounts do |format|
      format.xls { render :layout => 'header' }
      format.csv { render :csv => @accounts }
    end
  end

  # GET /accounts/1
  # AJAX /accounts/1
  #----------------------------------------------------------------------------
  def show
    @stage = Setting.unroll(:opportunity_stage)
    @comment = Comment.new
    @timeline = timeline(@account)
    respond_with(@account)
  end

  # GET /accounts/new
  #----------------------------------------------------------------------------
  def new
    @account.attributes = {:user => current_user, :access => Setting.default_access, :assigned_to => nil}

    if params[:related]
      model, id = params[:related].split('_')
      instance_variable_set("@#{model}", model.classify.constantize.find(id))
    end
    respond_with(@account)
  end

  # GET /accounts/1/edit                                                   AJAX
  #----------------------------------------------------------------------------
  def edit
    if params[:previous].to_s =~ /(\d+)\z/
      @previous = Account.my.find_by_id($1) || $1.to_i
    end

    respond_with(@account)
  end

  # POST /accounts
  #----------------------------------------------------------------------------
  def create
    @comment_body = params[:comment_body]
    
    respond_with(@account) do |format|
      if @account.save
        @account.add_comment_by_user(@comment_body, current_user)
        # None: account can only be created from the Accounts index page, so we
        # don't have to check whether we're on the index page.
        @accounts = get_accounts
        get_data_for_sidebar
      end
    end
  end
  
  # POST /accounts (many)
  #----------------------------------------------------------------------------
  def create_many
    data = {      
      :user_id => current_user.id,
      :assigned_to => current_user.id,
      :category => params[:account][:category],
      :subscribed_users => nil,
      :name => nil,
      :email => nil,
      :codename => nil,
      :birth => nil
    }

    file_data = params[:account][:customers]
    if file_data.respond_to?(:read)
      file_contents = file_data.read
    elsif file_data.respond_to?(:path)
      file_contents = File.read(file_data.path)
    else
      logger.error "Bad file_data: #{file_data.class.name}: #{file_data.inspect}"
    end
    head = true
    saved = 0
    CSV.parse(file_contents) do |row|
      puts row.inspect
      if head
        headers = row.split(',')[0].map{ |x| x.downcase }
        head = false
      else
        row = row.split(',')[0]
        data[:name] = "#{row[0].strip.capitalize} #{row[1].strip.capitalize}"
        data[:email] = "#{row[4].strip.downcase}"  
        data[:codename] = "#{row[6].strip.upcase}"
        data[:birth]= "#{row[3].strip}"
        next if /[Tt]est/.match(data[:name])
        @account = Account.new()
        @account.assign_attributes(data)
        puts account.inspect
        if @account.save
          saved += 1
        end
        puts "arrival"
      end
    end    
    flash.notice = "#{saved} accounts uploaded!"
    redirect_to action: 'index'
  end
  
  # PUT /accounts/1
  #----------------------------------------------------------------------------
  def update
    respond_with(@account) do |format|
      # Must set access before user_ids, because user_ids= method depends on access value.
      @account.access = params[:account][:access] if params[:account][:access]
      get_data_for_sidebar if @account.update_attributes(params[:account])<commit_msg>debugging production bulk add: found the mistake<commit_after># Copyright (c) 2008-2013 <NAME> and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class AccountsController < EntitiesController
  before_filter :get_data_for_sidebar, :only => :index

  # GET /accounts
  #----------------------------------------------------------------------------
  def index
    @accounts = get_accounts(:page => params[:page], :per_page => params[:per_page])

    respond_with @accounts do |format|
      format.xls { render :layout => 'header' }
      format.csv { render :csv => @accounts }
    end
  end

  # GET /accounts/1
  # AJAX /accounts/1
  #----------------------------------------------------------------------------
  def show
    @stage = Setting.unroll(:opportunity_stage)
    @comment = Comment.new
    @timeline = timeline(@account)
    respond_with(@account)
  end

  # GET /accounts/new
  #----------------------------------------------------------------------------
  def new
    @account.attributes = {:user => current_user, :access => Setting.default_access, :assigned_to => nil}

    if params[:related]
      model, id = params[:related].split('_')
      instance_variable_set("@#{model}", model.classify.constantize.find(id))
    end
    respond_with(@account)
  end

  # GET /accounts/1/edit                                                   AJAX
  #----------------------------------------------------------------------------
  def edit
    if params[:previous].to_s =~ /(\d+)\z/
      @previous = Account.my.find_by_id($1) || $1.to_i
    end

    respond_with(@account)
  end

  # POST /accounts
  #----------------------------------------------------------------------------
  def create
    @comment_body = params[:comment_body]
    
    respond_with(@account) do |format|
      if @account.save
        @account.add_comment_by_user(@comment_body, current_user)
        # None: account can only be created from the Accounts index page, so we
        # don't have to check whether we're on the index page.
        @accounts = get_accounts
        get_data_for_sidebar
      end
    end
  end
  
  # POST /accounts (many)
  #----------------------------------------------------------------------------
  def create_many
    data = {      
      :user_id => current_user.id,
      :assigned_to => current_user.id,
      :category => params[:account][:category],
      :subscribed_users => nil,
      :name => nil,
      :email => nil,
      :codename => nil,
      :birth => nil
    }

    file_data = params[:account][:customers]
    if file_data.respond_to?(:read)
      file_contents = file_data.read
    elsif file_data.respond_to?(:path)
      file_contents = File.read(file_data.path)
    else
      logger.error "Bad file_data: #{file_data.class.name}: #{file_data.inspect}"
    end
    head = true
    saved = 0
    CSV.parse(file_contents) do |row|
      puts row.inspect
      if head
        headers = row.map{ |x| x.downcase }
        head = false
      else
        data[:name] = "#{row[0].strip.capitalize} #{row[1].strip.capitalize}"
        data[:email] = "#{row[4].strip.downcase}"  
        data[:codename] = "#{row[6].strip.upcase}"
        data[:birth]= "#{row[3].strip}"
        next if /[Tt]est/.match(data[:name])
        @account = Account.new()
        @account.assign_attributes(data)
        puts account.inspect
        if @account.save
          saved += 1
        end
        puts "arrival"
      end
    end    
    flash.notice = "#{saved} accounts uploaded!"
    redirect_to action: 'index'
  end
  
  # PUT /accounts/1
  #----------------------------------------------------------------------------
  def update
    respond_with(@account) do |format|
      # Must set access before user_ids, because user_ids= method depends on access value.
      @account.access = params[:account][:access] if params[:account][:access]
      get_data_for_sidebar if @account.update_attributes(params[:account])
{'fraction_non_alphanumeric': 0.2551599275799638, 'fraction_numerical': 0.005310802655401328, 'mean_word_length': 1.5845290081097942, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1553874', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2280, 'n_words': 531}