-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
209 lines (164 loc) · 5.72 KB
/
server.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
from flask import Flask, render_template, request, session, g, redirect, url_for, jsonify
import flask_socketio
import flask_login
import sqlite3
from models.colonne import Colonne,ColonneForDisplay
from models.tableaux import Tableau,TableauForDisplay
from models.user import User, UserForLogin, ConnectedUser
from models.post import Post, PostForDisplay
import uuid
DATABASE = '.data/db.sqlite'
app = Flask(__name__)
app.secret_key = 'mysecret!'
io = flask_socketio.SocketIO(app)
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login_get'
@login_manager.user_loader
def load_user(email):
db = get_db()
cur = db.cursor()
return UserForLogin.getByEmail(cur, email)
##############################################################################
# BOILERPLATE CODE (you can essentially ignore this) #
##############################################################################
def get_db():
"""Boilerplate code to open a database
connection with SQLite3 and Flask.
Note that `g` is imported from the
`flask` module."""
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = make_dicts
return db
def make_dicts(cursor, row):
return dict((cursor.description[idx][0], value)
for idx, value in enumerate(row))
@app.teardown_appcontext
def close_connection(exception):
"""Boilerplate code: function called each time
the request is over."""
db = getattr(g, '_database', None)
if db is not None:
db.close()
##############################################################################
# APPLICATION CODE (read from this point!) #
##############################################################################
@app.route("/")
@flask_login.login_required
def home():
db = get_db()
cur = db.cursor()
tableau_list=TableauForDisplay.getAll(cur)
return render_template('index.html',tableau = tableau_list )
@app.route("/login", methods=['POST'])
def login_post():
email = request.form.get('email')
password = request.form.get('password')
remember = request.form.get('remember_me')
if not email or not password:
return render_template(
'login.html',
error_msg="Please provide your email and your password.",
)
db = get_db()
cur = db.cursor()
user = UserForLogin.getByEmail(cur, email)
if user is None or not user.check_password(password):
return render_template(
'login.html',
error_msg="Authentication failed",
)
flask_login.login_user(user, remember=remember)
return redirect(url_for('home'))
@app.route('/login', methods=['GET'])
def login_get():
return render_template('login.html')
@app.route('/register', methods=['GET'])
def register_get():
return render_template('register.html')
@app.route("/register", methods=['POST'])
def register_post():
email = request.form.get('email')
name = request.form.get('name')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
if not email or not name or not password1 or not password2:
return render_template(
'register.html',
error_msg="Please provide your email, name and password.",
)
if password1 != password2:
return render_template(
'register.html',
error_msg="The passwords do not match!",
)
user = User(name=name, email=email, password=password1)
db = get_db()
cur = db.cursor()
try:
user.insert(cur)
except sqlite3.IntegrityError:
return render_template(
'register.html',
error_msg="This email is already registered.",
)
db.commit()
return redirect(url_for('login_get'))
@app.route('/is-email-used/<email>')
def is_email_used(email):
db = get_db()
cur = db.cursor()
user = UserForLogin.getByEmail(cur, email)
free = user is None
return jsonify({"email": email, "free": free})
@app.route('/logout', methods=['GET'])
@flask_login.login_required
def logout():
flask_login.logout_user()
return redirect(url_for('login_get'))
@app.route('/loged', methods=['GET'])
def loged_get():
db = get_db()
cur = db.cursor()
tableau_list=TableauForDisplay.getAll(cur)
return render_template('index.html',tableau = tableau_list )
@app.route('/loged', methods=['POST'])
def loged_post():
task = request.form.get('task')
tableau = Tableau(str(uuid.uuid4()),task)
db = get_db()
cur = db.cursor()
try:
tableau.insert(cur)
except sqlite3.IntegrityError:
print("error")
db.commit()
tableau_list=TableauForDisplay.getAll(cur)
#print(tableau_list)
#tableau_list=["123","123"]
#x=len(tableau_list)
return render_template('index.html' , tableau = tableau_list )
@app.route('/tableau', methods=['GET'])
def tab_get():
db = get_db()
cur = db.cursor()
colonnes_list=ColonneForDisplay.getAllForTab(cur,request.args.get('tab'))
print(colonnes_list)
return render_template('tableau.html',tab_id=request.args.get('tab'),colonnes_list=colonnes_list)
@app.route('/ajout_colonne', methods=['POST'])
def ajouter_colonne():
tab_id=request.form.get('tableau')
colonne_nom=request.form.get('colonne')
colonne=Colonne(str(uuid.uuid4()),colonne_nom,tab_id)
db = get_db()
cur = db.cursor()
try:
colonne.insert(cur)
except sqlite3.IntegrityError:
print("error")
db.commit()
return redirect("tableau?tab="+tab_id, code=302)
if __name__ == '__main__':
io.run(app, debug=True)