Skip to content

Commit ed2e349

Browse files
authored
Merge pull request snap-cloud#365 from cs169/187121440_Materials_Bulk_Upload
187121440 materials bulk upload
2 parents 100f274 + b354368 commit ed2e349

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

app/controllers/admin/commercials_controller.rb

+22-10
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,40 @@ def render_commercial
5555
# Received a file from user
5656
# Reads file and creates commercial for event
5757
# File content example:
58-
# EventID:MyURL
58+
# EventID, Title, URL
5959
def mass_upload
6060
errors = Commercial.read_file(params[:file]) if params[:file]
6161

6262
if !params[:file]
6363
flash[:error] = 'Empty file detected while adding materials to Event'
64-
elsif errors.all? { |_k, v| v.blank? }
65-
flash[:notice] = 'Successfully added materials.'
66-
else
67-
errors_text = ''
68-
errors_text += 'Unable to find event with ID: ' + errors[:no_event].join(', ') + '. ' if errors[:no_event].any?
69-
if errors[:validation_errors].any?
70-
errors_text += 'There were some errors: ' + errors[:validation_errors].join('. ')
71-
end
64+
elsif errors.present?
65+
errors_text = aggregate_errors(errors)
66+
flash[:notice] = if errors_text.length > 4096
67+
'Errors are too long to be displayed. Please check the logs.'
68+
else
69+
errors_text
70+
end
7271

73-
flash[:error] = errors_text
72+
else
73+
flash[:notice] = 'Successfully added materials.'
7474
end
7575
redirect_back(fallback_location: root_path)
7676
end
7777

7878
private
7979

80+
# Aggregate errors and ensure that they do not exceed 4 KB in total size
81+
def aggregate_errors(errors)
82+
errors_text = ''
83+
if errors[:no_event].any?
84+
errors_text += 'Unable to find events with IDs: ' + errors[:no_event].join(', ') + '. '
85+
end
86+
if errors[:validation_errors].any?
87+
errors_text += 'Validation errors: ' + errors[:validation_errors].join('. ')
88+
end
89+
errors_text
90+
end
91+
8092
def commercial_params
8193
params.require(:commercial).permit(:title, :url)
8294
end

app/models/commercial.rb

+16-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# commercial_id :string
1515
# commercialable_id :integer
1616
#
17+
require 'csv'
18+
1719
class Commercial < ApplicationRecord
1820
require 'oembed'
1921

@@ -46,17 +48,24 @@ def self.read_file(file)
4648
errors[:no_event] = []
4749
errors[:validation_errors] = []
4850

49-
file.read.each_line do |line|
50-
# Get the event id (text before :)
51-
id = line.match(/:/).pre_match.to_i
52-
# Get the commercial url (text after :)
53-
url = line.match(/:/).post_match
51+
# Check if the file has a .csv extension
52+
unless File.extname(file.original_filename).casecmp('.csv').zero?
53+
errors[:validation_errors] << 'File must be a CSV.'
54+
return errors
55+
end
56+
57+
CSV.foreach(file.path, headers: true) do |row|
58+
# You can access columns by their names if headers are included in the file
59+
id = row['Event_ID'].to_i
60+
title = row['Title']
61+
url = row['URL']
62+
5463
event = Event.find_by(id: id)
5564

56-
# Go to next event, if the event is not found
65+
# Go to next event if the event is not found
5766
(errors[:no_event] << id) && next unless event
5867

59-
commercial = event.commercials.new(url: url)
68+
commercial = event.commercials.new(title: title, url: url)
6069
unless commercial.save
6170
errors[:validation_errors] << ("Could not create materials for event with ID #{event.id} (" + commercial.errors.full_messages.to_sentence + ')')
6271
end

app/views/admin/events/index.html.haml

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
.form-group
3636
= f.file_field 'file', as: :file
3737
%span.help-block
38-
Upload your file with data in the following format:
39-
%b Event_ID:Commercial_Link
38+
Upload your file.csv with data in the following format:
39+
%b Event_ID,Title,URL
4040
for instance:
4141
%pre
42-
11:https://youtube.com/myvideo
43-
23:https://vimeo.com/myvideo
42+
Event_ID,Title,URL
43+
1,Session Recording,https://youtube.com/myvideo
44+
2,Demo Video,https://vimeo.com/myvideo
4445
.modal-footer
4546
= f.submit nil, class: 'btn btn-primary'
4647
.row

0 commit comments

Comments
 (0)