Skip to content

Commit

Permalink
Added importing users
Browse files Browse the repository at this point in the history
  • Loading branch information
nagn committed Jul 31, 2014
1 parent aa58786 commit 2c19957
Show file tree
Hide file tree
Showing 26 changed files with 420 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ gem 'spring', group: :development
# gem 'debugger', group: [:development, :test]

gem 'rb-readline'
gem 'pry-rails'
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GEM
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
builder (3.2.2)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand All @@ -50,10 +51,17 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.2)
mime-types (1.25.1)
minitest (5.4.0)
multi_json (1.10.1)
polyglot (0.3.5)
pry (0.10.0)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.2)
pry (>= 0.9.10)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
Expand Down Expand Up @@ -85,6 +93,7 @@ GEM
sdoc (0.4.0)
json (~> 1.8)
rdoc (~> 4.0, < 5.0)
slop (3.6.0)
spring (1.1.3)
sprockets (2.11.0)
hike (~> 1.2)
Expand Down Expand Up @@ -117,6 +126,7 @@ DEPENDENCIES
coffee-rails (~> 4.0.0)
jbuilder (~> 2.0)
jquery-rails
pry-rails
rails (= 4.1.4)
rb-readline
sass-rails (~> 4.0.3)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/import_users.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/import_users.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the import_users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
50 changes: 50 additions & 0 deletions app/controllers/import_students_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class ImportStudentsController < ApplicationController
include CsvImport
def import
# initialize
file = params[:csv_upload] # the file object
if file
overwrite = (params[:overwrite] == '1') # update existing users?
filepath = file.tempfile.path # the rails CSV class needs a filepath

imported_students = csv_import(filepath)
end

# check if input file is valid and return appropriate error message if not
if valid_input_file?(imported_students, file)
# create the users and exit
@hash_of_statuses = import_students(imported_students, overwrite)
render 'imported'
end
end

# functions like the RESTful new action by rendering a form with a GET request
def import_page
# limits the options for user.role to the following array, and displays them with more user-friendly labels.
render 'import' # a form for uploading a csv file of users to import
end


private
# this method checks that the user has uploaded a file and displays flash messages if there is an error.
# putting these validations in the controller is not idiomatic in rails and there is likely a cleaner way to
# do this. If we ever have to validate more input than this, we should remove this to a csv_import validations model.
def valid_input_file?(imported_students, file)
# check if the user has uploaded a file at all.
if !file
flash[:error] = 'Please select a file to upload.'
redirect_to :back and return
end



# make sure the import went with proper headings / column handling

accepted_keys = [:name, :role, :first_name, :nick_name, :goes_by, :last_name, :official_full_name, :net_id, :cell, :yale_email, :yale_email_alias, :non_yale_email, :queue, :old_queue, :college, :class_year, :t_shirt_size, :birthday, :dietary_needs]
unless imported_students.first.keys == accepted_keys
flash[:error] = 'Unable to import CSV file. Please ensure that the first line of the file exactly matches the sample input (login, first_name, etc.) Note that headers are case sensitive and must be in the correct order.'
redirect_to :back and return
end
return true
end
end
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def update
#append student to list
@session = Session.find(params[:id])
@attended_students = Session.find(params[:id]).students
if params[:term]
if params[:selected_student]
student = Student.find(params[:selected_student])
attendance = Attendance.new({:student => student, :session => @session})
attendance.save
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/students_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class StudentsController < ApplicationController
def index
if params[:term]
session = Session.find(params[:session_id])

