-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtachatacha.rb
120 lines (103 loc) · 2.3 KB
/
tachatacha.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require 'sinatra'
require 'dm-core'
require 'appengine-apis/users'
require 'creare'
DataMapper.setup(:default, "appengine://auto")
class Article
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
property :ctime, Integer
property :mtime, Integer
end
helpers do
include Rack::Utils
alias_method :e, :escape
alias_method :h, :escape_html
def add_dashes(str)
str.gsub(/ /, '-')
end
def auth_editor?
AppEngine::Users.logged_in? && AppEngine::Users.admin?
end
def strip_dashes(str)
str.gsub(/\-/, ' ')
end
def u(str)
#CGI::unescape(str)
str
end
end
get '/' do
@articles = Article.all(:order => [ :mtime.desc], :limit => 40)
erb :index
end
get '/404' do
erb :notfound
end
get '/article/:title' do
clean_title = strip_dashes params[:title]
@article = Article.first(:title => clean_title)
redirect "/edit/#{params[:title]}" unless @article
@hbody = Creare.creolize(@article.body)
erb :article
end
get '/edit' do
redirect "/edit/#{params[:title]}"
end
get '/edit/' do
erb :emptyq
end
get '/edit/:title' do
redirect '/404' unless auth_editor?
@title = strip_dashes params[:title]
@article = Article.first(:title => u(params[:title]))
@body = (@article) ? @article.body : ''
erb :edit
end
get '/infodump' do
pout = "<p>" + request.inspect
if AppEngine::Users.logged_in?
u = AppEngine::Users.current_user()
pout << "<p>email: #{u.email}"
pout << "<p>nickname: #{u.nickname}"
end
pout
end
get '/login' do
redirect AppEngine::Users.create_login_url('/')
end
get '/logout' do
redirect AppEngine::Users.create_logout_url('/')
end
get '/unauthorized' do
erb :unauthorized
end
not_found do
erb :notfound
end
post '/edit' do
redirect '/unauthorized' unless auth_editor?
@title = params[:title]
ts = Time.now.to_i
@article = Article.first(:title => u(params[:title]))
if (@article)
if params[:body].strip.length > 0
@article[:body] = params[:body]
@article[:mtime] = ts
else
@article.destroy
redirect '/'
end
else
@article = Article.new(
:title => params[:title],
:body => params[:body],
:ctime => ts, :mtime => ts
)
end
@article.save
pretty_title = @title.gsub(/ /, '-')
redirect "/article/#{pretty_title}"
end