get / post by id works

This commit is contained in:
Askill 2020-03-16 13:31:18 +01:00
parent ed67f78a55
commit 0526bb9d04
4 changed files with 41 additions and 15 deletions

View File

@ -33,6 +33,11 @@ class Person(Base):
for fingerprint in self.fingerprints: for fingerprint in self.fingerprints:
prints.append(fingerprint.serialize()) prints.append(fingerprint.serialize())
if self.face is not None:
face = self.face.decode('utf-8')
else:
face = None
data = { data = {
"person_id": self.person_id, "person_id": self.person_id,
"timestamp": self.timestamp, "timestamp": self.timestamp,
@ -40,7 +45,7 @@ class Person(Base):
"lname": self.lname, "lname": self.lname,
"yob": self.yob, "yob": self.yob,
"gender": self.gender, "gender": self.gender,
"face": self.face, "face": face,
"fingerprints": prints "fingerprints": prints
} }
return data return data
@ -54,11 +59,15 @@ class Fingerprint(Base):
fingerprint = Column('fingerprint', LargeBinary) fingerprint = Column('fingerprint', LargeBinary)
def serialize(self): def serialize(self):
if self.fingerprint is not None:
fp = self.fingerprint.decode('utf-8')
else:
fp = None
data = { data = {
"person_id": self.person_id, "person_id": self.person_id,
"fingerprint_id": self.fingerprint_id, "fingerprint_id": self.fingerprint_id,
"timestamp": self.timestamp, "timestamp": self.timestamp,
"fingerprint": self.fingerprint "fingerprint": fp
} }
return data return data

View File

@ -5,12 +5,35 @@ import application.config as config
import json import json
from application.db import Session, Person, Fingerprint from application.db import Session, Person, Fingerprint
class PersonList(Resource): class PersonList(Resource):
def post(self, id = None): def post(self, id = None):
""" """ """ """
try: try:
data = "" jsonData = flask.request.get_json(force=True)
return flask.make_response(flask.jsonify({'data': data}), 201) personJSON = jsonData["person"]
session = Session()
fingerprintsObj = []
for fingerprint in personJSON["fingerprints"]:
fingerprint["fingerprint"] = fingerprint["fingerprint"].encode('utf-8')
fp = Fingerprint(**fingerprint) # ** Operator converts DICT/JSON to initializable
fingerprintsObj.append(fp)
session.add(fp)
personJSON["fingerprints"] = fingerprintsObj
personJSON["face"] = personJSON["face"].encode('utf-8')
person = Person(**personJSON)
session.add(person)
session.commit()
data = list(session.query(Person).filter_by(person_id=person.person_id))
arr = []
for x in data:
arr.append(x.serialize())
return flask.make_response(flask.jsonify({'data': arr}), 201)
except Exception as e: except Exception as e:
print("error: -", e) print("error: -", e)
return flask.make_response(flask.jsonify({'error': str(e)}), 400) return flask.make_response(flask.jsonify({'error': str(e)}), 400)
@ -19,18 +42,14 @@ class PersonList(Resource):
""" """ """ """
try: try:
session = Session() session = Session()
fingerprint = Fingerprint(fingerprint_id=1) if id is None:
person = Person(fname="hi", fingerprints=[fingerprint])
session.add(fingerprint)
session.add(person)
session.commit()
data = list(session.query(Person).all()) data = list(session.query(Person).all())
else:
data = list(session.query(Person).filter_by(person_id=id))
arr = [] arr = []
for x in data: for x in data:
arr.append(x.serialize()) arr.append(x.serialize())
print(arr)
return flask.make_response(flask.jsonify({'data': arr}), 200) return flask.make_response(flask.jsonify({'data': arr}), 200)
except Exception as e: except Exception as e:
print("error: -", e) print("error: -", e)

2
run.py
View File

@ -3,5 +3,3 @@ from application import app
app.run(host="localhost",port='10024', debug=True) app.run(host="localhost",port='10024', debug=True)

Binary file not shown.