@students = Student.where("name like ?", "%#{params[:term]}%")
@students = Student.where("name LIKE ? OR nick_name LIKE ? OR net_id LIKE ?", "%#{params[:term]}%", "%#{params[:term]}%", "%#{params[:term]}%")
@students -= session.students #remove all students that have already attended
else
@students = Student.all
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/import_users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ImportUsersHelper
end
18 changes: 18 additions & 0 deletions app/views/import_students/_import_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<thead>
<tr>
<th>Login</th>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Reason</th>
</tr>
</thead>
<tbody>
<% students.each do |student_data| %>
<% student = student_data.first %>
<tr>
<td><%= student[:first_name] + ' ' + user[:last_name] %></td>
<td><%= student[:cell_phone] %></td>
<td><%= student[:yale_email] %></td>
</tr>
<% end %>
14 changes: 14 additions & 0 deletions app/views/import_students/import.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h3>CSV Import Format</h3>
<h5>Columns (must be in the first row of the CSV file)</h5>
<%= form_tag csv_imported_path, :multipart => true do %>

<p>
<label>CSV file with users to import:</label>
<%= file_field_tag 'csv_upload' %>
</p>
<p>
<label>Update existing users' information?</label>
<%= check_box_tag 'overwrite' %>
</p>
<%= submit_tag 'Import Users!', :class => 'btn' %>
<% end %>
18 changes: 18 additions & 0 deletions app/views/import_students/imported.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% if @hash_of_statuses %>
<% unless @hash_of_statuses[:fail].empty? %>
<h2>students not successfully imported</h2>
<table class="datatable-wide table table-striped table-bordered" id="student_import_errors_table">
<%= render :partial => 'import_table', :locals => {:students => @hash_of_statuses[:fail]} %>
</table>
<% unless @hash_of_statuses[:success].empty? %>
<hr />
<% end %>
<% end %>
<% unless @hash_of_statuses[:success].empty? %>
<h2>Students successfully imported</h2>
<%= render :partial => 'students/table', :locals => {:students => @hash_of_statuses[:success]} %>
<% end %>
<% else %>
<h2>Please follow <%= link_to "this link", :controller => :import_students, :action => :import_page %> to import students.</h2>
<% end %>
3 changes: 3 additions & 0 deletions app/views/landing_page/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
</h1>
<h1>
<%= link_to "Link to Sessions Directory", sessions_path %>
</h1>
<h1>
<%= link_to "Upload CSV Students", csv_import_page_path %>
</h1>
3 changes: 3 additions & 0 deletions app/views/layouts/_flash.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="flashes">
<%= render 'layouts/flash_alerts' %>
</div>
10 changes: 10 additions & 0 deletions app/views/layouts/_flash_alerts.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%# flash messages, if any %>
<% flash.each do |name, msg| %>
<% unless msg.blank? %>
<%= content_tag :div, :id => "flash_#{name}", :class => "alert alert-#{(name == :notice || name == 'notice') ? "success" : "error"}" do %>
<button class="close" data-dismiss="alert">×</button>
<%= msg.gsub(/\n/, "<br />").html_safe %>
<% end %>
<% end %>
<% end %>
<% flash.clear %>
5 changes: 3 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
<html>
<head>
<title>Sessions</title>
<%= javascript_include_tag :defaults %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'jquery.min', 'jquery-ui-1.9.2.custom.min.js' %>
<%= stylesheet_link_tag 'ui-lightness/jquery-ui-1.9.2.custom' %>
<%= csrf_meta_tags %>
<%= javascript_include_tag :defaults %>

</head>
<body>
<p class="flash"><%= flash.notice %></p>
<%= render 'layouts/flash' %>
<%= yield %>

