def add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, PERSON_ADD_SCHEMA_PATH) cell_no = data.get('cell_no') if cell_no and person_cell_exists(db_session, cell_no): logger.error(LogMsg.PERSON_EXISTS, {'cell_no': cell_no}) raise Http_error(409, Message.ALREADY_EXISTS) email = data.get('email') if email and person_mail_exists(db_session, email): logger.error(LogMsg.PERSON_EXISTS, {'email': email}) raise Http_error(409, Message.ALREADY_EXISTS) logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data) model_instance = Person() populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.name = data.get('name') model_instance.last_name = data.get('last_name') model_instance.full_name = full_name(model_instance.name, model_instance.last_name) model_instance.address = data.get('address') model_instance.phone = data.get('phone') model_instance.email = data.get('email') model_instance.cell_no = data.get('cell_no') model_instance.bio = data.get('bio') model_instance.image = data.get('image') model_instance.is_legal = data.get('is_legal', False) db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def add(db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data,PERSON_ADD_SCHEMA_PATH) if username is not None and username != SIGNUP_USER: permissions, presses = get_user_permissions(username, db_session) has_permission([Permissions.PERSON_ADD_PREMIUM], permissions) logger.debug(LogMsg.PERMISSION_VERIFIED) cell_no = data.get('cell_no') if cell_no and person_cell_exists(db_session, cell_no): logger.error(LogMsg.PERSON_EXISTS, {'cell_no': cell_no}) raise Http_error(409, Message.ALREADY_EXISTS) email = data.get('email') if email and person_mail_exists(db_session, email): logger.error(LogMsg.PERSON_EXISTS, {'email': email}) raise Http_error(409, Message.ALREADY_EXISTS) logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data) model_instance = Person() populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.name = data.get('name') model_instance.last_name = data.get('last_name') model_instance.full_name = full_name(model_instance.name,model_instance.last_name) model_instance.address = data.get('address') model_instance.phone = data.get('phone') model_instance.email = data.get('email') model_instance.cell_no = data.get('cell_no') model_instance.bio = data.get('bio') model_instance.image = data.get('image') model_instance.is_legal = data.get('is_legal', False) unique_code = add_uniquecode(model_instance, db_session) db_session.add(model_instance) db_session.flush() logger.debug(LogMsg.DB_ADD) add_initial_account(model_instance.id, db_session, username) logger.debug(LogMsg.PERSON_ADD_ACCOUNT, {'person_id': model_instance.id}) add_connector(model_instance.id, unique_code.UniqueCode, db_session) logger.debug(LogMsg.UNIQUE_CONNECTOR_ADDED, {'person_id': model_instance.id, 'unique_constraint': unique_code.UniqueCode}) logger.info(LogMsg.END) return model_instance
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data, PERSON_EDIT_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) logger.debug(LogMsg.EDIT_REQUST, {'person_id': id, 'data': data}) user = check_user(username, db_session) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON, username) raise Http_error(404, Message.INVALID_USER) model_instance = db_session.query(Person).filter(Person.id == id).first() if model_instance: logger.debug(LogMsg.MODEL_GETTING) else: logger.debug(LogMsg.MODEL_GETTING_FAILED, {'person_id': id}) raise Http_error(404, Message.NOT_FOUND) if 'cell_no' in data.keys(): cell_person = person_cell_exists(db_session, data.get('cell_no')) if cell_person is not None: if cell_person.id != model_instance.id: logger.error(LogMsg.ANOTHER_PERSON_BY_CELL) raise Http_error(403, Message.CELL_EXISTS) try: for key, value in data.items(): setattr(model_instance, key, value) model_instance.full_name = full_name(model_instance.name, model_instance.last_name) edit_basic_data(model_instance, username, data.get('tags')) db_session.flush() logger.debug(LogMsg.MODEL_ALTERED, person_to_dict(model_instance, db_session)) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(500, Message.DELETE_FAILED) logger.info(LogMsg.END) return model_instance
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) schema_validate(data,PERSON_EDIT_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) logger.debug(LogMsg.EDIT_REQUST, {'person_id': id, 'data': data}) user = check_user(username, db_session) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON, username) raise Http_error(404, Message.INVALID_USER) model_instance = db_session.query(Person).filter(Person.id == id).first() if model_instance: logger.debug(LogMsg.MODEL_GETTING) else: logger.debug(LogMsg.MODEL_GETTING_FAILED, {'person_id': id}) raise Http_error(404, Message.NOT_FOUND) per_data = {} permissions, presses = get_user_permissions(username, db_session) if user.person_id == id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.PERSON_EDIT_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) if 'current_book_id' in data.keys(): if not is_book_in_library(model_instance.id, data.get('current_book_id'), db_session): logger.error(LogMsg.COLLECTION_BOOK_IS_NOT_IN_LIBRARY, {'current_book_id': data.get('current_book_id')}) raise Http_error(404, Message.BOOK_NOT_IN_LIB) if 'cell_no' in data.keys(): cell_person = person_cell_exists(db_session, data.get('cell_no')) if cell_person is not None: if cell_person.id != model_instance.id: logger.error(LogMsg.ANOTHER_PERSON_BY_CELL) raise Http_error(403, Message.CELL_EXISTS) try: for key, value in data.items(): # TODO if key is valid attribute of class setattr(model_instance, key, value) model_instance.full_name = full_name(model_instance.name,model_instance.last_name) edit_basic_data(model_instance, username, data.get('tags')) db_session.flush() logger.debug(LogMsg.MODEL_ALTERED, person_to_dict(model_instance, db_session)) logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING) unique_connector = get_connector(id, db_session) if unique_connector: if persons_code(model_instance) != unique_connector.UniqueCode: logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT) delete_uniquecode(unique_connector.UniqueCode, db_session) db_session.flush() logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data) code = add_uniquecode(model_instance, db_session) delete_connector(id, db_session) add_connector(id, code.UniqueCode, db_session) except: logger.exception(LogMsg.EDIT_FAILED, exc_info=True) raise Http_error(500, Message.DELETE_FAILED) logger.info(LogMsg.END) return model_instance
def edit(id, db_session, data, username): logger.info(LogMsg.START, username) # TODO: you never checked version of passed data, we have version field in our # records, to prevent conflict when we received two different edit request # concurrently. check KAVEH codes (edit functions) to better understanding # version field usage logger.debug(LogMsg.EDIT_REQUST, {'person_id': id, 'data': data}) if "id" in data.keys(): del data["id"] user = check_user(username,db_session) if user.person_id is None: logger.error(LogMsg.USER_HAS_NO_PERSON,username) raise Http_error(404,Message.INVALID_USER) model_instance = db_session.query(Person).filter(Person.id == id).first() if model_instance: logger.debug(LogMsg.MODEL_GETTING) else: logger.debug(LogMsg.MODEL_GETTING_FAILED, {'person_id': id}) raise Http_error(404, Message.NOT_FOUND) per_data = {} permissions, presses = get_user_permissions(username, db_session) if user.person_id == id: per_data.update({Permissions.IS_OWNER.value: True}) has_permission([Permissions.PERSON_EDIT_PREMIUM], permissions, None, per_data) logger.debug(LogMsg.PERMISSION_VERIFIED) if 'current_book' in data.keys(): if not is_book_in_library(model_instance.id, data.get('current_book'), db_session): logger.error(LogMsg.COLLECTION_BOOK_IS_NOT_IN_LIBRARY, {'current_book_id': data.get('current_book')}) raise Http_error(404, Message.BOOK_NOT_IN_LIB) if 'cell_no' in data.keys(): cell_person = person_cell_exists(db_session,data.get('cell_no')) if cell_person is not None: if cell_person.id !=model_instance.id: logger.error(LogMsg.ANOTHER_PERSON_BY_CELL) raise Http_error(403,Message.CELL_EXISTS) try: for key, value in data.items(): # TODO if key is valid attribute of class setattr(model_instance, key, value) model_instance.full_name ='{} {}'.format(model_instance.last_name or '',model_instance.name or '') edit_basic_data(model_instance, username, data.get('tags')) db_session.flush() logger.debug(LogMsg.MODEL_ALTERED, person_to_dict(model_instance, db_session)) logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING) unique_connector = get_connector(id, db_session) if unique_connector: logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT) delete_uniquecode(unique_connector.UniqueCode, db_session) logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data) db_session.flush() code = add_uniquecode(data, db_session) delete_connector(id, db_session) add_connector(id, code.UniqueCode, db_session) except: logger.exception(LogMsg.EDIT_FAILED,exc_info=True) raise Http_error(403,Message.DELETE_FAILED) logger.info(LogMsg.END) return model_instance