<commit_before>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

class CalendarEventsApiController < ApplicationController
  include Api::V1::CalendarEvent

  before_filter :require_user
  before_filter :get_context, :only => :create
  before_filter :get_options, :only => :index

  def index
    scope = if @type == :assignment
      Assignment.active.
        for_context_codes(@context_codes).
        send(*date_scope_and_args(:due_between))
    else
      CalendarEvent.active.
        for_user_and_context_codes(@current_user, @context_codes, @section_codes).
        send(*date_scope_and_args)
    end

    events = Api.paginate(scope.order('id'), self, api_v1_calendar_events_path(search_params))
    CalendarEvent.send(:preload_associations, events, :child_events) if @type == :event
    render :json => events.map{ |event| event_json(event, @current_user, session) }
  end

  def create
    @event = @context.calendar_events.build(params[:calendar_event])
    if authorized_action(@event, @current_user, :create)
      @event.validate_context! if @context.is_a?(AppointmentGroup)
      @event.updating_user = @current_user
      if @event.save
        render :json => event_json(@event, @current_user, session), :status => :created
      else
        render :json => @event.errors.to_json, :status => :bad_request
      end
    end
  end

  def show
    get_event(true)
    if authorized_action(@event, @current_user, :read)
      render :json => event_json(@event, @current_user, session)
    end
  end

  def reserve
    get_event(true)
    if authorized_action(@event, @current_user, :reserve)
      begin
        if params[:participant_id] && @event.appointment_group.grants_right?(@current_user, session, :manage)
          participant = @event.appointment_group.possible_participants.detect { |p| p.id == params[:participant_id].to_i }
        else
          participant = @event.appointment_group.participant_for(@current_user)
          participant = nil if participant && params[:participant_id] && params[:participant_id].to_i != participant.id
        end
        raise CalendarEvent::ReservationError, "invalid participant" unless participant
        reservation = @event.reserve_for(participant, @current_user, :cancel_existing => params[:cancel_existing])
        render :json => event_json(reservation, @current_user, session)
      rescue CalendarEvent::ReservationError => err
        reservations = participant ? @event.appointment_group.reservations_for(participant) : []
        render :json => [{
            :attribute => 'reservation',
            :type => 'calendar_event',
            :message => err.message,
            :reservations => reservations.map{ |r| event_json(r, @current_user, session) }
          }],
          :status => :bad_request
      end
    end
  end

  def update
    get_event(true)
    if authorized_action(@event, @current_user, :update)
      if @event.is_a?(Assignment)
        params[:calendar_event] = {:due_at => params[:calendar_event][:start_at]}
      else
        @event.validate_context! if @event.context.is_a?(AppointmentGroup)
        @event.updating_user = @current_user
      end
      params[:calendar_event].delete(:context_code)
      if @event.update_attributes(params[:calendar_event])
        render :json => event_json(@event, @current_user, session)
      else
        render :json => @event.errors.to_json, :status => :bad_request
      end
    end
  end

  def destroy
    get_event
    if authorized_action(@event, @current_user, :delete)
      @event.updating_user = @current_user
      @event.cancel_reason = params[:cancel_reason]
      if @event.destroy
        render :json => event_json(@event, @current_user, session)
      else
        render :json => @event.errors.to_json, :status => :bad_request
      end
    end
  end

  protected

  def get_context
    @context = Context.find_by_asset_string(params[:calendar_event].delete(:context_code)) if params[:calendar_event] && params[:calendar_event][:context_code]
    raise ActiveRecord::RecordNotFound unless @context
  end

  def get_event(search_assignments = false)
    @event = if params[:id] =~ /\Aassignment_(.*)/
      raise ActiveRecord::RecordNotFound unless search_assignments
      Assignment.find($1)
    else
      CalendarEvent.find(params[:id])
    end
  end

  def date_scope_and_args(between_scope = :between)
    if @start_date
      [between_scope, @start_date, @end_date]
    else
      [:undated]
    end
  end
<commit_msg>rename method to avoid unintended breakage<commit_after>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

