-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
323 lines (253 loc) · 10.7 KB
/
app.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
import os
import sys
from datetime import datetime
from flask import Flask,render_template ,redirect
from flask import flash,session ,request ,url_for
from flask_session import Session
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy.exc import IntegrityError
from flask_sqlalchemy import SQLAlchemy
# ------------ Owen Lib ------------- #
from subpython.validate import login_required
from subpython.form import LoginForm , RegisterForm
from subpython.config import Development,Production
import subpython.validate as validate
app = Flask(__name__)
app.config.from_object(Production)
# data base config
db = SQLAlchemy(app)
app.config["SESSION_TYPE"] = "filesystem"
app.config["SESSION_PERMANENT"] = False
Session(app)
# session config
'''with app.app_context():
if "app.db" not in os.listdir():
db.create_all()
print(" * DB Created :)")
'''
@app.before_first_request
def create_tables():
db.create_all()
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64) ,nullable=False ,unique=True)
password = db.Column(db.String(256) ,nullable=False,unique=False)
# 0 = New user || 1 = Old User
new_user = db.Column(db.Integer, default=0)
date = db.Column(db.DateTime ,default=datetime.now())
class Task(db.Model):
__tablename__ = "tasks"
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer,db.ForeignKey('users.id'))
task_title = db.Column(db.String(64) ,nullable=False ,unique=False)
task_info = db.Column(db.String(256) ,nullable=False,unique=False)
# 0 = on Doing || 1 = Done!
status = db.Column(db.Integer ,default = 0)
date = db.Column(db.DateTime ,default=datetime.now())
# class History(db.Model):
# __tablename__ = "history"
# id = db.Column(db.Integer, primary_key=True)
# user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# task_title = db.Column(db.String(64) ,nullable=False ,unique=False)
# task_info = db.Column(db.String(256) ,nullable=False,unique=False)
# # 0 = on Doing || 1 = Done! || 3 = Delete
# status = db.Column(db.Integer ,default=0)
# Error Handler for 404 or 500
@app.errorhandler(500)
def error_500_server(e):
return render_template('500.html'),500
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
@app.route("/")
@login_required
def index():
user_task = []
if session.get("user_id"):
# get user all task from data base
user_db_data = Task.query.filter_by(user_id=(session['user_id'])).all()
for index,value in enumerate(user_db_data):
print(value.status)
if value.status != 1:
user_task.append(value)
return render_template("index.html",user_db=user_task,user_id=session['user_id'])
else:
return redirect("login")
@app.route("/login",methods=["POST","GET"])
def login():
if request.method == "GET":
session.clear()
form_login = LoginForm()
return render_template("login.html", form=form_login,error=False)
# POST
if request.method == "POST":
form_login = LoginForm(request.form)
if form_login.validate():
username = request.form.get("username")
password = request.form.get("password")
checkbox = request.form.get("checkbox")
# safety check
if not validate.validate_field(username):
return render_template("login.html", form = form_login ,error=True,error_message="Username is invalid :(")
if not validate.validate_field(password):
return render_template("login.html", form = form_login ,error=True,error_message="Password is invalid :(")
# check username and password
user = User.query.filter_by(username=username).first()
if not user:
return render_template("login.html", form=form_login,error=True,error_message="information Wrong :(")
if user.username != username:
return render_template("login.html", form=form_login,error=True,error_message="username is Wrong :(")
pass_db = user.password
if not check_password_hash(pass_db,password):
return render_template("login.html", form=form_login,error=True,error_message="Password is Wrong :(")
# add user id in db to user session
session["user_id"] = user.id
# check its first time user log in to web site to show welcome message
if user.new_user == 0:
user.new_user = 1
db.session.commit()
flash(f" Welcome Dear {user.username}")
first = validate.first_login
return redirect('/')
else:
return redirect("/")
else:
return render_template("login.html", form=form_login,error=True, error_message="Invalid Inputs")
@app.route("/register",methods=["POST","GET"])
def register():
# GET
if request.method == "GET":
form = RegisterForm()
return render_template("register.html",form=form,error=False)
# POST
if request.method == "POST":
form = RegisterForm(request.form)
if form.validate():
username = request.form.get("username")
password = request.form.get("password")
password_re = request.form.get("password_re")
# safety check
if not validate.validate_field(username):
return render_template("register.html",form=form,error=True,error_message="Username Is invalid :(")
password_validation = validate.validate_passwords(password,password_re)
if password_validation == "NS":
return render_template("register.html",form=form,error=True,error_message="Passwords Are Not Match :(")
elif password_validation == False:
return render_template("register.html",form=form,error=True,error_message="Passwords are Wrong")
# check user is not duplicated
user_check = User.query.filter_by(username=username).first()
if user_check:
return render_template("register.html",form=form,error=True,error_message="Username Already Take by Another User")
# add it ro to data base
new_user = User(username=username,password=generate_password_hash(password))
db.session.add(new_user)
db.session.commit()
# query to data base
user_in_db = User.query.filter_by(username=username).first()
if not user_in_db:
return error_500_server()
# add first column(welcome message too user task column)
new_task = Task(user_id=user_in_db.id,task_title=validate.first_login[1],task_info=validate.first_login[2])
db.session.add(new_task)
db.session.commit()
flash(f"Register complete {username} :) ")
return redirect('login')
else:
return render_template("register.html",form=form,error=True,error_message="Invalid Inputs :(")
@app.route("/logout")
@login_required
def logout():
session.clear()
return redirect("/")
@app.route("/add_new_task",methods=['POST'])
@login_required
def add_new_task():
title = request.form.get("Task_Info")
info = request.form.get("Task_Name")
if not validate.validate_tasks(title):
return redirect('/')
if not validate.validate_tasks(info):
return redirect('/')
# add task to user db
new_task = Task(user_id=session['user_id'],task_info=info.title(),task_title=title.title())
try:
db.session.add(new_task)
db.session.commit()
flash("Task Added SuccessFully")
return redirect('/')
except:
return error_500_server()
@app.route("/edit",methods=["POST","GET"])
@login_required
def edit():
if request.method == "GET":
return render_template("edit.html",post_id=request.args.get("post_id"))
if request.method == "POST":
# edit section
title = request.form.get("Edit_Task_Name")
info = request.form.get("Edit_Task_Info")
task_id = request.form.get("post_id")
if not validate.validate_tasks(title):
return redirect('/')
if not validate.validate_tasks(info):
return redirect('/')
if not validate.validate_tasks(task_id):
return redirect('/')
try:
new_task = Task.query.filter_by(id=task_id).first()
if not new_task:
return redirect("/")
new_task.task_title=title.title()
new_task.task_info=info.title()
db.session.commit()
flash("Task Edited Successfully")
return redirect("/")
except:
return error_500_server
@app.route("/delete",methods=["POST","GET"])
@login_required
def delete():
if request.method == "GET":
return render_template("delete.html",post_id=request.args.get('post_id'))
if request.method == "POST":
if request.form.get('delete') == "Yes":
try:
new_task = Task.query.filter_by(id=request.form.get('post_id'),user_id=session["user_id"]).first()
if new_task == None:
return redirect("/")
db.session.delete(new_task)
db.session.commit()
flash("Task Deleted successfully")
return redirect("/")
except:
flash(message="Invalid Task",category="error")
return redirect("/")
else:
return redirect("/")
else:
return redirect("/")
@app.route("/done" ,methods=["POST"])
@login_required
def done():
if request.method == "POST":
task_id=request.form.get("post_id")
# query to data base for change task status to done
new_task = Task.query.filter_by(id=task_id ,user_id=session["user_id"]).first()
if new_task == None:
return redirect("/")
# status code 1 equal to done
new_task.status = 1
print(new_task.status)
print(new_task)
db.session.commit()
flash("Task Done. You can See Your all Done Tasks In history Section")
return redirect("/")
@app.route("/history")
@login_required
def history():
# query to db to get all Done Task
old_Task = Task.query.filter_by(status=1,user_id=session["user_id"])
return render_template("history.html",user_db=old_Task)
if __name__ == "__main__":
app.run()