-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
372 lines (322 loc) · 10.8 KB
/
__init__.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import os
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, render_template
from utils import get_datetime, trunc_version, valid_username, valid_password
from operator import itemgetter
from flaskext.bcrypt import Bcrypt
from secret import secret
# create the app
app = Flask(__name__, instance_path='/var/www/wiki/instance')
app.config.from_object(__name__)
bcrypt = Bcrypt(app)
app.config.update(dict(
DATABASE=os.path.join(app.instance_path, 'wiki.db'),
DEBUG=True,
SECRET_KEY='development key',
))
app.config.from_envvar('WIKI_SETTINGS', silent=True)
"""
Setting up primary database
"""
def connect_db():
"""Connects to the specific database."""
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
def init_db():
"""Creates the database tables."""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
@app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the request."""
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
"""
The View Functions
"""
push_title = ''
push_history_edit = False
back = ''
versions = {}
"""
# view a list of possible titles
@app.route('/')
def sidebar():
db = get_db()
cur = db.execute('SELECT title, text FROM entries ORDER BY id desc')
entries = cur.fetchall()
return render_template('base.html', entries=entries)
"""
# homepage
@app.route('/')
def homepage():
global back
back = request.url
global push_title
global push_history_edit
push_title = '|'
push_history_edit = False
title = push_title
entry = query_db("SELECT * FROM entries WHERE title = ?", [title], one=True)
if entry is not None:
if not entry['current']:
push_history_edit = True
return render_template("wiki.html", entry=entry['text'], homepage=True)
elif 'username' in session and entry is None:
return redirect(url_for('edit_homepage', title=title))
else:
return redirect(url_for('login_form'))
# individual wiki page
@app.route("/<title>")
def viewpage(title):
global back
back = request.url
global push_title
global push_history_edit
push_title = title
push_history_edit = False
entry = query_db("SELECT * FROM entries WHERE title = ?", [title], one=True)
if entry is not None:
if not entry['current']:
push_history_edit = True
return render_template("wiki.html", entry=entry['text'])
elif 'username' in session and entry is None:
return redirect(url_for('editpage', title=title))
else:
return redirect(url_for('login_form'))
# edit handler just for no addon
@app.route('/_edit')
def edit_homepage():
if 'username' in session:
return render_template("edit.html", title='|')
else:
return redirect(url_for('login'))
# edit the wiki page
@app.route('/edit/<title>')
def editpage(title):
if 'username' in session:
return render_template("edit.html", title=title)
else:
return redirect(url_for('login'))
# add the new entry from the editpage handler
@app.route('/add/<title>', methods=['POST'])
def add_entry(title):
if 'username' in session:
global versions
versions[title] = 1
current_date = get_datetime()
db = get_db()
db.execute('INSERT INTO entries (title, text, my_date, version, current) VALUES (?, ?, ?, ?, ?)',
[title, request.form['content'], current_date, versions[title], True])
db.commit()
#flash('New entry was successfully posted')
if title == "|":
return redirect(url_for('homepage'))
return redirect(url_for('viewpage', title=title))
else:
return redirect(url_for('login'))
@app.route('/_update_edit')
def update_home():
entry = query_db("SELECT * FROM entries WHERE title = ?", ["|"], one=True)
return render_template("edit.html", title='|', update=True, text=entry['text'])
@app.route('/update_edit/<title>')
def update_edit(title):
entry = query_db("SELECT * FROM entries WHERE title = ?", [title], one=True)
return render_template("edit.html", title=title, update=True, text=entry['text'])
@app.route('/update/<title>', methods=['POST'])
def update_entry(title):
global versions
version = versions[title]
versions[title] += 1
current_date = get_datetime()
db = get_db()
# create a version
entry = query_db("SELECT * FROM entries WHERE title = ?", [title], one=True)
db.execute('INSERT INTO entries (title, text, my_date, version, current) VALUES (?, ?, ?, ?, ?)',
[title + "v%s" % version, entry['text'], entry['my_date'], version, False])
db.commit()
# update entry
db.execute('UPDATE entries SET text=?, my_date=?, version=?, current=? WHERE title=?', (request.form['content'], current_date, versions[title], True, title))
db.commit()
if title == "|":
return redirect(url_for('homepage'))
return redirect(url_for('viewpage', title=title))
@app.context_processor
def push_title():
global push_title
global push_history_edit
return dict(title=push_title, history_edit=push_history_edit)
@app.context_processor
def get_user():
if 'username' in session:
user = session['username']
else:
user = ''
return dict(user=user)
@app.context_processor
def utility_processor():
def trunc_version(title):
index = title.rfind('v')
return title[:index]
return dict(trunc_version=trunc_version)
"""
Primary History Functions
"""
@app.route("/_history")
def history_homepage():
global back
back = request.url
global push_title
global push_history_edit
title = "|"
push_title = title
push_history_edit = False
db = get_db()
cur = db.execute('SELECT * FROM entries ORDER BY id desc')
entries = cur.fetchall()
# create list with appropriate names
i = versions[title] - 1
history = []
while i >= 0:
if i == 0:
history.append([title])
else:
history.append([title + "v%s" % i])
i -= 1
# populate list
for entry in entries:
for version in history:
if entry['title'] == version[0]:
version.append(entry['text'])
version.append(entry['version'])
version.append(entry['my_date'])
version.append(entry['current'])
history_sorted = sorted(history, key=itemgetter(2), reverse=True)
return render_template("history_index.html", history=history_sorted, title=title)
@app.route("/history/<title>")
def history(title):
global back
back = request.url
global push_title
global push_history_edit
push_title = title
push_history_edit = False
db = get_db()
cur = db.execute('SELECT * FROM entries ORDER BY id desc')
entries = cur.fetchall()
# create list with appropriate names
i = versions[title] - 1
history = []
while i >= 0:
if i == 0:
history.append([title])
else:
history.append([title + "v%s" % i])
i -= 1
# populate list
for entry in entries:
for version in history:
if entry['title'] == version[0]:
version.append(entry['text'])
version.append(entry['version'])
version.append(entry['my_date'])
version.append(entry['current'])
history_sorted = sorted(history, key=itemgetter(2), reverse=True)
return render_template("history_index.html", history=history_sorted, title=title)
@app.route("/history_update/<title>")
def history_update(title):
if 'username' in session:
entry = query_db("SELECT * FROM entries WHERE title = ?", [title], one=True)
current_title = trunc_version(title)
return render_template("edit.html", title=current_title, update=True, text=entry['text'])
else:
return redirect(url_for('login_form'))
"""
Sessions
"""
@app.route("/signup")
def signup():
global back
if 'username' in session:
return redirect(back)
else:
return render_template('signup.html')
@app.route("/register", methods=['POST'])
def register():
global back
db = get_db()
current_date = get_datetime()
have_error = False
username = request.form['username']
password = request.form['password']
verify = request.form['verify']
user = query_db("SELECT * FROM users WHERE username = ?", [username], one=True)
params = dict(username=username)
if user:
params['error_username'] = "That username is taken"
have_error = True
if not valid_username(username):
params['error_username'] = "That's not a valid username."
have_error = True
if not valid_password(password):
params['error_password'] = "That wasn't a valid password."
have_error = True
elif password != verify:
params['error_verify'] = "Your passwords didn't match."
have_error = True
if have_error:
return render_template('signup.html', **params)
else:
hpw = bcrypt.generate_password_hash(password)
db.execute('INSERT INTO users (username, hpw, my_date) VALUES (?, ?, ?)',
[username, hpw, current_date])
db.commit()
session['username'] = username
return redirect(back)
@app.route('/login_form')
def login_form():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
global back
params = dict()
username = request.form['username']
password = request.form['password']
user = query_db("SELECT * FROM users WHERE username = ?", [username], one=True)
if user is None:
params['error_username'] = "That username doesn't exist"
return render_template('login.html', **params)
if bcrypt.check_password_hash(user['hpw'], password):
session['username'] = username
return redirect(back)
else:
params['username'] = username
params['error_password'] = "That's not the right password"
return render_template('login.html', **params)
@app.route('/logout')
def logout():
global back
# remove the username from the session if it's there
session.pop('username', None)
return redirect(back)
app.secret_key = secret()
if __name__ == '__main__':
init_db()
app.run(host='107.170.69.45')
#host='107.170.69.45'