forked from SeoGangMin/PythonRestApiTemp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaskr_crud.py
91 lines (71 loc) · 2.3 KB
/
flaskr_crud.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
from database import init_db
from database import db_session
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
from models import User
import json
app = Flask(__name__)
app.config.update(dict(
DEBUG=True,
SECRET_KEY='development key',
USERNAME='admin',
PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
@app.before_request
def before_request():
print 'before_request'
@app.teardown_request
def teardown_request(exception):
print 'teardown_request'
@app.route('/')
def show_entries():
users_query = db_session.query(User)
entries = [dict(name=user.name, email=user.email, description=user.description) for user in users_query]
print len(entries)
return json.dumps(entries)
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
u = User(request.form['name'], request.form['email'])
db_session.add(u)
db_session.commit()
flash('New User was successfully posted')
return redirect(url_for('show_entries'))
@app.route('/update', methods=['POST'])
def update_entry():
u = db_session.query(User).filter(User.name==request.form['name']).first()
u.description = 'my description'
db_session.commit()
print '##########'
print u.description
return redirect(url_for('show_entries'))
@app.route('/delete', methods=['POST'])
def delete_entry():
u = User(request.form['name'], request.form['email'])
db_session.query(User).filter(User.name==u.name).delete()
db_session.commit()
flash('Selected User was successfully deleted')
return redirect(url_for('show_entries'))
@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()