<commit_before># This model is the master routine for uploading products
# Requires Paperclip and CSV to upload the CSV file and read it nicely.

# Original Author:: <NAME>
# Author:: <NAME>
# License:: MIT

class ProductImport < ActiveRecord::Base
  has_attached_file :data_file, :path => ":rails_root/lib/etc/product_data/data-files/:basename.:extension"
  validates_attachment_presence :data_file
  
  require 'csv'
  require 'pp'
  
  ## Data Importing:
  # Supplier, room and category are all taxonomies to be found (or created) and associated
  # Model maps to product name, description, brochure text and bullets 1 - 8 are combined to form description
  # List Price maps to Master Price, Current MAP to Cost Price, Net 30 Cost unused
  # Width, height, Depth all map directly to object
  # Image main is created independtly, then each other image also created and associated with the product
  # Meta keywords and description are created on the product model
  
  def import_data
    begin
      #Get products *before* import - 
      @products_before_import = Product.all
      @names_of_products_before_import = []
      @products_before_import.each do |product|
        @names_of_products_before_import << product.name
      end
      log("#{@names_of_products_before_import}")

      columns = ImportProductSettings::COLUMN_MAPPINGS
      rows = CSV.read(self.data_file.path)
      log("Importing products for #{self.data_file_file_name} began at #{Time.now}")
      rows[ImportProductSettings::INITIAL_ROWS_TO_SKIP..-1].each do |row|
        product_information = {}
        
        #Easy ones first
        product_information[:sku] = row[columns['SKU']]
        product_information[:name] = row[columns['Name']]
        product_information[:price] = row[columns['Master Price']]
        product_information[:cost_price] = row[columns['Cost Price']]
        product_information[:available_on] = DateTime.now - 1.day #Yesterday to make SURE it shows up
        product_information[:weight] = row[columns['Weight']]
        product_information[:height] = row[columns['Height']]
        product_information[:depth] = row[columns['Depth']]
        product_information[:width] = row[columns['Width']]
        product_information[:description] = columns['Description']
        

        #Create the product skeleton - should be valid
        product_obj = Product.new(product_information)
        unless product_obj.valid?
          log("A product could not be imported - here is the information we have:\n #{ pp product_information}", :error)
          next
        end
        <commit_msg>fixed bug 1. description was NOT being picked properly from CSV file<commit_after># This model is the master routine for uploading products
# Requires Paperclip and CSV to upload the CSV file and read it nicely.

# Original Author:: <NAME>
# Author:: <NAME>
# License:: MIT

class ProductImport < ActiveRecord::Base
  has_attached_file :data_file, :path => ":rails_root/lib/etc/product_data/data-files/:basename.:extension"
  validates_attachment_presence :data_file
  
  require 'csv'
  require 'pp'
  
  ## Data Importing:
  # Supplier, room and category are all taxonomies to be found (or created) and associated
  # Model maps to product name, description, brochure text and bullets 1 - 8 are combined to form description
  # List Price maps to Master Price, Current MAP to Cost Price, Net 30 Cost unused
  # Width, height, Depth all map directly to object
  # Image main is created independtly, then each other image also created and associated with the product
  # Meta keywords and description are created on the product model
  
  def import_data
    begin
      #Get products *before* import - 
      @products_before_import = Product.all
      @names_of_products_before_import = []
      @products_before_import.each do |product|
        @names_of_products_before_import << product.name
      end
      log("#{@names_of_products_before_import}")

      columns = ImportProductSettings::COLUMN_MAPPINGS
      rows = CSV.read(self.data_file.path)
      log("Importing products for #{self.data_file_file_name} began at #{Time.now}")
      rows[ImportProductSettings::INITIAL_ROWS_TO_SKIP..-1].each do |row|
        product_information = {}
        
        #Easy ones first
        product_information[:sku] = row[columns['SKU']]
        product_information[:name] = row[columns['Name']]
        product_information[:price] = row[columns['Master Price']]
        product_information[:cost_price] = row[columns['Cost Price']]
        product_information[:available_on] = DateTime.now - 1.day #Yesterday to make SURE it shows up
        product_information[:weight] = row[columns['Weight']]
        product_information[:height] = row[columns['Height']]
        product_information[:depth] = row[columns['Depth']]
        product_information[:width] = row[columns['Width']]
        product_information[:description] = row[columns['Description']]
        

        #Create the product skeleton - should be valid
        product_obj = Product.new(product_information)
        unless product_obj.valid?
          log("A product could not be imported - here is the information we have:\n #{ pp product_information}", :error)
          next
        end
        
{'fraction_non_alphanumeric': 0.09258555133079847, 'fraction_numerical': 0.00247148288973384, 'mean_word_length': 3.562879444926279, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 9, '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': '5601749', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1417, 'n_words': 447}