<commit_before>require "#{Rails.root}/lib/wiki_edits"
require "#{Rails.root}/lib/utils"

# Handles assignment data submitted by users
class AssignmentsManager
  def self.update_assignments(course, user_params, current_user)
    user_params['assignments'].each do |assignment|
      assignment['course_id'] = course.id
      assignment['article_title'] = Utils.format_article_title(assignment['article_title'])
      assigned = Article.find_by(title: assignment['article_title'], namespace: 0)
      if assigned
        # We double check that the titles are equal to avoid false matches of case variants.
        # We can revise this once the database is set to use case-sensitive collation.
        assignment['article_id'] = assigned.id if assigned.title == assignment['article_title']
      end
      update_util Assignment, assignment
    end

    WikiEdits.update_assignments(current_user, course)
    WikiEdits.update_course(course, current_user)
  end

  def self.update_util(model, object)
    if object['id'].nil?
      model.create object
    elsif object['deleted']
      model.destroy object['id']
    else
      model.update object['id'], object
    end
  end
end<commit_msg>For assignment updates, catch ActiveRecord::RecordNotUnique errors and log them to Sentry<commit_after>require "#{Rails.root}/lib/wiki_edits"
require "#{Rails.root}/lib/utils"

# Handles assignment data submitted by users
class AssignmentsManager
  def self.update_assignments(course, user_params, current_user)
    user_params['assignments'].each do |assignment|
      assignment['course_id'] = course.id
      assignment['article_title'] = Utils.format_article_title(assignment['article_title'])
      assigned = Article.find_by(title: assignment['article_title'], namespace: 0)
      if assigned
        # We double check that the titles are equal to avoid false matches of case variants.
        # We can revise this once the database is set to use case-sensitive collation.
        assignment['article_id'] = assigned.id if assigned.title == assignment['article_title']
      end
      update_util Assignment, assignment
    end

    WikiEdits.update_assignments(current_user, course)
    WikiEdits.update_course(course, current_user)
  end

  def self.update_util(model, object)
    if object['id'].nil?
      model.create object
    elsif object['deleted']
      model.destroy object['id']
    else
      model.update object['id'], object
    end
  rescue ActiveRecord::RecordNotUnique => error
    Raven.capture_exception error, level: 'warning'
  end
end
{'fraction_non_alphanumeric': 0.09411764705882353, 'fraction_numerical': 0.000784313725490196, 'mean_word_length': 4.0019607843137255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6581941', 'n_tokens_mistral': 718, 'n_tokens_neox': 716, 'n_words': 198}