<commit_before>class Trackodoro < Sinatra::Base
  #register Sinatra::Reloader if CONFIG['development']


  ###
  ### PUBLIC SIDE
  ###

  get '/' do
    if warden_handler.authenticated?
      redirect "/home"
    else
      @users = User.all
      erb :'public/index', :layout => :'public/layout'
    end
  end

  get "/login" do
    session[:temp]!=nil ? @message = session.delete(:temp) : nil
    redirect '/home' if warden_handler.authenticated?
    erb :'public/login', :layout => :'public/layout'
  end

  post "/session" do
    warden_handler.authenticate!
    if warden_handler.authenticated?
      redirect "/"
    else
      redirect "/login"
    end
  end

  get "/logout" do
    warden_handler.logout
    redirect '/'
  end

  post "/unauthenticated" do
    session[:temp] = "Invalid username or password"
    redirect "/login"
  end

  get "/signup" do
    erb :'public/signup', :layout => :'public/layout'
  end

  post "/signup" do
    if(params["email"].match(/\A[^@]+@[^@]+\Z/))
      # Add :activated = false, and send mail to confirm
      @user = User.new(:email => params["email"])
      @user.password = params["password"]
      @user.password_confirmation = params["password"] # yes I know....
      @user.save
    end
    redirect "/login"
  end


  ###
  ### USER SIDE - See '/' for index
  ###

  get "/home" do
    check_authentication
    @user = User[current_user]
    now = Time.now()
    @chart_data = {
      # CAREFUL NO SUPPORT FOR NEW YEAR !!
      week_pomodoros: @user.pomodoros.map{|p| p.h.year==now.year && p.h.yday.between?(now.yday, now.yday-6) },
      week_dates: (Array.new(7) {|d| (now-86400*d)}).reverse,
    }
    erb :'users/index', :layout => :'users/layout'
  end

  post "/add" do
    check_authentication
    puts Time.at(params[:day].to_i)
    User[current_user].add_pomodoro(Pomodoro.new(:h => Time.at(params[:day].to_i)))
    redirect back
  end


  ###
  ### USER MANAGEMENT
  ###

  use Warden::Manager do |manager|
    manager.default_strategies :password
    manager.failure_app = Trackodoro
    manager.serialize_into_session {|user| user.id}
    manager.serialize_from_session {|id| User.get(id)}
  end

  Warden::Manager.before_failure do |env,opts|
    env['REQUEST_METHOD'] = 'POST'<commit_msg>Forgot to set hour to completed pomodoro<commit_after>class Trackodoro < Sinatra::Base
  #register Sinatra::Reloader if CONFIG['development']


  ###
  ### PUBLIC SIDE
  ###

  get '/' do
    if warden_handler.authenticated?
      redirect "/home"
    else
      @users = User.all
      erb :'public/index', :layout => :'public/layout'
    end
  end

  get "/login" do
    session[:temp]!=nil ? @message = session.delete(:temp) : nil
    redirect '/home' if warden_handler.authenticated?
    erb :'public/login', :layout => :'public/layout'
  end

  post "/session" do
    warden_handler.authenticate!
    if warden_handler.authenticated?
      redirect "/"
    else
      redirect "/login"
    end
  end

  get "/logout" do
    warden_handler.logout
    redirect '/'
  end

  post "/unauthenticated" do
    session[:temp] = "Invalid username or password"
    redirect "/login"
  end

  get "/signup" do
    erb :'public/signup', :layout => :'public/layout'
  end

  post "/signup" do
    if(params["email"].match(/\A[^@]+@[^@]+\Z/))
      # Add :activated = false, and send mail to confirm
      @user = User.new(:email => params["email"])
      @user.password = params["password"]
      @user.password_confirmation = params["password"] # yes I know....
      @user.save
    end
    redirect "/login"
  end


  ###
  ### USER SIDE - See '/' for index
  ###

  get "/home" do
    check_authentication
    @user = User[current_user]
    now = Time.now()
    @chart_data = {
      # CAREFUL NO SUPPORT FOR NEW YEAR !!
      week_pomodoros: @user.pomodoros.map{|p| p.h.year==now.year && p.h.yday.between?(now.yday, now.yday-6) },
      week_dates: (Array.new(7) {|d| (now-86400*d)}).reverse,
    }
    erb :'users/index', :layout => :'users/layout'
  end

  post "/add" do
    check_authentication
    p_time_a = Time.at(params[:day].to_i).to_a
    p_time_a[0] = 0
    p_time_a[1] = 0
    p_time_a[2] = params[:hour].to_i
    p_time = Time.local(*p_time_a)
    User[current_user].add_pomodoro(Pomodoro.new(:h => p_time))
    redirect back
  end


  ###
  ### USER MANAGEMENT
  ###

  use Warden::Manager do |manager|
    manager.default_strategies :password
    manager.failure_app = Trackodoro
    manager.serialize_into_session {|user| user.id}
    manager.serialize_from_session {|id| User.get(id)}
  end

  Warden::Manager.before_failure do |env,opts|
    env['REQUEST_METHOD'] = 'POST'
{'fraction_non_alphanumeric': 0.15310077519379844, 'fraction_numerical': 0.00409130060292851, 'mean_word_length': 2.9066442388561815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1380758', 'n_tokens_mistral': 1693, 'n_tokens_neox': 1618, 'n_words': 355}