Skip to content

Commit

Permalink
refs #1098, replaces mass_upload finished notify by email
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Jurke committed Nov 27, 2014
1 parent daf01b4 commit 454d4a8
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 28 deletions.
4 changes: 0 additions & 4 deletions app/controllers/mass_uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,5 @@ def send_emails_for mass_upload
if mass_upload.articles_for_mass_activation.any?
ArticleMailer.delay.mass_upload_activation_message(mass_upload.id)
end

if mass_upload.deleted_articles.any?
ArticleMailer.delay.mass_upload_deletion_message(mass_upload.id)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def show_notice

def check_for_complete_mass_uploads
if user_signed_in?
current_user.mass_uploads.where(:state => :processing).each do |mu|
current_user.mass_uploads.processing.each do |mu|
mu.finish
end
end
Expand Down
21 changes: 19 additions & 2 deletions app/mailers/article_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,27 @@ def mass_upload_activation_message mass_upload_id
mail(to: @user.email, subject: "[Fairmondo] Du hast Deine per CSV-Dateien eingestellten Artikel aktiviert")
end

def mass_upload_deletion_message mass_upload_id
def mass_upload_failed_message mass_upload_id
@mass_upload = MassUpload.find mass_upload_id
@user = @mass_upload.user
mail(to: @user.email, subject: "[Fairmondo] Du hast Artikel per CSV-Datei gelöscht")
terms_pdf
mail(to: @user.email, subject: "[Fairmondo] Bei deinem CSV-Upload sind Fehler aufgetreten")
end

def mass_upload_finished_message mass_upload_id
@mass_upload = MassUpload.find mass_upload_id
@user = @mass_upload.user
terms_pdf
subject = "[Fairmondo] Dein CSV-Upload ist abgeschlossen"
if @mass_upload.articles_for_mass_activation.any?
subject << ". Es liegen Artikel zur Aktivierung bereit!"
@created_count = @mass_upload.created_articles.count
@updated_count = @mass_upload.updated_articles.count
@activated_count = @mass_upload.activated_articles.count
end
@deleted_count = @mass_upload.deleted_articles.count
@deactivated_count = @mass_upload.deactivated_articles.count
mail(to: @user.email, subject: subject)
end

private
Expand Down
13 changes: 8 additions & 5 deletions app/models/mass_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ class MassUpload < ActiveRecord::Base
end

after_transition :to => :finished do |mass_upload,transition|
mass_upload.user.notify I18n.t('mass_uploads.labels.finished'), Rails.application.routes.url_helpers.mass_upload_path(mass_upload)
ArticleMailer.delay.mass_upload_finished_message(mass_upload.id)
end

after_transition :processing => :failed do |mass_upload, transition|
mass_upload.failure_reason = transition.args.first
mass_upload.save
mass_upload.user.notify I18n.t('mass_uploads.labels.failed'), Rails.application.routes.url_helpers.user_path(mass_upload.user, anchor: "my_mass_uploads"), :error
ArticleMailer.delay.mass_upload_failed_message(mass_upload.id)
end
end

scope :processing, -> { where(state: :processing) }

include Checks, Questionnaire, FeesAndDonations

has_many :mass_upload_articles
Expand All @@ -60,9 +62,11 @@ class MassUpload < ActiveRecord::Base
has_many :deactivated_articles, -> { where('mass_upload_articles.action' => 'deactivate') }, through: :mass_upload_articles, source: :article
has_many :activated_articles, -> { where('mass_upload_articles.action' => 'activate') }, through: :mass_upload_articles, source: :article
has_many :articles_for_mass_activation, -> { where("mass_upload_articles.action IN ('create', 'update', 'activate')") } , through: :mass_upload_articles, source: :article
has_many :skipped_articles, -> { where('mass_upload_articles.action' => 'nothing') }, through: :mass_upload_articles, source: :article

has_many :valid_mass_upload_articles, -> { where(validation_errors: nil).where.not(article_id: nil) }, class_name: 'MassUploadArticle'
has_many :erroneous_articles, -> { where.not(validation_errors: nil) }, class_name: 'MassUploadArticle'

has_many :erroneous_articles, -> { where("validation_errors IS NOT NULL") }, class_name: 'MassUploadArticle'
has_attached_file :file
belongs_to :user

Expand Down Expand Up @@ -105,9 +109,8 @@ def self.article_attributes
"gtin", "custom_seller_identifier", "action"]
end


def processed_articles_count
self.mass_upload_articles.where("article_id IS NOT NULL").count + self.erroneous_articles.count
self.valid_mass_upload_articles.count + self.erroneous_articles.count
end

def process
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
h3
= t('email.greeting', name: @mass_upload.user_nickname)
= t('email.article.mass_deletion.text')
= t('email.article.mass_upload_finished.failure')
p
= t('email.article.mass_deletion.to_csv')
= link_to @mass_upload.file_file_name, mass_upload_url(@mass_upload)
br
= t('email.close')

