Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

187121440 materials bulk upload #28

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions app/controllers/admin/commercials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,40 @@ def render_commercial
# Received a file from user
# Reads file and creates commercial for event
# File content example:
# EventID:MyURL
# EventID, Title, URL
def mass_upload
errors = Commercial.read_file(params[:file]) if params[:file]

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

flash[:error] = errors_text
else
flash[:notice] = 'Successfully added materials.'
end
redirect_back(fallback_location: root_path)
end

private

# Aggregate errors and ensure that they do not exceed 4 KB in total size
def aggregate_errors(errors)
errors_text = ''
if errors[:no_event].any?
errors_text += 'Unable to find events with IDs: ' + errors[:no_event].join(', ') + '. '
end
if errors[:validation_errors].any?
errors_text += 'Validation errors: ' + errors[:validation_errors].join('. ')
end
errors_text
end

def commercial_params
params.require(:commercial).permit(:title, :url)
end
Expand Down
23 changes: 16 additions & 7 deletions app/models/commercial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# commercial_id :string
# commercialable_id :integer
#
require 'csv'

class Commercial < ApplicationRecord
require 'oembed'

Expand Down Expand Up @@ -46,17 +48,24 @@ def self.read_file(file)
errors[:no_event] = []
errors[:validation_errors] = []

file.read.each_line do |line|
# Get the event id (text before :)
id = line.match(/:/).pre_match.to_i
# Get the commercial url (text after :)
url = line.match(/:/).post_match
# Check if the file has a .csv extension
unless File.extname(file.original_filename).casecmp('.csv').zero?
errors[:validation_errors] << 'File must be a CSV.'
return errors
end

CSV.foreach(file.path, headers: true) do |row|
# You can access columns by their names if headers are included in the file
id = row['Event_ID'].to_i
title = row['Title']
url = row['URL']

event = Event.find_by(id: id)

# Go to next event, if the event is not found
# Go to next event if the event is not found
(errors[:no_event] << id) && next unless event

commercial = event.commercials.new(url: url)
commercial = event.commercials.new(title: title, url: url)
unless commercial.save
errors[:validation_errors] << ("Could not create materials for event with ID #{event.id} (" + commercial.errors.full_messages.to_sentence + ')')
end
Expand Down
9 changes: 5 additions & 4 deletions app/views/admin/events/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
.form-group
= f.file_field 'file', as: :file
%span.help-block
Upload your file with data in the following format:
%b Event_ID:Commercial_Link
Upload your file.csv with data in the following format:
%b Event_ID,Title,URL
for instance:
%pre
11:https://youtube.com/myvideo
23:https://vimeo.com/myvideo
Event_ID,Title,URL
1,Session Recording,https://youtube.com/myvideo
2,Demo Video,https://vimeo.com/myvideo
.modal-footer
= f.submit nil, class: 'btn btn-primary'
.row
Expand Down
Loading