/
add_product.rb
97 lines (87 loc) · 3.14 KB
/
add_product.rb
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
class Pritory < Sinatra::Base
# Add product
get '/add_product' do
protected
user = User.first(username: session['name'])
r = user.setting.nil? rescue true
if r
flash[:error] = "#{t 'error_no_country'}"
redirect "/settings"
else
@vats = Vat.where(country: user.setting.country).select_map(:vat)
@products = user.products
haml :add_product
end
end
# Post product
post '/add_product' do
protected
user = User.first(username: session['name'])
img = params['image']
if params['name'].empty?
flash[:error] = "#{t 'error_product_name'}"
redirect '/panel'
end
if params['tags'].empty?
flash[:error] = "#{t 'error_product_tag'}"
redirect '/panel'
end
begin
a = Product.create(
user_id: user.id,
vat_category: params['vat_category'].to_f,
name: params['name'],
cost: MyHelpers.euro_to_cents(params['cost']),
notes: params['comment'],
created_at: TZInfo::Timezone.get('Europe/Athens').now
)
# Associate tags to products
params['tags'].split(',').each do |t|
a.add_tag(name: t.strip)
end
# Add source to storename
a.add_source(
name: user.setting.storename,
price: MyHelpers.euro_to_cents(params['price']),
created_at: TZInfo::Timezone.get('Europe/Athens').now
)
if img
filename = params['image'][:filename]
images_dir = "public/images/users/#{user.id}/products/"
FileUtils::mkdir_p images_dir unless File.directory?(images_dir)
# Check for overwrites - DOESNT WORK
#
# if images.include? "public/images/#{user.id}/products/#{filename}"
# raise ArgumentError.new("Παρακαλώ αλλάξτε όνομα στην εικόνα!")
# end
image_path = images_dir + params['image'][:filename]
File.open(image_path, "w") do |f|
f.write(params['image'][:tempfile].read)
end
# Convert all images to 150px height for the time being
# NOTE: Fore more complex processing try: https://github.com/markevans/dragonfly
# I believe this processing is considerably easy/fast even for underpowered servers,
# If further processing on images is needed it can be using sidekiq_async in the background.
process_image = MiniMagick::Image.open(image_path)
process_image.resize "150x150"
process_image.write image_path
a.update(img_url: params['image'][:filename])
end
# NOTE: we could use a partial here to make it modular
if params['squick'] == 'yes'
redirect "/skroutz_add/:#{params['name']}"
else
redirect "/panel"
flash[:success] = "Το προϊόν προστέθηκε στην βάση δεδομένων"
end
rescue Sequel::Error => e
flash[:error] = "#{t 'error_sql_random'}"
settings.log.error("ERROR SQL [x1]: #{e}")
redirect "/add_product"
rescue ArgumentError => e
flash[:error] = "ArgumentError occured!"
settings.log.error("ERROR ArgumentError: #{e}")
redirect "/add_product"
end
end
end