.pdf
= link_to 'Unsere AGB', "#{root_url}/assets/AGB.pdf"
br
= link_to 'Kostenloses PDF Anzeigeprogramm herunter laden.', 'http://get.adobe.com/reader/otherversions/'

- content_for 'sidebar' do
= render 'shared/email/social_buttons'
32 changes: 32 additions & 0 deletions app/views/article_mailer/mass_upload_finished_message.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
h3
= t('email.greeting', name: @mass_upload.user_nickname)
= t('email.article.mass_upload_finished.success')
- if @mass_upload.articles_for_mass_activation.any?
p
= t('email.article.mass_upload_finished.to_activate', count: @mass_upload.articles_for_mass_activation)
ul
- if @created_count > 0
li= t('email.article.mass_upload_finished.created_articles', count: @created_count)
- if @updated_count > 0
li= t('email.article.mass_upload_finished.updated_articles', count: @updated_count)
- if @activated_count > 0
li= t('email.article.mass_upload_finished.activated_articles', count: @activated_count)

= link_to t('email.article.mass_upload_finished.activate_now'), mass_upload_url(@mass_upload)
- if @mass_upload.deleted_articles.any?
p
= t('email.article.mass_upload_finished.deleted_articles', count: @deleted_count)
- if @mass_upload.deactivated_articles.any?
p
= t('email.article.mass_upload_finished.deactivated_articles', count: @deactivated_count)

br
= t('email.close')

.pdf
= link_to 'Unsere AGB', "#{root_url}/assets/AGB.pdf"
br
= link_to 'Kostenloses PDF Anzeigeprogramm herunter laden.', 'http://get.adobe.com/reader/otherversions/'

- content_for 'sidebar' do
= render 'shared/email/social_buttons'
39 changes: 39 additions & 0 deletions app/workers/mass_uploads_finish_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# encoding: UTF-8
#
# == License:
# Fairmondo - Fairmondo is an open-source online marketplace.
# Copyright (C) 2013 Fairmondo eG
#
# This file is part of Fairmondo.
#
# Fairmondo is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Fairmondo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Fairmondo. If not, see <http://www.gnu.org/licenses/>.
#
class MassUploadsFinishWorker
include Sidekiq::Worker
sidekiq_options queue: :mass_uploads_finish,
retry: 20,
backtrace: true

include Sidetiq::Schedulable

recurrence {
hourly.minute_of_hour(*((0..5).to_a.map{|d|d*10}))
}

def perform
MassUpload.processing.each do |mass_upload|
mass_upload.finish
end
end
end
26 changes: 14 additions & 12 deletions app/workers/process_row_mass_upload_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def perform mass_upload_id, unsanitized_row_hash, index
row_hash = MassUpload::Questionnaire.include_fair_questionnaires(row_hash)
row_hash = MassUpload::Questionnaire.add_commendation(row_hash)

article = create_or_find_according_to_action row_hash, mass_upload.user
article = build_or_find_according_to_action row_hash, mass_upload.user

if article.action != :nothing # so we can ignore rows when reimporting
article.user_id = mass_upload.user_id
Expand All @@ -73,7 +73,7 @@ def perform mass_upload_id, unsanitized_row_hash, index

# When an action is set, modify the save call to reflect what should be done
# @return [Article] Article ready to save or containing an error
def create_or_find_according_to_action attribute_hash, user
def build_or_find_according_to_action attribute_hash, user
attribute_hash['action'].strip! if attribute_hash['action']
case attribute_hash['action']
when 'c', 'create'
Expand All @@ -82,14 +82,14 @@ def create_or_find_according_to_action attribute_hash, user
process_dynamic_update attribute_hash, user
when nil
attribute_hash['action'] = get_processing_default attribute_hash, user
create_or_find_according_to_action attribute_hash, user #recursion happens once
build_or_find_according_to_action attribute_hash, user #recursion happens once
when 'nothing'
# Keep article as is. We could update it, but this conflicts with locked articles
article = find_by_id_or_custom_seller_identifier attribute_hash, user
article.action = :nothing
article
else
create_error_article I18n.t("mass_uploads.errors.unknown_action")
build_error_article I18n.t("mass_uploads.errors.unknown_action")
end
end

Expand All @@ -100,11 +100,11 @@ def find_by_id_or_custom_seller_identifier attribute_hash, user
elsif attribute_hash['custom_seller_identifier']
article = find_article_by_custom_seller_identifier attribute_hash['custom_seller_identifier'], user
else
article = create_error_article I18n.t("mass_uploads.errors.no_identifier")
article = build_error_article I18n.t("mass_uploads.errors.no_identifier")
end

unless article
article = create_error_article I18n.t("mass_uploads.errors.article_not_found")
article = build_error_article I18n.t("mass_uploads.errors.article_not_found")
end

