-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
93 lines (77 loc) · 2.67 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
from flask import Flask, render_template, request, json, Response
from flask_sqlalchemy import SQLAlchemy
from marshmallow import fields, Schema
import math
app = Flask(__name__)
app.secret_key = 'super-secret-key'
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///fajax.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class User(db.Model):
'''set structure of database'''
sno = db.Column(db.Integer, primary_key=True,)
name = db.Column(db.String(80), nullable=False)
city = db.Column(db.String(80), nullable=False)
class UserSchema(Schema):
sno = fields.Int(dump_only=True)
name = fields.Str(required=True)
city = fields.Str(required=True)
user_schema = UserSchema()
@app.route("/")
def home():
return render_template('index.html')
@app.route("/loaddata", methods = ['POST'])
def lodadata():
data = request.get_json()
page_num = data['page_num']
users = User.query.filter_by().all()
total_pages = math.ceil(len(users)/5)
users_data = users[(int(page_num)-1)*int(5): (int(page_num)-1)*int(5)+ int(5)]
users_data = user_schema.dump(users_data, many=True)
data = {"users": users_data, "total_pages": total_pages, "page_num":page_num}
return custom_response(data, 200)
def custom_response(res, status_code):
return Response(mimetype="application/json",response=json.dumps(res),status=status_code)
@app.route("/insertdata", methods = ['POST'])
def insertdata():
data = request.get_json()
name = data['name']
city = data['city']
user = User(name=name, city=city)
db.session.add(user)
db.session.commit()
return "1"
@app.route("/updatedata", methods = ['POST'])
def updatedata():
data = request.get_json()
sno = data['id']
name = data['name']
city = data['city']
user = User.query.filter_by(sno=sno).first()
user.name = name
user.city = city
db.session.commit()
return "1"
@app.route("/deletedata", methods = ['POST'])
def deletedata():
data = request.get_json()
sno = data['id']
user = User.query.filter_by(sno=sno).first_or_404()
current_ssession = db.session.object_session(user)
current_ssession.delete(user)
current_ssession.commit()
return "1"
@app.route("/searchdata")
def searchdata():
search_query = request.args.get('q')
search = "%{0}%".format(search_query)
dataName = User.query.filter(User.name.like(search))
dataCity = User.query.filter(User.city.like(search))
data = dataName.union(dataCity).all()
if data == []:
return "0"
else:
r_data = user_schema.dump(data,many=True)
return custom_response(r_data, 200)
if __name__ == "__main__":
app.run(debug=True)