-
Notifications
You must be signed in to change notification settings - Fork 104
/
products_images.thor
160 lines (102 loc) · 6.54 KB
/
products_images.thor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright:: (c) Autotelik Media Ltd 2012
# Author :: Tom Statter
# Date :: March 2012
# License:: MIT. Free, Open Source.
#
# Usage::
# bundle exec thor help datashift:spree
# bundle exec thor datashift:spree:products -i db/datashift/MegamanFozz20111115_load.xls -s 299S_
#
# bundle exec thor datashift:spree:images -i db/datashift/imagebank -s -p 299S_
#
# Note, not DataShift, case sensitive, create namespace for command line : datashift
require 'datashift_spree'
require 'spree_helper'
module DatashiftSpree
class Load < Thor
include DataShift::Logging
desc "products", "Populate Spree Product/Variant data from .xls (Excel) or CSV file"
method_option :input, :aliases => '-i', :required => true, :desc => "The import file (.xls or .csv)"
method_option :sku_prefix, :aliases => '-s', :desc => "Prefix to add to each SKU before saving Product"
method_option :image_path_prefix, :aliases => '-p', :desc => "Prefix to add to image path for importing from disk"
method_option :verbose, :aliases => '-v', :type => :boolean, :desc => "Verbose logging"
method_option :config, :aliases => '-c', :type => :string, :desc => "Configuration file containg defaults or over rides in YAML"
method_option :dummy, :aliases => '-d', :type => :boolean, :desc => "Dummy run, do not actually save Image or Product"
def products()
# TODO - We're assuming run from a rails app/top level dir...
# ...can we make this more robust ? e.g what about when using active record but not in Rails app,
require File.expand_path('config/environment.rb')
input = options[:input]
require 'product_loader'
loader = DataShift::SpreeHelper::ProductLoader.new( nil, {:verbose => options[:verbose]})
# YAML configuration file to drive defaults etc
if(options[:config])
raise "Bad Config - Cannot find specified file #{options[:config]}" unless File.exists?(options[:config])
loader.configure_from( options[:config] )
else
loader.populator.set_default_value('available_on', Time.now.to_s(:db) )
loader.populator.set_default_value('cost_price', 0.0 )
loader.populator.set_default_value('price', 0.0 )
end
loader.set_prefix('sku', options[:sku_prefix] ) if(options[:sku_prefix])
puts "DataShift::Product starting upload from file: #{input}"
opts = options.dup
opts[:mandatory] = ['sku', 'name', 'price']
loader.perform_load(input, opts)
end
desc "attach_images", "Populate Products with images from Excel/CSV\nProvide column SKU or Name\nColumn containing full path to image can be named 'attachment', 'images' or 'path' "
# :dummy => dummy run without actual saving to DB
method_option :input, :aliases => '-i', :required => true, :desc => "The 2 column import file (.xls or .csv)"
def attach_images()
require File.expand_path('config/environment.rb')
require 'image_loader'
loader = DataShift::SpreeHelper::ImageLoader.new(nil, options)
loader.perform_load( options[:input], options )
end
# => thor datashift:spree:images input=vendor/extensions/site/fixtures/images
#
desc "images", "Populate the DB with images from a directory
The image name, must contain the Product Sku somewhere within it.
N.B Currently only lookup on SKU available - more flexability coming soon"
method_option :input, :aliases => '-i', :required => true, :desc => "The input path containing images "
method_option :glob, :aliases => '-g', :desc => 'The glob to use to find files e.g. \'{*.jpg,*.gif,*.png}\' '
method_option :recursive, :aliases => '-r', :type => :boolean, :desc => "Scan sub directories of input for images"
method_option :find_by_field, :desc => "TODO - Find Variant/Product based on any field"
method_option :sku_prefix, :aliases => '-s', :desc => "SKU prefix to add to each image name before attempting Product lookup"
method_option :dummy, :aliases => '-d', :type => :boolean, :desc => "Dummy run, do not actually save Image or Product"
method_option :process_when_no_assoc, :aliases => '-f', :type => :boolean, :desc => "Process image even if no Product found - force loading"
method_option :skip_when_assoc, :aliases => '-x', :type => :boolean, :desc => "DO not process image if Product already has image"
method_option :verbose, :aliases => '-v', :type => :boolean, :desc => "Verbose logging"
method_option :config, :aliases => '-c', :type => :string, :desc => "Configuration file for Image Loader in YAML"
method_option :split_file_name_on, :type => :string, :desc => "delimiter to progressivley split filename for Prod lookup", :default => "_"
method_option :case_sensitive, :type => :boolean, :desc => "Use case sensitive where clause to find Product"
method_option :use_like, :type => :boolean, :desc => "Use sku/name LIKE 'string%' instead of sku/name = 'string' in where clauses to find Product"
def images()
@attachment_path = options[:input]
unless(File.exists?(@attachment_path))
puts "ERROR: Supplied Path [#{@attachment_path}] not accesible"
exit(-1)
end
require File.expand_path('config/environment.rb')
require 'paperclip/attachment_loader'
image_klass = DataShift::SpreeHelper::get_spree_class('Image' )
raise "Cannot find suitable Paperclip Attachment Class" unless image_klass
loader_options = { :verbose => true }
owner_klass = DataShift::SpreeHelper::product_attachment_klazz
loader_options[:attach_to_klass] = owner_klass # Pass in real Ruby class not string class name
# WTF ... this works in the specs but thor gives me
# products_images.thor:131:in `images': uninitialized constant Thor::Sandbox::Datashift::Spree::Variant (NameError)
# loader_options[:attach_to_find_by_field] = (owner_klass. == Spree::Variant) ? :sku : :name
# so for now just sku lookup available .... TOFIX - name wont currently work for Variant and sku won't work for Product
# so need way to build a where clause or add scopes to Variant/Product
loader_options[:attach_to_find_by_field] = :sku
loader_options[:attach_to_field] = 'images'
loader = DataShift::Paperclip::AttachmentLoader.new(image_klass, true, nil, loader_options)
logger.info "Loading attachments from #{@attachment_path}"
attach_options = options.dup
attach_options[:add_prefix] = options[:sku_prefix]
puts "Setting prefix to [#{attach_options[:add_prefix]}]"
loader.process_from_filesystem(@attachment_path, attach_options.dup)
end
end
end