-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (52 loc) · 2.16 KB
/
Copy pathapp.py
File metadata and controls
67 lines (52 loc) · 2.16 KB
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
from flask import Flask, request, redirect, Markup
import db
import markdown
import random
numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
actions = ['plus', 'minus', 'multiplied by']
def perform_math(arg1, arg2, action):
if action == 'plus':
return numbers.index(arg1) + numbers.index(arg2)
elif action == 'minus':
return numbers.index(arg1) - numbers.index(arg2)
else:
return numbers.index(arg1) * numbers.index(arg2)
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def main():
if request.method == 'POST':
return '{0}{1}\n'.format(request.url_root, db.new_paste(request.form['paste']))
content = db.get_paste(1) # the main page must be stated as paste (id(1))
return Markup(markdown.markdown(content))
@app.route('/<int:pasteid>') # FIXME :: syntax hl, indent is ugly
def pasteid(pasteid):
paste = db.get_paste(pasteid)
if request.args.get('term') in ('true', 'yes', 'da'):
pass
else:
paste = Markup(u'<pre>{0}</pre>').format(paste)
return paste
@app.route('/new', methods=['POST', 'GET'])
def form():
if request.method == 'POST':
arg1 = request.form['arg1']
arg2 = request.form['arg2']
action = request.form['action']
try:
result = int(request.form['result'], 0)
except Exception:
return 'wrong captcha'
if perform_math(arg1, arg2, action) == result:
return redirect('{0}{1}'.format(request.url_root, db.new_paste(request.form['paste'])))
return 'wrong captcha'
return '''
<form method="POST" action="/new">
<textarea name="paste" cols="80" rows="25"></textarea><br />
{arg1} {action} {arg2} = <input type="text" name="result" />
<input type="hidden" name="arg1" value="{arg1}" />
<input type="hidden" name="arg2" value="{arg2}" />
<input type="hidden" name="action" value="{action}" />
<input type="submit" value="Paste" />
</form>'''.format(arg1=random.choice(numbers), arg2=random.choice(numbers), action=random.choice(actions))
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)