<commit_before># Controller for change requests
class ChangeRequestsController < ApplicationController
  before_action :set_change_request, except: %i[index all create]
  before_action :verify_login
  before_action :verify_accepted_terms, only: %i[create accept]
  before_action :verify_view_change_request, except: %i[index all create]
  before_action :verify_edit_or_admin, only: %i[accept decline]
  before_action :verify_requestor_or_admin, only: [:cancel]
  before_action :verify_admin, only: %i[all destroy]
  before_action :verify_pending_status, only: %i[accept decline cancel]
  before_action :set_stage, only: [:create]

  # GET /change_requests
  def index
    # This is limited to those owned/requested by @user

    sorter = proc do |a, b|
      if a.status == 'pending' and b.status != 'pending'
        -1
      elsif a.status != 'pending' and b.status == 'pending'
        1
      elsif a.status == b.status
        b.updated_at <=> a.updated_at
      else
        a.status <=> b.status
      end
    end
    @has_archived = false

    @change_requests_requested = @user.change_requests
    @change_requests_requested = @change_requests_requested.where("status = 'pending' or updated_at >= ?", 7.days.ago) unless params[:archived] == "true"
    @change_requests_requested = @change_requests_requested.sort(&sorter)

    @change_requests_owned = @user.change_requests_owned
    @change_requests_owned = @change_requests_owned.where("status = 'pending' or updated_at >= ?", 7.days.ago) unless params[:archived] == "true"
    @change_requests_owned = @change_requests_owned.sort(&sorter)

    @change_requests = @change_requests_requested + @change_requests_owned
    @has_archived = (@user.change_requests.count + @user.change_requests_owned.count) > (@change_requests.count)
  end

  # GET /change_requests/all
  def all
    # This is for admins to view all requests
    if params[:archived] == "true"
      @change_requests = ChangeRequest.all
      @has_archived = false
    else
      @change_requests = ChangeRequest.where("status = 'pending' or updated_at >= ?", 7.days.ago)
      @has_archived = ChangeRequest.all.count > @change_requests.count
    end
  end

  # GET /change_requests/:reqid
  def show
  end

  # GET /change_requests/:reqid/diff
  def diff
    render layout: false
  end

  # GET /change_requests/:reqid/compare
  def compare
    params[:view] = "full"
    render layout: false
  end

  # GET /change_requests/:reqid/diff_inline
  def diff_inline
    render layout: false
  end

  # GET /change_requests/:reqid/download
  def download
    send_file(
      @change_request.filename,
      filename: "#{@notebook.title} -- Change Request.ipynb"
    )
  end

  # POST /change_requests
  def create
    # Get the notebook the request is targeted for
    @notebook = Notebook.find_by!(uuid: params[:notebook_id])
    if @stage.content == @notebook.content
      raise ChangeRequest::BadUpload, 'Proposed content is the same as the original.'
    end
<commit_msg>Resolve the downloading of change requests issue<commit_after># Controller for change requests
class ChangeRequestsController < ApplicationController
  before_action :set_change_request, except: %i[index all create]
  before_action :verify_login
  before_action :verify_accepted_terms, only: %i[create accept]
  before_action :verify_view_change_request, except: %i[index all create]
  before_action :verify_edit_or_admin, only: %i[accept decline]
  before_action :verify_requestor_or_admin, only: [:cancel]
  before_action :verify_admin, only: %i[all destroy]
  before_action :verify_pending_status, only: %i[accept decline cancel]
  before_action :set_stage, only: [:create]

  # GET /change_requests
  def index
    # This is limited to those owned/requested by @user

    sorter = proc do |a, b|
      if a.status == 'pending' and b.status != 'pending'
        -1
      elsif a.status != 'pending' and b.status == 'pending'
        1
      elsif a.status == b.status
        b.updated_at <=> a.updated_at
      else
        a.status <=> b.status
      end
    end
    @has_archived = false

    @change_requests_requested = @user.change_requests
    @change_requests_requested = @change_requests_requested.where("status = 'pending' or updated_at >= ?", 7.days.ago) unless params[:archived] == "true"
    @change_requests_requested = @change_requests_requested.sort(&sorter)

    @change_requests_owned = @user.change_requests_owned
    @change_requests_owned = @change_requests_owned.where("status = 'pending' or updated_at >= ?", 7.days.ago) unless params[:archived] == "true"
    @change_requests_owned = @change_requests_owned.sort(&sorter)

    @change_requests = @change_requests_requested + @change_requests_owned
    @has_archived = (@user.change_requests.count + @user.change_requests_owned.count) > (@change_requests.count)
  end

  # GET /change_requests/all
  def all
    # This is for admins to view all requests
    if params[:archived] == "true"
      @change_requests = ChangeRequest.all
      @has_archived = false
    else
      @change_requests = ChangeRequest.where("status = 'pending' or updated_at >= ?", 7.days.ago)
      @has_archived = ChangeRequest.all.count > @change_requests.count
    end
  end

  # GET /change_requests/:reqid
  def show
  end

  # GET /change_requests/:reqid/diff
  def diff
    render layout: false
  end

  # GET /change_requests/:reqid/compare
  def compare
    params[:view] = "full"
    render layout: false
  end

  # GET /change_requests/:reqid/diff_inline
  def diff_inline
    render layout: false
  end

  # GET /change_requests/:reqid/download
  def download
    send_data(
      @change_request.proposed_notebook.to_json,
      filename: "#{@notebook.title} -- Change Request.ipynb"
    )
  end

  # POST /change_requests
  def create
    # Get the notebook the request is targeted for
    @notebook = Notebook.find_by!(uuid: params[:notebook_id])
    if @stage.content == @notebook.content
      raise ChangeRequest::BadUpload, 'Proposed content is the same as the original.'
    end

{'fraction_non_alphanumeric': 0.10076108537392456, 'fraction_numerical': 0.0016545334215751159, 'mean_word_length': 3.7748815165876777, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4431667', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1883, 'n_words': 466}