Skip to content

Commit

Permalink
Merge pull request #375 from cs169/187216987_add_event_by_CSV
Browse files Browse the repository at this point in the history
187216987 add event by csv
  • Loading branch information
cycomachead authored Jul 21, 2024
2 parents acb36a5 + 3619bfb commit c9be854
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/osem-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ $(document).ready( function() {
var now = new Date();
var closestEventId = null;
var smallestDiff = Infinity;
var i=0
var i = 0;

$('.event-item').each(function() {

Expand Down
56 changes: 56 additions & 0 deletions app/controllers/admin/schedules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Admin
class SchedulesController < Admin::BaseController
# By authorizing 'conference' resource, we can ensure there will be no unauthorized access to
# the schedule of a conference, which should not be accessed in the first place
before_action :set_conference
load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource :program, through: :conference, singleton: true
load_and_authorize_resource :schedule, through: :program, except: %i[new create]
Expand Down Expand Up @@ -68,8 +69,63 @@ def destroy
end
end

def upload_csv
authorize! :update, @conference
return flash[:alert] = 'No file was attached!' unless file_present?

if process_csv
flash[:notice] = 'Schedule uploaded successfully!'
else
flash[:alert] = 'Failed to process CSV file.'
end

redirect_to admin_conference_schedules_path(@conference)
end

private

def set_conference
@conference = Conference.find_by!(short_title: params[:conference_id])
end

def file_present?
params[:schedule] && params[:schedule][:file].present?
end

def process_csv
file = params[:schedule][:file]
CSV.foreach(file.path, headers: true) do |row|
process_row(row)
end
true
rescue StandardError => e
Rails.logger.error "CSV Processing Error: #{e.message}"
false
end

def process_row(row)
event_date = parse_date(row['Date'])
event_time = parse_time(row['Start_Time'])
event_start_time = combine_datetime(event_date, event_time)

room = Room.find_or_create_by(name: row['Room'])
event = Event.find_by(id: row['Event_ID'])

event&.update(start_time: event_start_time, room: room)
end

def parse_date(date_str)
Date.strptime(date_str, '%m/%d/%y')
end

def parse_time(time_str)
Time.parse(time_str)
end

def combine_datetime(date, time)
DateTime.new(date.year, date.month, date.day, time.hour, time.min, time.sec, time.zone)
end

def schedule_params
params.require(:schedule).permit(:track_id) if params[:schedule]
end
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/events/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
%th Rating
%th Submitter
%th Speakers
%th Volunteers
- if @program.languages.present?
%th Language
%th Requires Registration
Expand Down
27 changes: 27 additions & 0 deletions app/views/admin/schedules/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@
.col-md-12
.page-header
%h1= "#{@conference.title} Schedule"

.btn-group.pull-right
%button.btn.btn-primary{ title: 'Mass import of schedule for events',
data: { toggle: 'modal', target: '#mass-schedule-modal' } }
Upload with CSV file

%p.text-muted
Create the schedules for the conference
= render 'schedules/event_types_key', event_types: @event_types, favourites: false

.modal#mass-schedule-modal
.modal-dialog
.modal-content
.modal-header
%h1
Schedule events
.modal-body
= form_for :schedule, url: upload_csv_admin_conference_schedules_path(@conference), html: { multipart: true }, method: :post do |f|
.form-group
= f.file_field :file, as: :file
%span.help-block
Upload your CSV file with data in the following format:
%b Event_ID, Start_Time, Date, Room
for instance:
%pre
Event_ID, Start_Time, Date, Room
osemdemo, 10:00, 6/1/2024, Australia
osemdemo, 11:00, 6/2/2024, Brazil
.modal-footer
= f.submit 'Upload', class: 'btn btn-primary'

- if @rooms.present?
.row
.col-md-2
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
resource :ticket_scanning, only: [:create]
resources :comments, only: [:index]
resources :conferences do
resources :schedules do
collection do
post :upload_csv
end
end
resources :surveys do
resources :survey_questions, except: :index
end
Expand Down
5 changes: 0 additions & 5 deletions db/migrate/20240308190204_add_currency_to_payments.rb

This file was deleted.

5 changes: 0 additions & 5 deletions db/migrate/20240315025823_add_currency_to_ticket_purchases.rb

This file was deleted.

2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@
t.integer "status", default: 0, null: false
t.integer "user_id", null: false
t.integer "conference_id", null: false
t.string "currency"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.string "currency"
end

create_table "physical_tickets", force: :cascade do |t|
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/features/event_schedules_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe EventSchedule, js: true do
describe EventSchedule, :js do
Timecop.return
let(:test_date) { Time.current }
let!(:conference) do
Expand Down

0 comments on commit c9be854

Please sign in to comment.