</body>
Expand Down
4 changes: 2 additions & 2 deletions app/views/sessions/_selected_users.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
//Once a value in the drop down list is selected, do the following:
select: function(event, ui) {
var user_id = ui.item.id;
var update_path = "<%=session_path(@session)%>";
var session_path = "<%=@session%>";
$.ajax({
type: "POST",
url: update_path,
url: "7",
data: {
_method:'PUT',
selected_student: user_id
Expand Down
43 changes: 43 additions & 0 deletions app/views/students/_info.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<table id="table_info" class="datatable-wide table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>First Name</th>
<th>Nickname</th>
<th>Last Name</th>
<th>Goes By</th>
<th>Official Name</th>
<th>NetID</th>
<th>Cell Phone</th>
<th>Yale Email</th>
<th>Yale Email Alias</th>
<th>Real Email</th>
<th>Queue</th>
<th>Old Queue</th>
<th>Residential College/Graduate</th>
<th>Class Year</th>
<th>Birthday</th>
<th>Other</th>
</tr>
<td><%= link_to @student.name, @student %></td>
<td><%= link_to @student.first_name, @student %></td>
<td><%= link_to @student.nick_name, @student %></td>
<td><%= link_to @student.last_name, @student %></td>
<td><%= link_to @student.goes_by, @student %></td>
<td><%= link_to @student.official_full_name, @student %></td>
<td><%= link_to @student.net_id, @student %></td>
<td><%= link_to @student.cell, @student %></td>
<td><%= link_to @student.yale_email, @student %></td>
<td><%= link_to @student.yale_email_alias, @student %></td>
<td><%= link_to @student.non_yale_email, @student %></td>
<td><%= link_to @student.queue, @student %></td>
<td><%= link_to @student.old_queue, @student %></td>
<td><%= link_to @student.college, @student %></td>
<td><%= link_to @student.class_year, @student %></td>
<td><%= link_to @student.birthday, @student %></td>
<td><%= link_to @student.other, @student %></td>
</tr>
</thead>
<tbody>
</tbody>
</table>
20 changes: 20 additions & 0 deletions app/views/students/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<table id="table_students" class="datatable-wide table table-striped table-bordered">
<thead>
<tr>
<th>NetID</th>
<th>First Name</th>
<th>Nickname</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<% for student in students %>
<tr>
<td><%= link_to student.net_id, student %></td>
<td><%= link_to student.first_name, student %></td>
<td><%= link_to student.nick_name, student %></td>
<td><%= link_to student.last_name, student %></td>
</tr>
<% end %>
</tbody>
</table>
1 change: 1 addition & 0 deletions app/views/students/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<h1><%= @student.name %></h1>
<%= render :partial => 'info'%>
<%= link_to "<< Back to Students List", students_path %>

<h1>Sessions attended</h1>
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

module Sessions
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib/extras #{config.root}/lib/)
config.watchable_dirs['lib'] = [:rb]
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
resources :attendances
resources :search
root 'landing_page#index'
resources :landing_page
get '/import_students/import' => 'import_students#import_page', :as => :csv_import_page
post '/import_stude ts/imported' => 'import_students#import', :as => :csv_imported
end
21 changes: 21 additions & 0 deletions db/migrate/20140731140456_add_roster_fields_to_student.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class AddRosterFieldsToStudent < ActiveRecord::Migration
def change
add_column :students, :role, :string
add_column :students, :first_name, :string
add_column :students, :nick_name, :string
add_column :students, :goes_by, :string
add_column :students, :last_name, :string
add_column :students, :official_full_name, :string
add_column :students, :net_id, :string
add_column :students, :cell, :string
add_column :students, :yale_email, :string
add_column :students, :yale_email_alias, :string
add_column :students, :non_yale_email, :string
add_column :students, :queue, :string
add_column :students, :old_queue, :string
add_column :students, :college, :string
add_column :students, :class_year, :integer
add_column :students, :birthday, :string
add_column :students, :other, :text
end
end
19 changes: 18 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140724145916) do
ActiveRecord::Schema.define(version: 20140731140456) do

create_table "attendances", force: true do |t|
t.integer "session_id"
Expand All @@ -38,6 +38,23 @@
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
t.string "first_name"
t.string "nick_name"
t.string "goes_by"
t.string "last_name"
t.string "official_full_name"
t.string "net_id"
t.string "cell"
t.string "yale_email"
t.string "yale_email_alias"
t.string "non_yale_email"
t.string "queue"
t.string "old_queue"
t.string "college"
t.integer "class_year"
t.string "birthday"
t.text "other"
end

end
Loading

0 comments on commit 2c19957

Please sign in to comment.