-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
118 lines (95 loc) · 2.88 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
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
import json
# Init app
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
'''
@app.route('/',methods=['GET'])
def get():
return jsonify({'msg':'hello world'})
'''
#Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir,'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
#Init db
db = SQLAlchemy(app)
#Init ma
ma = Marshmallow(app)
# Student Class/Model
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), unique=True)
age = db.Column(db.Integer)
address = db.Column(db.String(50))
phone = db.Column(db.Integer)
gender = db.Column(db.String(6))
def __init__(self, name, age, address, phone, gender):
self.name = name
self.age = age
self.address = address
self.phone = phone
self.gender = gender
# Product Schema
class StudentSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'age', 'address', 'phone', 'gender')
# Init schema
student_schema = StudentSchema(strict=True)
students_schema = StudentSchema(many=True, strict=True)
# Create a Student
@app.route('/addstudent', methods=['POST'])
def add_student():
name = request.form['name']
age = request.form['age']
address = request.form['address']
phone = request.form['phone']
gender = request.form['gender']
new_student = Student(name, age, address, phone, gender)
db.session.add(new_student)
db.session.commit()
return student_schema.jsonify(new_student)
# Display all students
@app.route('/getstudents')
def display():
return render_template('display.html')
# Get All Students
@app.route('/student', methods=['GET'])
def get_students():
all_students = Student.query.all()
result = students_schema.dump(all_students)
with open('file.json', 'w') as f:
json.dump(result.data, f)
return jsonify(result.data)
# Update a Student
@app.route('/student/<id>', methods=['PUT'])
def update_student(id):
student = Student.query.get(id)
name = request.json['name']
age = request.json['age']
address = request.json['address']
phone = request.json['phone']
gender = request.json['gender']
student.name = name
student.age = age
student.address = address
student.phone = phone
student.gender = gender
db.session.commit()
return student_schema.jsonify(student)
# Delete Student
@app.route('/student/<id>', methods=['DELETE'])
def delete_student(id):
student = Student.query.get(id)
db.session.delete(student)
db.session.commit()
return student_schema.jsonify(student)
# Start app
@app.route("/")
def main():
return render_template('front.html')
#Run Server
if __name__ == '__main__':
app.run(debug=True)