<commit_before>
class SequencesController < ApplicationController
  layout 'sequence_run', :only => [:show]
  before_filter :set_sequence, :except => [:index, :new, :create]
  before_filter :find_or_create_sequence_run, :only => [:show]
  before_filter :portal_login, :only   => [:edit]

  # GET /sequences
  # GET /sequences.json
  def index
    @filter  = CollectionFilter.new(current_user, Sequence, params[:filter] || {})
    @sequences = @filter.collection.includes(:user,:lightweight_activities).paginate(:page => params['page'], :per_page => 20)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @sequences }
    end
  end

  # GET /sequences/1
  # GET /sequences/1.json
  def show
    authorize! :read, @sequence
    current_theme
    current_project
    respond_to do |format|
      format.html do
        if @sequence_run && @sequence_run.has_been_run
          unless params[:show_index]
            redirect_to sequence_activity_path(@sequence, @sequence_run.most_recent_activity)
            return
          end
        end
        # show.html.erb  ⬅ default template is shown otherwise
      end
      format.json { render json: @sequence }
    end
  end

  # GET /sequences/new
  # GET /sequences/new.json
  def new
    # Create a "blank" sequence in #create and head directly to #edit
    create
  end

  # GET /sequences/1/edit
  def edit
    authorize! :update, @sequence
    @activities = LightweightActivity.can_see(current_user)
  end

  # POST /sequences
  # POST /sequences.json
  def create
    @sequence = Sequence.new(params[:sequence])
    authorize! :create, @sequence
    @sequence.user = current_user

    respond_to do |format|
      if @sequence.save
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Sequence was successfully created.' }
        format.json { render json: @sequence, status: :created, location: @sequence }
      else
        format.html { render action: "new" }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /sequences/1
  # PUT /sequences/1.json
  def update
    authorize! :update, @sequence
    respond_to do |format|
      if @sequence.update_attributes(params[:sequence])
        format.html { redirect_to @sequence, notice: 'Sequence was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  def add_activity
    authorize! :update, @sequence
    activity = LightweightActivity.find(params[:activity_id])
    respond_to do |format|
      if @sequence.lightweight_activities << activity
        format.html { redirect_to edit_sequence_url(@sequence) }
        format.json { head :no_content }
      else
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Activity was not added.' }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  def remove_activity
    authorize! :update, @sequence
    activity = LightweightActivity.find(params[:activity_id])
    respond_to do |format|
      act_sequence = activity.for_sequence(@sequence)
      if act_sequence && act_sequence.remove_from_list
        act_sequence.delete
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Activity was successfully removed.' }
        format.json { head :no_content }
      else
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Activity was not removed.' }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  def reorder_activities
    authorize! :update, @sequence
    params[:item_lightweight_activities_sequence].each do |a|
      # Format: item_lightweight_activities_sequence[]=1&item_lightweight_activities_sequence[]=3
      activity = @sequence.lightweight_activities_sequences.find(a)
      # If we move everything to the bottom in order, the first one should be at the top
      activity.move_to_bottom
    end
    # Respond with 200
    if request.xhr?
      respond_with_nothing
    else
      redirect_to edit_sequence_path(@sequence)
    end
  end

  # DELETE /sequences/1
  # DELETE /sequences/1.json
  def destroy
    @sequence.destroy
    authorize! :update, @sequence

    respond_to do |format|
      format.html { redirect_to sequences_url }
      format.json { head :no_content }
    end
  end

  def duplicate
    authorize! :duplicate, @sequence
    @new_sequence = @sequence.duplicate(current_user)
    redirect_to edit_sequence_path(@new_sequence)
  end

  def show_status
    @message = params[:message] || ''
    respond_to do |format|
      format.js { render :json => { :html => render_to_string('export')}, :content_type => 'text/json' }
      format.html
    end
  end
  
  def export
    authorize! :export, @sequence
    sequence_json = @sequence.export
    send_data sequence_json, type: :json, disposition: "attachment", filename: "#{@sequence.name}_version_1.json"
  end

  private
  def set_sequence
    @sequence = Sequence.find(params[:id])
  end

  def find_or_create_sequence_run
    if sequence_run_id = params['sequence_run']
      @sequence_run = SequenceRun.find(sequence_run_id)
      return @sequence_run if @sequence_run
    end

    # We verify that the user exists after did_reauthenticate.
    # there have been instances where the session kept :did_reauthenticate without
    # having a valid user. In that case, we shoudl re-auth agian.
    if session.delete(:did_reauthenticate) && current_user
      # Special case when collaborators_data_url is provided (usually as a GET param).
      if params[:collaborators_data_url] && params[:domain]
        cc = CreateCollaboration.new(params[:collaborators_data_url], params[:domain], current_user, @sequence)
        @sequence_run = cc.call
      else
        portal = RemotePortal.new(params)
        @sequence_run = SequenceRun.lookup_or_create(@sequence, current_user, portal)
        # If sequence is ran with "portal" params, it means that user wants to run it individually.
        # Note that "portal" refers to individual student data endpoint, this name should be updated.
        @sequence_run.disable_collaboration if portal.valid?
      end
    elsif params[:domain]
      # Force re-authentication with a portal if there is portal info in params
      # If there is no portal info, then we just assume there is no sequence run.
      update_portal_session(params[:domain])
    end
    # This creates a new sequence_run if it doesn't exist.
  end

end
<commit_msg>Apply portal_login before_filter to the duplicate action.[#82037090]<commit_after>
class SequencesController < ApplicationController
  layout 'sequence_run', :only => [:show]
  before_filter :set_sequence, :except => [:index, :new, :create]
  before_filter :find_or_create_sequence_run, :only => [:show]
  before_filter :portal_login, :only   => [:edit,:duplicate]

  # GET /sequences
  # GET /sequences.json
  def index
    @filter  = CollectionFilter.new(current_user, Sequence, params[:filter] || {})
    @sequences = @filter.collection.includes(:user,:lightweight_activities).paginate(:page => params['page'], :per_page => 20)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @sequences }
    end
  end

  # GET /sequences/1
  # GET /sequences/1.json
  def show
    authorize! :read, @sequence
    current_theme
    current_project
    respond_to do |format|
      format.html do
        if @sequence_run && @sequence_run.has_been_run
          unless params[:show_index]
            redirect_to sequence_activity_path(@sequence, @sequence_run.most_recent_activity)
            return
          end
        end
        # show.html.erb  ⬅ default template is shown otherwise
      end
      format.json { render json: @sequence }
    end
  end

  # GET /sequences/new
  # GET /sequences/new.json
  def new
    # Create a "blank" sequence in #create and head directly to #edit
    create
  end

  # GET /sequences/1/edit
  def edit
    authorize! :update, @sequence
    @activities = LightweightActivity.can_see(current_user)
  end

  # POST /sequences
  # POST /sequences.json
  def create
    @sequence = Sequence.new(params[:sequence])
    authorize! :create, @sequence
    @sequence.user = current_user

    respond_to do |format|
      if @sequence.save
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Sequence was successfully created.' }
        format.json { render json: @sequence, status: :created, location: @sequence }
      else
        format.html { render action: "new" }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /sequences/1
  # PUT /sequences/1.json
  def update
    authorize! :update, @sequence
    respond_to do |format|
      if @sequence.update_attributes(params[:sequence])
        format.html { redirect_to @sequence, notice: 'Sequence was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  def add_activity
    authorize! :update, @sequence
    activity = LightweightActivity.find(params[:activity_id])
    respond_to do |format|
      if @sequence.lightweight_activities << activity
        format.html { redirect_to edit_sequence_url(@sequence) }
        format.json { head :no_content }
      else
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Activity was not added.' }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  def remove_activity
    authorize! :update, @sequence
    activity = LightweightActivity.find(params[:activity_id])
    respond_to do |format|
      act_sequence = activity.for_sequence(@sequence)
      if act_sequence && act_sequence.remove_from_list
        act_sequence.delete
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Activity was successfully removed.' }
        format.json { head :no_content }
      else
        format.html { redirect_to edit_sequence_url(@sequence), notice: 'Activity was not removed.' }
        format.json { render json: @sequence.errors, status: :unprocessable_entity }
      end
    end
  end

  def reorder_activities
    authorize! :update, @sequence
    params[:item_lightweight_activities_sequence].each do |a|
      # Format: item_lightweight_activities_sequence[]=1&item_lightweight_activities_sequence[]=3
      activity = @sequence.lightweight_activities_sequences.find(a)
      # If we move everything to the bottom in order, the first one should be at the top
      activity.move_to_bottom
    end
    # Respond with 200
    if request.xhr?
      respond_with_nothing
    else
      redirect_to edit_sequence_path(@sequence)
    end
  end

  # DELETE /sequences/1
  # DELETE /sequences/1.json
  def destroy
    @sequence.destroy
    authorize! :update, @sequence

    respond_to do |format|
      format.html { redirect_to sequences_url }
      format.json { head :no_content }
    end
  end

  def duplicate
    authorize! :duplicate, @sequence
    @new_sequence = @sequence.duplicate(current_user)
    redirect_to edit_sequence_path(@new_sequence)
  end

  def show_status
    @message = params[:message] || ''
    respond_to do |format|
      format.js { render :json => { :html => render_to_string('export')}, :content_type => 'text/json' }
      format.html
    end
  end
  
  def export
    authorize! :export, @sequence
    sequence_json = @sequence.export
    send_data sequence_json, type: :json, disposition: "attachment", filename: "#{@sequence.name}_version_1.json"
  end

  private
  def set_sequence
    @sequence = Sequence.find(params[:id])
  end

  def find_or_create_sequence_run
    if sequence_run_id = params['sequence_run']
      @sequence_run = SequenceRun.find(sequence_run_id)
      return @sequence_run if @sequence_run
    end

    # We verify that the user exists after did_reauthenticate.
    # there have been instances where the session kept :did_reauthenticate without
    # having a valid user. In that case, we shoudl re-auth agian.
    if session.delete(:did_reauthenticate) && current_user
      # Special case when collaborators_data_url is provided (usually as a GET param).
      if params[:collaborators_data_url] && params[:domain]
        cc = CreateCollaboration.new(params[:collaborators_data_url], params[:domain], current_user, @sequence)
        @sequence_run = cc.call
      else
        portal = RemotePortal.new(params)
        @sequence_run = SequenceRun.lookup_or_create(@sequence, current_user, portal)
        # If sequence is ran with "portal" params, it means that user wants to run it individually.
        # Note that "portal" refers to individual student data endpoint, this name should be updated.
        @sequence_run.disable_collaboration if portal.valid?
      end
    elsif params[:domain]
      # Force re-authentication with a portal if there is portal info in params
      # If there is no portal info, then we just assume there is no sequence run.
      update_portal_session(params[:domain])
    end
    # This creates a new sequence_run if it doesn't exist.
  end

end

{'fraction_non_alphanumeric': 0.08976447933639461, 'fraction_numerical': 0.0028143978669826694, 'mean_word_length': 3.1856788592684437, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '236261', 'n_tokens_mistral': 4005, 'n_tokens_neox': 3897, 'n_words': 1100}