2011-06-24 13:28:19 +02:00
|
|
|
class Admin < Sinatra::Base
|
2011-06-24 14:29:59 +02:00
|
|
|
set $settings
|
2011-06-28 10:59:49 +02:00
|
|
|
use Rack::Session::Pool, :expire_after => 1800
|
2011-06-27 09:19:28 +02:00
|
|
|
use Rack::Flash, :accessorize => [:error, :warning, :notice]
|
2011-06-26 08:57:59 +02:00
|
|
|
set :haml, :layout => :admin_layout
|
2011-08-09 11:06:53 +02:00
|
|
|
register Sinatra::CompassSupport
|
2011-06-24 14:29:59 +02:00
|
|
|
|
2011-07-01 14:14:02 +02:00
|
|
|
get '/' do
|
2011-06-30 21:39:01 +02:00
|
|
|
haml :admin_index
|
2011-06-28 11:58:41 +02:00
|
|
|
end
|
|
|
|
|
2011-06-30 21:39:01 +02:00
|
|
|
get '/post' do
|
2011-08-09 11:06:53 +02:00
|
|
|
@posts = Post.all(:order => [:id.desc], :limit => 15)
|
2011-06-30 21:39:01 +02:00
|
|
|
haml :admin_posts
|
|
|
|
end
|
|
|
|
|
2011-07-01 10:45:55 +02:00
|
|
|
get '/post/new' do
|
|
|
|
@post = Post.new
|
|
|
|
haml :admin_post_create
|
|
|
|
end
|
|
|
|
|
|
|
|
put '/post' do
|
2011-08-10 09:45:39 +02:00
|
|
|
if params[:post].has_key? 'tags'
|
2011-08-11 13:02:59 +02:00
|
|
|
tags = params[:post].delete('tags')
|
|
|
|
else
|
|
|
|
tags = []
|
2011-08-10 09:45:39 +02:00
|
|
|
end
|
2011-08-11 13:02:59 +02:00
|
|
|
@post = Post.new params[:post]
|
|
|
|
@post.set_tags tags
|
2011-07-01 10:45:55 +02:00
|
|
|
if @post.save
|
|
|
|
flash.notice = 'Post saved'
|
|
|
|
redirect "/admin/post/#{@post.id}"
|
|
|
|
else
|
|
|
|
flash.error = 'Error at saving the post'
|
|
|
|
flash[:errors] = @post.errors
|
|
|
|
redirect "/admin/post/new"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-30 21:39:01 +02:00
|
|
|
get '/post/:id' do
|
|
|
|
@post = Post.get(params[:id])
|
|
|
|
if @post
|
2011-07-01 10:45:55 +02:00
|
|
|
haml :admin_post_change
|
2011-06-28 11:58:41 +02:00
|
|
|
else
|
2011-06-30 21:39:01 +02:00
|
|
|
flash.warning = "Post with id #{params[:id]} not found!"
|
|
|
|
redirect './post'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/post/:id' do
|
|
|
|
# read the checkbox value
|
|
|
|
if params['post'].has_key?('released')
|
|
|
|
params['post']['released'] = true
|
|
|
|
else
|
|
|
|
params['post']['released'] = false
|
|
|
|
end
|
|
|
|
# get the post and update it
|
|
|
|
@post = Post.get(params[:id])
|
|
|
|
if @post
|
2011-08-09 18:05:59 +02:00
|
|
|
if params[:post].has_key? 'tags'
|
2011-09-28 15:07:05 +02:00
|
|
|
tags = params[:post].delete('tags')
|
2011-08-09 18:05:59 +02:00
|
|
|
end
|
2011-09-28 15:07:05 +02:00
|
|
|
if @post.update(params[:post])
|
|
|
|
@post.set_tags tags
|
|
|
|
@post.save
|
|
|
|
else
|
2011-06-30 21:39:01 +02:00
|
|
|
flash.warning = 'Error at saving the post!'
|
|
|
|
flash[:errors] = true
|
|
|
|
end
|
2011-07-01 10:45:55 +02:00
|
|
|
haml :admin_post_change
|
2011-06-30 21:39:01 +02:00
|
|
|
else
|
|
|
|
flash.warning = "Post with id #{params[:id]} not found!"
|
|
|
|
redirect './post'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-09 14:59:55 +02:00
|
|
|
# tags
|
|
|
|
get '/tag' do
|
|
|
|
@tags = Tag.all(:order => [:name.asc])
|
|
|
|
haml :admin_tag_index
|
|
|
|
end
|
|
|
|
|
|
|
|
put '/tag/new' do
|
|
|
|
@tag = Tag.new(:name => params['tag']['name'])
|
|
|
|
if @tag.save
|
|
|
|
flash[:notice] = "Tag '#{@tag.name}' created!"
|
|
|
|
else
|
|
|
|
flash[:warning] = "Tag '#{@tag.name} could not be created! Error was: '#{@tag.errors.first}'"
|
|
|
|
end
|
|
|
|
redirect '/admin/tag'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/tag/:id' do
|
|
|
|
@tag = Tag.first(:id => params[:id])
|
|
|
|
haml :admin_tag_edit
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/tag/:id' do
|
|
|
|
@tag = Tag.first(:id => params[:id])
|
|
|
|
@tag.name = params['tag']['name']
|
|
|
|
if @tag.save
|
|
|
|
flash[:notice] = "Tag saved"
|
|
|
|
redirect '/admin/tag'
|
|
|
|
else
|
|
|
|
flash[:notice] = "Tag could not be saved! Message: '#{@tag.errors.first}'"
|
|
|
|
haml :admin_tag_edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# login
|
2011-07-01 14:14:02 +02:00
|
|
|
get '/login' do
|
2011-06-28 10:29:44 +02:00
|
|
|
haml :admin_index_no_login
|
2011-06-24 13:28:19 +02:00
|
|
|
end
|
2011-06-27 09:19:28 +02:00
|
|
|
|
|
|
|
post '/login' do
|
|
|
|
account = Account.authenticate(params['username'], params['password'])
|
|
|
|
if account.nil?
|
|
|
|
flash.warning = 'wrong username or password'
|
2011-06-28 10:29:44 +02:00
|
|
|
flash[:username] = params['username']
|
2011-06-27 09:19:28 +02:00
|
|
|
redirect '/admin'
|
|
|
|
else
|
|
|
|
flash.notice = 'Login successful'
|
2011-06-28 10:29:44 +02:00
|
|
|
session[:id] = account.id
|
|
|
|
session[:last_updated] = Time.now
|
2011-06-30 21:39:01 +02:00
|
|
|
# redirect to the url set from the #before block
|
|
|
|
if session.has_key? :to_path
|
|
|
|
redirect "/admin#{session.delete(:to_path)}"
|
|
|
|
else
|
2011-07-04 11:15:29 +02:00
|
|
|
redirect '/admin/'
|
2011-06-30 21:39:01 +02:00
|
|
|
end
|
2011-06-27 09:19:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-28 10:29:44 +02:00
|
|
|
get '/logout' do
|
2011-06-28 10:57:24 +02:00
|
|
|
session[:id] = nil
|
|
|
|
session[:last_updated] = nil
|
2011-06-28 10:29:44 +02:00
|
|
|
flash.notice = 'Logout complete'
|
2011-08-09 11:06:53 +02:00
|
|
|
redirect '/'
|
2011-06-28 10:29:44 +02:00
|
|
|
end
|
|
|
|
|
2011-06-27 09:19:28 +02:00
|
|
|
get '/stylesheet.css' do
|
|
|
|
scss :admin_stylesheet
|
|
|
|
end
|
2011-06-28 10:29:44 +02:00
|
|
|
|
|
|
|
helpers do
|
2011-06-28 11:58:41 +02:00
|
|
|
def session_read
|
|
|
|
if (session.has_key?(:id) && session.has_key?(:last_updated) &&
|
|
|
|
Time.now - session[:last_updated] < 1800)
|
|
|
|
Account.get(session[:id])
|
2011-06-28 10:29:44 +02:00
|
|
|
else
|
2011-06-28 11:58:41 +02:00
|
|
|
nil
|
2011-06-28 10:29:44 +02:00
|
|
|
end
|
|
|
|
end
|
2011-06-30 21:39:01 +02:00
|
|
|
|
|
|
|
def keys_to_sym hash
|
|
|
|
new_hash = {}
|
|
|
|
hash.each do |k, v|
|
|
|
|
new_hash[k.to_sym] = v
|
|
|
|
end
|
|
|
|
hash = new_hash
|
|
|
|
end
|
2011-08-09 11:06:53 +02:00
|
|
|
|
|
|
|
def markup content, markup
|
|
|
|
markup = markup.to_sym
|
|
|
|
if respond_to? markup
|
|
|
|
send markup, content
|
|
|
|
else
|
|
|
|
content
|
|
|
|
end
|
|
|
|
end
|
2011-06-30 21:39:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
@account = session_read
|
|
|
|
end
|
|
|
|
|
2011-07-01 14:14:02 +02:00
|
|
|
before %r{^(?!\/(login|stylesheet\.css)+$)} do
|
2011-06-30 21:39:01 +02:00
|
|
|
if @account.nil?
|
|
|
|
flash.warning = 'You are not logged in!'
|
|
|
|
session[:to_path] = request.path_info
|
2011-07-01 14:14:02 +02:00
|
|
|
redirect '/admin/login'
|
2011-06-30 21:39:01 +02:00
|
|
|
else
|
|
|
|
session[:last_updated] = Time.now
|
|
|
|
end
|
2011-06-28 10:29:44 +02:00
|
|
|
end
|
2011-06-24 13:28:19 +02:00
|
|
|
end
|