Created
October 24, 2010 16:32
-
-
Save carlhoerberg/643657 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra/base' | |
require 'haml' | |
require 'models' | |
require 'rack-flash' | |
require 'pony' | |
require 'mime/types' | |
class App < Sinatra::Base | |
enable :sessions | |
use Rack::Flash | |
set :haml, {:escape_html => true} | |
configure do | |
DataMapper::Logger.new($stdout, :debug) | |
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/dev.db") | |
DataMapper.finalize | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => ENV['AMAZON_ACCESS_KEY_ID'] || '', | |
:secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'] || '' | |
) | |
end | |
before do | |
content_type :html, 'charset' => 'utf-8' | |
end | |
get '/' do | |
login_required | |
@user = current_user | |
haml :index | |
end | |
get '/profile' do | |
required_role :student | |
@profile = current_user.profile | |
if @profile.nil? | |
flash[:notify] = "You haven't created a profile yet, fill in the form below and then continue" | |
redirect '/profile/edit' | |
else | |
@canEdit = true | |
haml :profile | |
end | |
end | |
get '/profile/edit' do | |
required_role :student | |
@profile = current_user.profile || Profile.new | |
haml :profileedit | |
end | |
post '/profile/edit' do | |
required_role :student | |
@profile = current_user.profile || Profile.new(:user => current_user) | |
@profile.attributes = params | |
@profile.save ? redirect('/profile') : haml(:profileedit) | |
end | |
get '/profile/upload' do | |
required_role :student | |
if current_user.profile.nil? | |
flash[:notice] = "You have to have a complete profile before you can upload a CV" | |
return redirect '/profile/edit' | |
end | |
haml :upload | |
end | |
post '/profile/upload' do | |
required_role :student | |
cv = CvFile.new(:profile => current_user.profile, :isEnglish => params[:isEnglish]) | |
cv.store_file(params[:file]) | |
cv.save ? redirect('/profile') : haml(:upload) | |
end | |
post '/profile/delete/:id' do | |
required_role :student | |
file = CvFile.get(params[:id]) | |
file.destory | |
redirect '/profile/edit' | |
end | |
get '/profiles' do | |
required_role :corp | |
@profiles = Profile.all | |
haml :profiles | |
end | |
get '/profile/:id' do | |
required_role :corp | |
@profile = Profile.get(params[:id]) | |
haml :profile | |
end | |
get '/cv/:id' do | |
cv = CvFile.get params[:id] | |
file = cv.get_file | |
attachment file.key | |
content_type file.content_type | |
last_modified file.last_modified | |
file.value | |
end | |
get '/profiles/all.zip' do | |
required_role :corp | |
send_data CvFile.get_all_as_zip, :filename => all.zip, :type => 'application/zip' | |
end | |
helpers do | |
def login_required | |
halt 401 if current_user.nil? | |
end | |
def required_role(role) | |
login_required | |
halt 401 unless current_user.roles.include? role | |
end | |
def current_user | |
env['warden'].user | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a js function for interrupting the form submit, check whether a number is in use, and if it is, ask the user if to proceed or not | |
var checkOrgNr = function () { | |
var hasConfirmed = false; | |
$("form:first").submit(function () { | |
if (hasConfirmed) return true; | |
var button = $(this).find(":submit:first"); | |
button.attr("disabled", "disabled"); | |
var form = $(this); | |
$.getJSON(url, { "orgnr": $("#OrganisationsNr").val() }, function (result) { | |
if (result == false || confirm("There's already a customer with the same organisation number, are you sure you want to continue?")) { | |
hasConfirmed = true; | |
form.submit(); | |
} | |
button.removeAttr("disabled"); | |
}); | |
return false; | |
}); | |
}; | |
// usage: | |
// $(checkOrgNr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'dm-core' | |
require 'dm-types' | |
require 'dm-validations' | |
require 'zippy' | |
require 'aws/s3' | |
class User | |
include DataMapper::Resource | |
property :id, Serial | |
property :email, String, :unique => true, :required => true, :format => :email_address | |
property :password, BCryptHash | |
property :roles, Flag[:student, :corp, :admin], :default => :student | |
has 1, :profile | |
def self.authenticate(email, password) | |
u = User.first(:email => email) | |
return false if u.nil? | |
u.password == password ? u : false | |
end | |
end | |
class Profile | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String, :required => true | |
property :telephone, String | |
property :address, String | |
property :presentation, Text | |
property :program, String | |
property :hp, Integer | |
property :year, Integer | |
property :wantsExjobb, Boolean | |
property :wantsAnstallning, Boolean | |
property :wantsSommarjobb, Boolean | |
property :wantsSamtal, Boolean | |
belongs_to :university | |
belongs_to :user | |
has n, :cv_files | |
end | |
class University | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String, :unique => true, :required => true | |
has n, :profiles | |
end | |
class CvFile | |
include DataMapper::Resource | |
property :id, Serial | |
property :filename, String, :writer => :private, :required => true | |
property :isEnglish, Boolean, :required => true, :default => false | |
belongs_to :profile | |
before :destroy do |file| | |
AWS::S3::S3Object.delete(file.filename, 'datatjejcv') | |
end | |
def self.get_all_as_zip | |
zipFile = Zippy.new do |zip| | |
all().each do |cvfile| | |
cv = cvfile.get_file | |
zip[cv.key] = cv.value | |
end | |
end | |
zipFile.data | |
end | |
def store_file(file) | |
self[:filename] = "#{profile.user.email}/#{file[:filename]}" | |
AWS::S3::S3Object.store(filename, open(file[:tempfile]), 'datatjejcv') | |
end | |
def s3_url() | |
AWS::S3::S3Object.url_for self[:filename], 'datatjejcv' | |
end | |
def get_file() | |
AWS::S3::S3Object.find self[:filename], 'datatjejcv' | |
end | |
def random_str(len) | |
chars = ("a".."z").to_a + ("0".."9").to_a | |
newpass = "" | |
1.upto(len) { |i| newpass << chars[rand(chars.size-1)] } | |
return newpass | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment