-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.py
76 lines (55 loc) · 1.92 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
from flask import Flask, render_template, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from json import JSONEncoder
import json
import os
app = Flask(__name__, instance_relative_config=False)
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL")
app.config["SQLALCHEMY_ECHO"] = False
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# db variable initialization
db = SQLAlchemy(app)
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60))
surname = db.Column(db.String(60))
def __repr__(self):
return '<Person: {}>'.format(self.name)
db.init_app(app)
db.create_all() # Create sql tables for our data models
class PersonEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
@app.route("/")
def main():
return render_template('index.html')
@app.route('/signUp',methods=['POST'])
def signUp():
# read the posted values from the UI
name = request.form['name']
surname = request.form['surname']
person = Person(name = name, surname = surname )
db.session.add(person)
db.session.flush()
pid = person.id
print(pid)
db.session.commit()
return jsonify({'pid':pid})
# # validate the received values
# if name and surname:
# return json.dumps({'html':'<span>All fields good !!</span>'})
# else:
# return json.dumps({'html':'<span>Enter the required fields</span>'})
@app.route('/view',methods=['POST'])
def view():
pid = request.json['pid']
print("The view pid: ", pid)
p1 = db.session.query(Person).get(pid)
print("The p1 person: ", p1)
print("The p1 person surname: ", p1.surname)
#person = Person.query.filter_by(name="Future")
#print("The view person: ", person)
#personJSONData = json.dumps(p1, indent=4, cls=PersonEncoder)
return jsonify({'name': p1.name, 'surname': p1.surname})
if __name__ == "__main__":
app.run()