article
Expand Down Expand Up @@ -152,7 +152,7 @@ def get_processing_default attribute_hash, user
# Get article with error message for display in MassUpload#new error list
# @param error_message [String] Message to display
# @return [Article] Article containing error field
def create_error_article error_message
def build_error_article error_message
article = Article.new
article.errors[:base] = error_message
article
Expand Down Expand Up @@ -183,9 +183,9 @@ def validation_errors_as_text_for article
end

# Perform Validations without clearing the base errors on this object.
# Need this for setting things in create_error_article
# Need this for setting things in build_error_article
def errors_exist_in? article
unless article.errors[:base].any? # If there are base errors something went wrong in general and we can skip the other validations
if article.errors[:base].empty? # If there are base errors something went wrong in general and we can skip the other validations
base_errors = article.errors.dup # save errors before .valid? call because it clears errors
article.valid? # preform validation
base_errors.each do |key,error| # readd old error msgs
Expand All @@ -199,7 +199,7 @@ def errors_exist_in? article

def process article, mass_upload_article
mass_upload_article.with_lock do
unless mass_upload_article.article_id.present?
if mass_upload_article.article_id.blank?
case article.action
when :activate, :create, :update
article.calculate_fees_and_donations
Expand All @@ -215,7 +215,9 @@ def process article, mass_upload_article
end

def self.add_article_error_messages_to( mass_upload, validation_errors, mass_upload_article, row_hash )
csv = CSV.generate_line(MassUpload.article_attributes.map{ |column| row_hash[column] }, col_sep: ';')
mass_upload_article.update_attributes!(validation_errors: validation_errors,article_csv: csv)
mass_upload_article.with_lock do
csv = CSV.generate_line(MassUpload.article_attributes.map{ |column| row_hash[column] }, col_sep: ';')
mass_upload_article.update_attributes!(validation_errors: validation_errors,article_csv: csv)
end
end
end
10 changes: 10 additions & 0 deletions config/locales/email/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ de:
Unterschrift des/der Verbraucher*in (nur bei Mitteilung auf Papier)
Datum'
mass_upload_finished:
success: Dein CSV-Upload wurde erfolgreich verarbeitet.
failure: 'Bei der Verarbeitung Deiner CSV Datei durch das System ist ein Fehler aufgetreten. Klicke hier um den Fehler anzuzeigen:'
to_activate: 'Dabei musst Du noch %{count} Artikel aktivieren.'
created_articles: 'Neu erstellte Artikel: %{count}'
updated_articles: 'Modifizierte Artikel: %{count}'
activated_articles: 'Zu reaktivierende Artikel: %{count}'
activate_now: "Jetzt aktivieren"
deleted_articles: 'Du hast %{count} Artikel gelöscht.'
deactivated_articles: 'Du hast %{count} Artikel deaktiviert.'
mass_activation:
text: 'Du hast erfolgreich per CSV-Upload eingestellte Artikel aktiviert.'
to_csv: 'Hier kommst Du zu Deinem CSV-Upload: '
Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@
require 'sidekiq/web'
require 'sidetiq/web'

constraint = lambda { |request| request.env['warden'].authenticate? and
request.env['warden'].user.admin?}
if Rails.env.development?
constraint = lambda { |request| true }
else
constraint = lambda { |request| request.env['warden'].authenticate? and
request.env['warden'].user.admin? }
end

constraints constraint do
mount Sidekiq::Web => '/sidekiq'
Expand Down
1 change: 1 addition & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ production:
- [default, 5]
- [mass_upload, 3]
- [mass_upload_rows, 3]
- [mass_uploads_finish, 1]
- [paperclip_background, 1]
- [paperclip_foreground, 4]
- [fastbill, 5]
Expand Down
4 changes: 4 additions & 0 deletions test/factories/mass_uploads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
factory :mass_upload do
file { fixture_file_upload('test/fixtures/mass_upload_correct.csv', 'text/csv') }
user { FactoryGirl.create :legal_entity }
factory :mass_upload_to_finish do
state :processing
row_count 0
end
end
end
12 changes: 12 additions & 0 deletions test/mailers/article_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@

mail.must deliver_to mass_upload.user.email
end

it '#mass_upload_processed_message' do
mail = ArticleMailer.mass_upload_finished_message(mass_upload)

mail.must deliver_to mass_upload.user.email
end

it '#mass_upload_failed_message' do
mail = ArticleMailer.mass_upload_failed_message(mass_upload)

mail.must deliver_to mass_upload.user.email
end
end
14 changes: 14 additions & 0 deletions test/workers/mass_uploads_finish_worker_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "test_helper"

describe MassUploadsFinishWorker do
it "finishes a processing mass_upload" do
mass_upload = FactoryGirl.create(:mass_upload_to_finish)
MassUploadsFinishWorker.new.perform
mass_upload.reload
mass_upload.state.must_equal "finished"
end
end




0 comments on commit 454d4a8

Please sign in to comment.