2020-03-15 19:02:37 +00:00
|
|
|
from flask_restful import Resource, reqparse
|
|
|
|
|
import flask
|
|
|
|
|
import requests
|
|
|
|
|
import application.config as config
|
|
|
|
|
import json
|
2020-03-15 22:15:40 +00:00
|
|
|
from application.db import Session, Person, Fingerprint
|
2020-03-15 19:02:37 +00:00
|
|
|
|
2020-03-16 12:31:18 +00:00
|
|
|
|
2020-03-15 19:02:37 +00:00
|
|
|
class PersonList(Resource):
|
2020-03-15 23:31:07 +00:00
|
|
|
def post(self, id = None):
|
2020-03-15 19:02:37 +00:00
|
|
|
""" """
|
|
|
|
|
try:
|
2020-03-16 12:31:18 +00:00
|
|
|
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)
|
|
|
|
|
|
2020-03-15 19:02:37 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
print("error: -", e)
|
|
|
|
|
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
|
|
|
|
|
|
2020-03-15 23:31:07 +00:00
|
|
|
def get(self, id = None):
|
2020-03-15 19:02:37 +00:00
|
|
|
""" """
|
|
|
|
|
try:
|
|
|
|
|
session = Session()
|
2020-03-16 12:31:18 +00:00
|
|
|
if id is None:
|
|
|
|
|
data = list(session.query(Person).all())
|
|
|
|
|
else:
|
|
|
|
|
data = list(session.query(Person).filter_by(person_id=id))
|
2020-03-15 19:02:37 +00:00
|
|
|
arr = []
|
|
|
|
|
for x in data:
|
2020-03-15 22:15:40 +00:00
|
|
|
arr.append(x.serialize())
|
2020-03-15 19:02:37 +00:00
|
|
|
|
|
|
|
|
return flask.make_response(flask.jsonify({'data': arr}), 200)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("error: -", e)
|
|
|
|
|
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
|
|
|
|
|
|
2020-03-15 23:31:07 +00:00
|
|
|
def put(self, id = None):
|
2020-03-15 19:02:37 +00:00
|
|
|
""" """
|
|
|
|
|
try:
|
|
|
|
|
data = ""
|
|
|
|
|
return flask.make_response(flask.jsonify({'data': data}), 200)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("error: -", e)
|
|
|
|
|
return flask.make_response(flask.jsonify({'error': str(e)}), 400)
|
|
|
|
|
|
2020-03-15 23:31:07 +00:00
|
|
|
def delete(self, id = None):
|
2020-03-15 19:02:37 +00:00
|
|
|
""" """
|
|
|
|
|
try:
|
2020-03-16 12:50:13 +00:00
|
|
|
if id is None:
|
|
|
|
|
return flask.make_response(flask.jsonify({'error': "No ID given"}), 404)
|
|
|
|
|
|
|
|
|
|
session = Session()
|
|
|
|
|
data = session.query(Person).filter_by(person_id=id).delete()
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2020-03-15 19:02:37 +00:00
|
|
|
return flask.make_response(flask.jsonify({'data': data}), 204)
|
2020-03-16 12:50:13 +00:00
|
|
|
|
2020-03-15 19:02:37 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
print("error: -", e)
|
2020-03-16 12:50:13 +00:00
|
|
|
return flask.make_response(flask.jsonify({'error': str(e)}), 404)
|
2020-03-15 19:02:37 +00:00
|
|
|
|