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

Add extra validation when editing all labels by CSV #5368

Merged
merged 1 commit into from
Feb 16, 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
5 changes: 5 additions & 0 deletions app/controllers/course_members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
return render json: { message: I18n.t('course_members.upload_labels_csv.no_file') }, status: :unprocessable_entity if params[:file] == 'undefined'

begin
headers = CSV.foreach(params[:file].path).first
%w[id labels].each do |column|
return render json: { message: I18n.t('course_members.upload_labels_csv.missing_column', column: column) }, status: :unprocessable_entity unless headers&.include?(column)

Check warning on line 88 in app/controllers/course_members_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/course_members_controller.rb#L86-L88

Added lines #L86 - L88 were not covered by tests
end

CSV.foreach(params[:file].path, headers: true) do |row|
row = row.to_hash
cm = CourseMembership.find_by(user_id: row['id'], course: @course)
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/course_members/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ en:
upload_labels_csv:
no_file: "Please upload a file."
malformed: Something went wrong while reading the file. Check that the file you uploaded is in the correct format.
missing_column: "We couldn't find the column \"%{column}\". Check that the file you uploaded is in the correct format."
1 change: 1 addition & 0 deletions config/locales/views/course_members/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ nl:
upload_labels_csv:
no_file: "Gelieve een bestand op te laden."
malformed: Er ging iets mis bij het inlezen van het bestand. Controleer of het aangepaste bestand aan de voorwaarden voldoet.
missing_column: "We konden de kolom \"%{column}\" niet vinden. Controleer of het aangepaste bestand aan de voorwaarden voldoet."