def update_user(user_id, user): """ Update an existing user in the user structure :param user_id: Id of the user to update in the user structure :param user: user data to update :return: 200 if success 404 if not found """ user = User.query.filter(User.user_id == user_id).one_or_none() if not user: response = code_404('Not found user with id={user_id}.') else: schema = UserSchema() updating_user = schema.load(user, session=db.session).data # Set the id of the user and update date to update updating_user.user_id = user.user_id updating_user.updated_on = datetime.utcnow() # merge the new object into old merge(updating_user) response = code_200('Success to update user with id={user_id}.') return response
def delete_face(image_id): face = Face.query.get(image_id=image_id) if not face: response = code_404('Not found face with id=\'{}\''.format(image_id)) else: delete(face) response = code_200('Success to delete all faces with id=\'{}\''.format(image_id)) return response
def delete_faces(staff_id): faces = Face.query.get(staff_id) if not faces: response = code_404('Not found faces with staff_id=\'{}\''.format(staff_id)) else: delete(faces) # reset_nfaces(staff_id) response = code_200('Success to delete all faces with staff_id=\'{}\''.format(staff_id)) return response
def add_face(data): employee = Employee.query.filter_by(id=data['staff_id']).first() if not employee: response = code_404('Not found employee with staff_id=\'{}\' in database'.format(data['staff_id'])) else: new_face = Face(staff_id=data['staff_id'], name=data['name']) # insert face insert(new_face) # update num images # increase_nfaces(data['staff_id']) response = code_201('Success to add new face with staff_id=\'{}\''.format(data['staff_id'])) return response
def update_face(data): face = Face.query.filter_by(image_id=data['image_id']).first() if not face: response = code_404('Not found face with id=\'{}\''.format(data['image_id'])) else: for k in data.keys(): if k == 'id': continue setattr(face, k, data[k]) face.updated_on = datetime.utcnow() commit() response = code_200('Success to update face with id=\'{}\''.format(data['image_id'])) return response
def delete_user(user_id): """ Delete a user from the user structure :param user_id: Id of the user to delete :return: 200 if success 404 if not found """ user = User.query.filter(User.user_id == user_id).one_or_none() if not user: response = code_404('Not found user with id={user_id}.') else: delete(user) response = code_200('Success to delete user with id={}.') return response
def get_user(user_id): """ Respond to a request for `/api/user/{user_id}` with one matching user from user id :param user_id: Id of user to find :return: user data matching id if success 404 if not found """ user = User.query.filter( User.user_id == user_id).outerjoin(Face).one_or_none() if not user: return code_404('Not found user with id={user_id}.') else: # Serialize the data for response schema = UserSchema() data = schema.dump(user).data return data