diff --git a/application/db.py b/application/db.py index da46c3c..7b12462 100644 --- a/application/db.py +++ b/application/db.py @@ -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 diff --git a/application/endpoints.py b/application/endpoints.py index 725e2b1..7b7d8ba 100644 --- a/application/endpoints.py +++ b/application/endpoints.py @@ -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) diff --git a/run.py b/run.py index f644a58..7224a78 100644 --- a/run.py +++ b/run.py @@ -3,5 +3,3 @@ from application import app app.run(host="localhost",port='10024', debug=True) - - diff --git a/test.sqlite b/test.sqlite index 12d8db9..0e5035d 100644 Binary files a/test.sqlite and b/test.sqlite differ