def post(self): try: user_identity = get_jwt_identity() users = User.get_user_by_id(user_identity['id']) data_parser = parser.parse_args() if not users: return bad_request('User not found!', 422) if data_parser['file'] == "": return bad_request('File not found!', 422) photo = data_parser['file'] if photo and allowed_file(photo.filename): user_photo = os.path.join(APP_STATIC, users.image) known_face = face_recognition.load_image_file(user_photo) known_face_encoding = face_recognition.face_encodings( known_face)[0] unknown_face = face_recognition.load_image_file(photo) factor = 0 if unknown_face.shape[0] > 1028 or unknown_face.shape[1] > 1028: if unknown_face.shape[0] > unknown_face.shape[1]: factor = 1028 / unknown_face.shape[0] else: factor = 1028 / unknown_face.shape[1] resized_image = Image.fromarray(unknown_face).resize( (int(unknown_face.shape[1] * factor), int(unknown_face.shape[0] * factor))) unknown_face = numpy.array(resized_image) unknown_face_encoding = face_recognition.face_encodings( unknown_face)[0] distance = face_recognition.face_distance( [known_face_encoding], unknown_face_encoding) result = face_recognition.compare_faces([known_face_encoding], unknown_face_encoding) if result[0]: return ok( { 'result': 'Verified', 'user': users.name, 'tingkat kemiripan': str( int( round((face_distance_to_conf(distance)[0]), 2) * 100)) + '%' }, 200) else: return ok( { 'result': 'Not verified', 'tingkat kemiripan': str( int( round((face_distance_to_conf(distance)[0]), 2) * 100)) + '%' }, 200) except Exception as e: return bad_request(str(e), 400)
def post(self): try: json_data = request.get_json(force=True) if not json_data: return bad_request('No input data provided', 400) if 'email' not in request.json: return bad_request('email not provided', 422) if 'password' not in request.json: return bad_request('password not provided', 422) if json_data['email'] == '' or json_data['email'] == None: return bad_request('email can\'t be empty or null', 422) if json_data['password'] == '' or json_data['password'] == None: return bad_request('password can\'t be empty or null', 422) user_in_db = User.get_user_by_email(json_data['email']) if user_in_db: authorized = user_in_db.check_password(json_data['password']) if authorized: users = UserSchema(only=('id', 'name', 'roles')).dump(user_in_db) expires = timedelta(days=1) token = create_access_token(identity=users, expires_delta=expires) return ok( { 'id': users['id'], 'name': users['name'], 'token': token }, 200) else: return bad_request('wrong password', 422) else: return bad_request('user not found!', 404) except Exception as e: return bad_request(str(e), 500)
def get(self): try: current_user = get_jwt_identity() tes = get_jwt_claims() return ok(current_user, 200) except Exception: return bad_request('Something went wrong', 500)
def patch(self, user_id): try: data_parser = parser.parse_args() users = User.get_user_by_id(user_id) if not users: return bad_request('User not found!', 422) if data_parser['file'] == "": return bad_request('File not found!', 422) photo = data_parser['file'] if photo and allowed_file(photo.filename): secure_name = secure_filename(photo.filename) secure_name = get_file_extension(secure_name) secure_name = '{}{:-%Y%m%d%H%M%S}.{}'.format( str(uuid4().hex), datetime.now(), secure_name) photo.save(os.path.join(APP_STATIC, secure_name)) if users.image: os.remove(os.path.join(APP_STATIC, users.image)) # photo_url = request.url_root + url_for( # 'static', filename="image/" + secure_name) users.image = secure_name users.updated_at = datetime.now() db_helper.only_save() users = UserSchema(exclude=['password']).dump(users) return ok(users, 200) else: return bad_request('File not allowed!', 422) except Exception as e: return bad_request('Something went wrong', 500)
def delete(self, user_id): try: user_in_db = User.get_user_by_id(user_id) if not user_in_db: return bad_request('User not found!', 422) if user_in_db.image: os.remove(os.path.join(APP_STATIC, user_in_db.image)) db_helper.delete(user_in_db) return ok('user deleted', 200) except Exception as e: return bad_request('Something went wrong', 500)
def put(self, user_id): try: req_data = request.get_json(force=True) user_in_db = User.get_user_by_id(user_id) if not user_in_db: return bad_request('User not found!', 422) data = UserSchema().load(req_data, partial=True) db_helper.update(user_in_db, data) data = UserSchema(exclude=['password']).dump(user_in_db) return ok(data, 200) except ValidationError as e: return bad_request(e.messages, 422) except Exception as e: print(e) return bad_request('Something went wrong', 500)
def post(self): json_data = request.get_json(force=True) if not json_data: return bad_request('No input data provided', 400) try: user_in_db = User.get_user_by_id(json_data['user_id']) role_in_db = Role.get_role_by_id(json_data['role_id']) if not user_in_db: return bad_request('User not found', 422) if not role_in_db: return bad_request('Role not found', 422) role_in_db.users.append(user_in_db) db_helper.only_save() return ok('ok', 200) except Exception as e: return bad_request(str(e), 500)
def post(self): json_data = request.get_json(force=True) if not json_data: return bad_request('No input data provided', 400) try: data = UserSchema().load(json_data) user_in_db = User.get_user_by_email(data.get('email')) if user_in_db: return bad_request('User already exist, please supply another email address', 422) users = User(data) db_helper.insert(users) ser_data = UserSchema(exclude=['password']).dump(users) return ok(ser_data, 201) except ValidationError as e: return bad_request(e.messages, 422) except Exception: return bad_request('Something went wrong', 500)
def post(self): json_data = request.get_json(force=True) if not json_data: return bad_request('No input data provided', 400) try: data = RoleSchema().load(json_data) role_in_db = Role.get_role_by_name(data.get('name')) if role_in_db: return bad_request( 'Role already exist, please supply another role name', 422) roles = Role(data) db_helper.insert(roles) role_data = RoleSchema().dump(roles) return ok(role_data, 200) except ValidationError as e: return bad_request(e.messages, 422) except Exception: return bad_request('Something went wrong', 500)
def get(self): roles = Role.query.all() roles = RoleSchema(many=True).dump(roles) return ok(roles, 200)
def post(self): try: data_parser = parser.parse_args() if data_parser['file'] == "": return bad_request('File not found!', 422) photo = data_parser['file'] if photo and allowed_file(photo.filename): unknown_face = face_recognition.load_image_file(photo) factor = 0 if unknown_face.shape[0] > 1028 or unknown_face.shape[1] > 1028: if unknown_face.shape[0] > unknown_face.shape[1]: factor = 1028 / unknown_face.shape[0] else: factor = 1028 / unknown_face.shape[1] resized_image = Image.fromarray(unknown_face).resize( (int(unknown_face.shape[1] * factor), int(unknown_face.shape[0] * factor))) unknown_face = numpy.array(resized_image) unknown_face_encoding = face_recognition.face_encodings( unknown_face)[0] for filename in os.listdir(APP_STATIC): user_photo = os.path.join(APP_STATIC, filename) known_face = face_recognition.load_image_file(user_photo) if known_face.shape[0] > 1028 or known_face.shape[1] > 1028: if known_face.shape[0] > known_face.shape[1]: factor = 1028 / known_face.shape[0] else: factor = 1028 / known_face.shape[1] resized_image = Image.fromarray(known_face).resize( (int(known_face.shape[1] * factor), int(known_face.shape[0] * factor))) known_face = numpy.array(resized_image) known_face_encoding = face_recognition.face_encodings( known_face)[0] result = face_recognition.compare_faces( [known_face_encoding], unknown_face_encoding) if result[0]: users = User.get_user_by_image(filename) photo_url = request.url_root + url_for( 'static', filename="image/" + users.image) distance = face_recognition.face_distance( [known_face_encoding], unknown_face_encoding) return ok( { 'result': 'user ditemukan', 'user': users.name, 'image': photo_url, 'tingkat kemiripan': str( int( round( (face_distance_to_conf(distance)[0] ), 2) * 100)) + '%' }, 200) break return ok({'result': 'user tidak ditemukan'}, 200) except Exception as e: return bad_request(str(e), 400)
def get(self): users_schema = UserSchema(many=True, exclude=['password']) users = User.query.all() users = users_schema.dump(users) return ok(users, 200)