Skip to content

Commit

Permalink
Merge pull request #12 from elijah-santos25/master
Browse files Browse the repository at this point in the history
Added calendar view, created excusals, and created optional builds.
  • Loading branch information
patfair authored Feb 11, 2023
2 parents df4b701 + dd0c571 commit 0690050
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 101 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ gem "rake"
gem "sequel"
gem "sinatra"
gem "thin"
gem "tzinfo-data"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ GEM
tilt (2.0.11)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2022.7)
tzinfo (>= 1.0.0)
zeitwerk (2.5.1)

PLATFORMS
Expand All @@ -65,6 +67,7 @@ DEPENDENCIES
sequel
sinatra
thin
tzinfo-data

BUNDLED WITH
2.1.4
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dev": {
"db_password": "correcthorsebatterystaple",
"members_url": "http://members.team254.localhost:81/",
"members_url": "http://members.team254.localhost:81",
"signin_ip_whitelist": []
},
"prod": {
Expand Down
8 changes: 8 additions & 0 deletions db/migrations/008_create_optional_builds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Sequel.migration do
change do
create_table(:optional_builds) do
primary_key :id
Date :date, null: false, unique: true
end
end
end
9 changes: 9 additions & 0 deletions db/migrations/009_create_excused_sessions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Sequel.migration do
change do
create_table(:excused_sessions) do
primary_key :id
Date :date, null: false
Integer :student_id, null: false
end
end
end
99 changes: 95 additions & 4 deletions hours_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
require "json"

require "models"
require "queries"

module CheesyHours
class Server < Sinatra::Base
use Rack::Session::Cookie, :key => "rack.session", :expire_after => 3600

# Enforce authentication for all non-public routes.
before do
@user = CheesyCommon::Auth.get_user(request)
Expand Down Expand Up @@ -78,24 +78,99 @@ class Server < Sinatra::Base
erb :leader_board
end

get "/calendar" do
erb :calendar
end

get "/schedule_optional" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
@referrer = request.referrer
@date = params[:date]
erb :optional_build_scheduler
end

post "/schedule_optional" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
halt(400, "Invalid date.") if params[:date].nil?|| params[:date] == ""

OptionalBuild.create(:date => params[:date]) if OptionalBuild.where(:date => params[:date]).empty?

redirect params[:referrer]
end

get "/delete_optional/:date" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
@referrer = request.referrer

erb :delete_optional_build
end

post "/delete_optional/:date" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
OptionalBuild.where(:date => params[:date]).delete

redirect params[:referrer]
end


get "/students/:id" do
@student = Student[params[:id]]
halt(400, "Invalid student.") if @student.nil?
erb :student
end

get "/students/:id/mark_excused" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
@student = Student[params[:id]]
halt(400, "Invalid student.") if @student.nil?
@referrer = request.referrer
if !params[:date].nil?
@date = params[:date]
end
erb :mark_excused
end

post "/students/:id/mark_excused" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
halt(400, "Missing date.") if params[:date].nil? || params[:date] == ""
ExcusedSession.create(:date => params[:date], :student_id => params[:id])
redirect params[:referrer]
end

get "/students/:id/excusals/:date/delete" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
@excusal = ExcusedSession.where(:date => params[:date], :student_id => :id)
halt(400, "Invalid excusal.") if @excusal.nil?
@referrer = request.referrer
erb :delete_excusal
end

post "/students/:id/excusals/:date/delete" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
@excusal = ExcusedSession.where(:date => params[:date], :student_id => params[:id])
halt(400, "Invalid excusal.") if @excusal.nil?
@excusal.delete
redirect params[:referrer]
end

get "/students/:id/new_lab_session" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
@student = Student[params[:id]]
halt(400, "Invalid student.") if @student.nil?
@referrer = request.referrer
if !params[:date].nil?
# allow prefilling the date based on a url parameter
@lab_session = OpenStruct.new(:time_in => params[:date], :time_out => params[:date])
end
erb :edit_lab_session
end

post "/students/:id/new_lab_session" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_EDIT")
student = Student[params[:id]]
halt(400, "Invalid student.") if student.nil?
student.add_lab_session(:time_in => params[:time_in], :time_out => params[:time_out],
student.add_lab_session(:time_in => DateTime.parse(params[:time_in]).utc,
:time_out => DateTime.parse(params[:time_out]).utc,
:notes => params[:notes],
:mentor_name => params[:time_out].empty? ? nil : @user.name_display)
redirect params[:referrer] || "/leader_board"
Expand All @@ -120,7 +195,8 @@ class Server < Sinatra::Base
else
mentor_name = @lab_session.mentor_name
end
@lab_session.update(:time_in => params[:time_in], :time_out => params[:time_out],
@lab_session.update(:time_in => DateTime.parse(params[:time_in]).utc,
:time_out => DateTime.parse(params[:time_out]).utc,
:notes => params[:notes], :mentor_name => mentor_name)
redirect params[:referrer] || "/leader_board"
end
Expand Down Expand Up @@ -316,6 +392,21 @@ def sms_response(messages)
END
end

get "/csv_attendance_report" do
halt(403, "Insufficient permissions.") unless @user.has_permission?("HOURS_VIEW_REPORT")
content_type "text/csv"

rows = []
rows << ["Last Name", "First Name", "Student ID", "Attendance Percentage", "Project Hours", "Total # of Sign Outs"].join(",")
DB.fetch CALENDAR_STUDENT_INFO_QUERY do |row|
student = Student[row[:student_id]]
build_percentage = ((100 * row[:required_attended_count].to_f/row[:required_count]).to_i rescue "0").to_s + "%"
rows << [student.last_name, student.first_name, student.id, build_percentage, student.project_hours, student.total_sessions_attended].join(",")
end
rows.join("\n")
end


get "/reset_hours" do
unless @user.has_permission?("DATABASE_ADMIN")
halt(400, "Need to be an administrator.")
Expand All @@ -325,4 +416,4 @@ def sms_response(messages)
"Reset Hours"
end
end
end
end
2 changes: 2 additions & 0 deletions models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
require "models/lab_session"
require "models/mentor"
require "models/mentor_checkin"
require "models/optional_build"
require "models/student"
require "models/excused_session"
4 changes: 4 additions & 0 deletions models/excused_session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ExcusedSession < Sequel::Model
unrestrict_primary_key
many_to_one :student
end
3 changes: 3 additions & 0 deletions models/optional_build.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class OptionalBuild < Sequel::Model
unrestrict_primary_key
end
1 change: 1 addition & 0 deletions models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Student < Sequel::Model
unrestrict_primary_key
one_to_many :lab_sessions
one_to_many :excused_sessions

def self.get_by_id(id)
# Try first by assuming id is the full 6-digit ID.
Expand Down
Loading

0 comments on commit 0690050

Please sign in to comment.