Skip to content

Commit

Permalink
csv exporter ready, worker must be registered
Browse files Browse the repository at this point in the history
  • Loading branch information
chiramiso committed Mar 31, 2015
1 parent 94bc0fb commit c4d4a7a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 32 deletions.
9 changes: 9 additions & 0 deletions app/models/article/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ module Article::Scopes
articles.friendly_percent_organisation_id,
articles.transport_bike_courier')
}

scope :belboon_trackable, -> {
where('state = ? AND
condition = ? AND
fair = ? AND
ecologic = ? AND
small_and_precious = ?',
'active', 'new', false, false, false)
}
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class User < ActiveRecord::Base
####################################################
scope :sorted_ngo, -> { order(:nickname).where(ngo: true) }
scope :ngo_with_profile_image, -> { where(ngo: true).joins(:image).limit(6) }
scope :banned, -> { where(banned: true) }
scope :unbanned, -> { where('banned = ? OR banned IS NULL', false) }


####################################################
Expand Down
2 changes: 1 addition & 1 deletion app/models/users/legal_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LegalEntity < User
end

def commercial_seller_constants
commercial_seller_constants = {
{
standard_salesvolume: $commercial_seller_constants['standard_salesvolume'],
verified_bonus: $commercial_seller_constants['verified_bonus'],
good_factor: $commercial_seller_constants['good_factor'],
Expand Down
101 changes: 70 additions & 31 deletions app/objects/service/belboon_article_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
class BelboonArticleExporter
@@csv_options = { col_sep: ";", encoding: 'utf-8'}
@@csv_options = { col_sep: ";", encoding: 'utf-8' }

EXPORT_MAPPING = {
'Merchant_ProductNumber' => 'slug',
'EAN_Code' => 'gtin',
'Product_Title' => 'title',
'Manufacturer' => '',
'Brand' => '',
'Price' => 'price_cents',
'Price_old' => '',
'Currency' => '',
'Valid_From' => '',
'Valid_To' => '',
'DeepLink_URL' => '',
'Into_Basket_URL' => '',
'Image_Small_URL' => '',
'Image_Small_HEIGHT' => '',
'Image_Small_WIDTH' => '',
'Image_Large_URL' => '',
'Image_Large_HEIGHT' => '',
'Image_Large_WIDTH' => '',
'Merchant_Product_Category' => '',
'Keywords' => '',
'Product_Description_Short' => '',
'Product_Description_Long' => 'description',
'Last_Update' => '',
'Shipping' => '',
'Availability' => '',
'Optional_1' => '',
'Optional_2' => '',
'Optional_3' => '',
'Optional_4' => '',
'Optional_5' => ''
'Manufacturer' => nil,
'Brand' => nil,
'Price' => lambda { |article| Money.new(article.price_cents).to_s.gsub(',', '.') },
'Price_old' => nil,
'Currency' => "'EUR'",
'Valid_From' => nil,
'Valid_To' => nil,
'DeepLink_URL' => lambda { |article| "https://www.fairmondo.de/articles/#{ article.slug }" },
'Into_Basket_URL' => nil,
'Image_Small_URL' => lambda { |article| thumb_image_path_for article },
'Image_Small_HEIGHT' => "'200'",
'Image_Small_WIDTH' => "'280'",
'Image_Large_URL' => lambda { |article| medium_image_path_for article },
'Image_Large_HEIGHT' => "'360'",
'Image_Large_WIDTH' => "'520'",
'Keywords' => nil,
'Merchant_Product_Category' => nil,
'Product_Description_Short' => nil,
'Product_Description_Long' => 'content',
'Last_Update' => 'updated_at',
'Shipping' => nil,
'Availability' => "'sofort lieferbar'",
'Optional_1' => nil,
'Optional_2' => nil,
'Optional_3' => nil,
'Optional_4' => nil,
'Optional_5' => nil
}

EXPORT_HEADER = [
Expand All @@ -44,9 +44,48 @@ class BelboonArticleExporter
'Optional_1', 'Optional_2', 'Optional_3', 'Optional_4', 'Optional_5'
]

def self.export(csv, user)
csv.puts CSV.generate_line(EXPORT_HEADER, @@csv_options)
FILE_NAME = Rails.env == 'production' ? \
'/var/www/fairnopoly/shared/public/fairmondo_articles.csv' : \
"#{ Rails.root }/public/fairmondo_articles.csv"

def self.export(user)
CSV.open(FILE_NAME, 'wb', @@csv_options) do |line|
line << EXPORT_HEADER

export_articles(user).find_each do |article|
line << line_for(article)
end
end

csv.flush
end

private

def self.line_for article
attrs = []
EXPORT_HEADER.each do |attr|
if !EXPORT_MAPPING[attr].present?
attrs << EXPORT_MAPPING[attr]
elsif article.respond_to? EXPORT_MAPPING[attr]
attrs << "'#{ article.send(EXPORT_MAPPING[attr]) }'"
elsif EXPORT_MAPPING[attr].class != String && EXPORT_MAPPING[attr].lambda?
attrs << "'#{ EXPORT_MAPPING[attr].call(article) }'"
else
attrs << EXPORT_MAPPING[attr]
end
end
attrs
end

def self.export_articles user
user.articles.belboon_trackable.includes(:images)
end

def self.thumb_image_path_for article
"https://www.fairmondo.de/#{ article.title_image_url :thumb }"
end

def self.medium_image_path_for article
"https://www.fairmondo.de/#{ article.title_image_url :medium }"
end
end
1 change: 1 addition & 0 deletions app/objects/service/line_item_group_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# csv.flush
# end
#
# TODO refactor, does not seem to work
# def self.kind_of_address(obj, lig)
# option = nil
# if obj == lig.payment_address
Expand Down
19 changes: 19 additions & 0 deletions app/workers/belboon_article_exporter_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class BelboonArticleExporterWorker
include Sidekiq::Worker
include Sidetiq::Schedulable

recurrance { weekly.day_of_week(1).hour_of_day(2) }

sidekiq_options queue: :sidekiq_pro,
retry: 5,
backtrace: true

IDS = [1020]

def perform
IDS.each do |id|
user = User.find id
BelboonArticleExporter.export(user)
end
end
end

0 comments on commit c4d4a7a

Please sign in to comment.