class CalendarEventsApiController < ApplicationController
  include Api::V1::CalendarEvent

  before_filter :require_user
  before_filter :get_calendar_context, :only => :create
  before_filter :get_options, :only => :index

  def index
    scope = if @type == :assignment
      Assignment.active.
        for_context_codes(@context_codes).
        send(*date_scope_and_args(:due_between))
    else
      CalendarEvent.active.
        for_user_and_context_codes(@current_user, @context_codes, @section_codes).
        send(*date_scope_and_args)
    end

    events = Api.paginate(scope.order('id'), self, api_v1_calendar_events_path(search_params))
    CalendarEvent.send(:preload_associations, events, :child_events) if @type == :event
    render :json => events.map{ |event| event_json(event, @current_user, session) }
  end

  def create
    @event = @context.calendar_events.build(params[:calendar_event])
    if authorized_action(@event, @current_user, :create)
      @event.validate_context! if @context.is_a?(AppointmentGroup)
      @event.updating_user = @current_user
      if @event.save
        render :json => event_json(@event, @current_user, session), :status => :created
      else
        render :json => @event.errors.to_json, :status => :bad_request
      end
    end
  end

  def show
    get_event(true)
    if authorized_action(@event, @current_user, :read)
      render :json => event_json(@event, @current_user, session)
    end
  end

  def reserve
    get_event(true)
    if authorized_action(@event, @current_user, :reserve)
      begin
        if params[:participant_id] && @event.appointment_group.grants_right?(@current_user, session, :manage)
          participant = @event.appointment_group.possible_participants.detect { |p| p.id == params[:participant_id].to_i }
        else
          participant = @event.appointment_group.participant_for(@current_user)
          participant = nil if participant && params[:participant_id] && params[:participant_id].to_i != participant.id
        end
        raise CalendarEvent::ReservationError, "invalid participant" unless participant
        reservation = @event.reserve_for(participant, @current_user, :cancel_existing => params[:cancel_existing])
        render :json => event_json(reservation, @current_user, session)
      rescue CalendarEvent::ReservationError => err
        reservations = participant ? @event.appointment_group.reservations_for(participant) : []
        render :json => [{
            :attribute => 'reservation',
            :type => 'calendar_event',
            :message => err.message,
            :reservations => reservations.map{ |r| event_json(r, @current_user, session) }
          }],
          :status => :bad_request
      end
    end
  end

  def update
    get_event(true)
    if authorized_action(@event, @current_user, :update)
      if @event.is_a?(Assignment)
        params[:calendar_event] = {:due_at => params[:calendar_event][:start_at]}
      else
        @event.validate_context! if @event.context.is_a?(AppointmentGroup)
        @event.updating_user = @current_user
      end
      params[:calendar_event].delete(:context_code)
      if @event.update_attributes(params[:calendar_event])
        render :json => event_json(@event, @current_user, session)
      else
        render :json => @event.errors.to_json, :status => :bad_request
      end
    end
  end

  def destroy
    get_event
    if authorized_action(@event, @current_user, :delete)
      @event.updating_user = @current_user
      @event.cancel_reason = params[:cancel_reason]
      if @event.destroy
        render :json => event_json(@event, @current_user, session)
      else
        render :json => @event.errors.to_json, :status => :bad_request
      end
    end
  end

  protected

  def get_calendar_context
    @context = Context.find_by_asset_string(params[:calendar_event].delete(:context_code)) if params[:calendar_event] && params[:calendar_event][:context_code]
    raise ActiveRecord::RecordNotFound unless @context
  end

  def get_event(search_assignments = false)
    @event = if params[:id] =~ /\Aassignment_(.*)/
      raise ActiveRecord::RecordNotFound unless search_assignments
      Assignment.find($1)
    else
      CalendarEvent.find(params[:id])
    end
  end

  def date_scope_and_args(between_scope = :between)
    if @start_date
      [between_scope, @start_date, @end_date]
    else
      [:undated]
    end
  end

{'fraction_non_alphanumeric': 0.10376715662416042, 'fraction_numerical': 0.0015574807748466855, 'mean_word_length': 3.546017699115044, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5210238', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2997, 'n_words': 749}