-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkey.py
More file actions
202 lines (170 loc) · 6.69 KB
/
monkey.py
File metadata and controls
202 lines (170 loc) · 6.69 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
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
# -*- coding: utf-8 -*-
"""
Flaskr
~~~~~~
A microblog example application written as Flask tutorial with
Flask and sqlite3.
:copyright: (c) 2014 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
Turned into monkey database by Paavo Kivistö
"""
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
# create our little application :)
app = Flask(__name__)
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE='monkey.db',
DEBUG=True,
SECRET_KEY='development key',
USERNAME='admin',
PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
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()
g.sqlite_db.execute('pragma foreign_keys = on')
return g.sqlite_db
@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()
@app.route('/')
@app.route('/<int:page>')
@app.route('/<int:page>/<string:sort>')
def show_entries(page=1, sort='id'):
db = get_db()
cur = db.execute('select mail from entries')
entries = cur.fetchall()
for entry in entries:
cur = db.execute('select * from entries where mail in (select mail1 as mail from friends where mail2=? union select mail2 as mail from friends where mail1=?)', [entry['mail'], entry['mail']])
number = len(cur.fetchall());
db.execute('update entries set friend=? where mail=?', [number, entry['mail']]);
db.commit()
if sort == 'id':
cur = db.execute('select * from entries order by id desc')
elif sort == 'age':
cur = db.execute('select * from entries order by age asc')
elif sort == 'name':
cur = db.execute('select * from entries order by name asc')
elif sort == 'best':
cur = db.execute('select * from entries order by best asc')
elif sort == 'friend':
cur = db.execute('select * from entries order by friend desc')
else:
flash('Unknown sorting argument.');
cur = db.execute('select * from entries order by id desc')
entries = cur.fetchall()
return render_template('show_entries.html', entries=entries, page=page, sort=sort)
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('insert into entries (name, age, mail, text) values (?, ?, ?, ?)',
[request.form['name'], request.form['age'], request.form['mail'], request.form['text']])
db.commit()
flash('New monkey was successfully added')
return redirect(url_for('show_entries'))
@app.route('/edit', methods=['POST'])
def edit_entry():
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('update entries set name=?, age=?, mail=?, text=? where mail=?', [request.form['name'], request.form['age'], request.form['mail'], request.form['text'], request.form['oldmail']])
db.commit()
flash('Monkey was successfully edited')
return redirect(url_for('show_entries'))
@app.route('/delete', methods=['POST'])
def delete_entry():
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('delete from entries where mail=?', [request.form['mail']])
db.commit()
flash('Monkey was successfully removed from the database.')
return redirect(url_for('show_entries'))
@app.route('/friends/<friend1>&<friend2>', )
def add_friends(friend1, friend2):
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('insert into friends (mail1, mail2) values (?,?)', [friend1, friend2])
db.commit()
flash('Friendship added.')
return redirect(url_for('profile',mail=friend2))
@app.route('/delfriends/<friend1>&<friend2>', )
def delete_friends(friend1, friend2):
if not session.get('logged_in'):
abort(401)
db = get_db()
db.execute('delete from friends where (mail1=? and mail2=?) or (mail1=? and mail2=?)', [friend1, friend2, friend2, friend1])
db.execute('update entries set best=null where (mail=? and best=?) or (mail=? and best=?)', [friend1, friend2, friend2, friend1])
db.commit()
flash('Friendship removed.')
return redirect(url_for('profile',mail=friend2))
@app.route('/best/<string:mail>')
@app.route('/best/<string:best>&<string:mail>')
def make_best(mail, best=None):
if not session.get('logged_in'):
abort(401)
db = get_db()
if best is None:
db.execute('update entries set best=null where mail=?', [mail])
else:
db.execute('update entries set best=? where mail=?', [best, mail])
db.commit()
flash('Best friend set for the monkey.')
return redirect(url_for('profile',mail=mail))
@app.route('/profile/<mail>/')
def profile(mail):
db = get_db()
cur = db.execute('select * from entries where mail=?', [mail])
entry = cur.fetchone()
if entry is None:
flash('Profile not found')
return redirect(url_for('show_entries'))
cur = db.execute('select * from entries where mail in (select mail1 as mail from friends where mail2=? union select mail2 as mail from friends where mail1=?)', [mail, mail])
friends = cur.fetchall()
cur = db.execute('select * from entries where mail<>? and mail not in (select mail1 as mail from friends where mail2=? union select mail2 as mail from friends where mail1=?)', [mail, mail, mail])
nonfriends = cur.fetchall()
return render_template('show_profile.html', entry=entry, friends=friends, nonfriends=nonfriends)
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))
if __name__ == '__main__':
init_db()
app.run()