get / post by id works
This commit is contained in:
parent
ed67f78a55
commit
0526bb9d04
|
|
@ -32,7 +32,12 @@ class Person(Base):
|
|||
prints = []
|
||||
for fingerprint in self.fingerprints:
|
||||
prints.append(fingerprint.serialize())
|
||||
|
||||
|
||||
if self.face is not None:
|
||||
face = self.face.decode('utf-8')
|
||||
else:
|
||||
face = None
|
||||
|
||||
data = {
|
||||
"person_id": self.person_id,
|
||||
"timestamp": self.timestamp,
|
||||
|
|
@ -40,7 +45,7 @@ class Person(Base):
|
|||
"lname": self.lname,
|
||||
"yob": self.yob,
|
||||
"gender": self.gender,
|
||||
"face": self.face,
|
||||
"face": face,
|
||||
"fingerprints": prints
|
||||
}
|
||||
return data
|
||||
|
|
@ -54,11 +59,15 @@ class Fingerprint(Base):
|
|||
fingerprint = Column('fingerprint', LargeBinary)
|
||||
|
||||
def serialize(self):
|
||||
if self.fingerprint is not None:
|
||||
fp = self.fingerprint.decode('utf-8')
|
||||
else:
|
||||
fp = None
|
||||
data = {
|
||||
"person_id": self.person_id,
|
||||
"fingerprint_id": self.fingerprint_id,
|
||||
"timestamp": self.timestamp,
|
||||
"fingerprint": self.fingerprint
|
||||
"fingerprint": fp
|
||||
}
|
||||
return data
|
||||
|
||||
|
|
|
|||
|
|
@ -5,12 +5,35 @@ import application.config as config
|
|||
import json
|
||||
from application.db import Session, Person, Fingerprint
|
||||
|
||||
|
||||
class PersonList(Resource):
|
||||
def post(self, id = None):
|
||||
""" """
|
||||
try:
|
||||
data = ""
|
||||
return flask.make_response(flask.jsonify({'data': data}), 201)
|
||||
jsonData = flask.request.get_json(force=True)
|
||||
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:
|
||||
print("error: -", e)
|
||||
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
|
||||
|
|
@ -19,18 +42,14 @@ class PersonList(Resource):
|
|||
""" """
|
||||
try:
|
||||
session = Session()
|
||||
fingerprint = Fingerprint(fingerprint_id=1)
|
||||
person = Person(fname="hi", fingerprints=[fingerprint])
|
||||
session.add(fingerprint)
|
||||
session.add(person)
|
||||
session.commit()
|
||||
|
||||
data = list(session.query(Person).all())
|
||||
if id is None:
|
||||
data = list(session.query(Person).all())
|
||||
else:
|
||||
data = list(session.query(Person).filter_by(person_id=id))
|
||||
arr = []
|
||||
for x in data:
|
||||
arr.append(x.serialize())
|
||||
|
||||
print(arr)
|
||||
return flask.make_response(flask.jsonify({'data': arr}), 200)
|
||||
except Exception as e:
|
||||
print("error: -", e)
|
||||
|
|
|
|||
2
run.py
2
run.py
|
|
@ -3,5 +3,3 @@ from application import app
|
|||
app.run(host="localhost",port='10024', debug=True)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
BIN
test.sqlite
BIN
test.sqlite
Binary file not shown.
Loading…
Reference in New Issue