-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
87 lines (71 loc) · 2.13 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
from flask import Flask,render_template,request,redirect,url_for,jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgres://postgres@localhost:5432/postgres'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db=SQLAlchemy(app)
migrate=Migrate(app,db)
class student(db.Model):
id=db.Column(db.Integer,primary_key=True)
name=db.Column(db.String(),nullable=True)
completed=db.Column(db.Boolean,nullable=False)
list_id=db.Column(db.Integer,db.ForeignKey('todolists.id'),nullable=False)
def __repr__(self):
return f'<{self.id} {self.name}>'
#db.create_all()
class TodoList(db.Model):
__tablename__="todolists"
id=db.Column(db.Integer,primary_key=True)
name=db.Column(db.String(),nullable=False)
student=db.relationship('student',backref='list',lazy=True)
@app.route('/create', methods=['POST'])
def create_todo():
na=request.form['disc']
todo=student(name=na,completed=False)
db.session.add(todo)
db.session.commit()
return redirect(url_for('index'))
@app.route('/create/<id>/set-completed', methods=['POST'])
def set_complete_student(id):
try:
check=request.get_json()['completed']
print('completed', check)
st=student.query.get(id)
st.completed=check
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
return redirect(url_for('index'))
@app.route('/create/<id>',methods=['DELETE'])
def del_student(id):
try:
st=student.query.get(id)
db.session.delete(st)
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
#return redirect(url_for('index'))
return jsonify({
'succuss':True
})
@app.route('/lists/<id>')
def get_list_id(id):
names=student.query.filter_by(id=id).order_by('id').all()
return render_template('index.html',names=names)
@app.route('/')
def index():
return redirect(url_for('get_list_id',id=1))
if __name__==('__main__'):
app.run(debug=True)
# @app.route('/login', methods=['POST'])
# # def app_login():
# if request.method=='POST':
# user=request.form.get('username')
# password=request.form.get('password')
# return render_template('login')
#