def add(db_session, data, username): logger.info(LogMsg.START) schema_validate(data, USER_ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) new_username = data.get('username') # adder = check_user(username,db_session) user = check_by_username(new_username, db_session) if user: logger.error(LogMsg.USER_XISTS, new_username) raise Http_error(409, Message.USERNAME_EXISTS) logger.debug(LogMsg.USR_ADDING) model_instance = User() model_instance.username = new_username model_instance.password = data.get('password') populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) person_id = data.get('person_id') if person_id: person_is_valid = validate_person(person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS, {'person_id': person_id}) if person_is_valid: if person_is_valid.is_legal: person_user = get_by_person(person_id, db_session) if person_user is not None: logger.error(LogMsg.LEGAL_PERSON_USER_RESTRICTION) raise Http_error(409, Message.LEGAL_PERSON_USER_RESTRICTION) model_instance.person_id = person_id else: logger.error(LogMsg.INVALID_USER, {'person_id': person_id}) raise Http_error(404, Message.INVALID_USER) db_session.add(model_instance) logger.debug(LogMsg.DB_ADD, model_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance
def forget_pass(data, db_session): logger.info(LogMsg.START, data) schema_validate(data, FORGET_PASS_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) reset_password_interval = value('reset_password_interval', 120) username = data.get('username') cell_no = data.get('cell_no') user = None if username: user = check_by_username(username, db_session) elif cell_no: user = check_by_cell_no(cell_no, db_session) else: logger.error(LogMsg.INVALID_USER, data) raise Http_error(400, Message.USERNAME_CELLNO_REQUIRED) if user: person = validate_person(user.person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS, username) password = str(random.randint(1000, 9999)) sending_data = { 'receptor': person.cell_no, 'token': user.username, 'token2': password, 'type': 'sms', 'template': 'fajrresetPassword' } send_message(sending_data) logger.debug(LogMsg.SMS_SENT, sending_data) redis_key = 'PASS_{}'.format(person.cell_no) redis.set(redis_key, password, ex=reset_password_interval) logger.debug(LogMsg.REDIS_SET, redis_key) logger.info(LogMsg.END) return data logger.error(LogMsg.INVALID_USER, data) raise Http_error(404, Message.INVALID_USER)
def add(db_session, data, username): logger.info(LogMsg.START) cell_no = data.get('cell_no') name = data.get('name') new_username = data.get('username') user = check_by_username(new_username, db_session) if user: logger.error(LogMsg.USER_XISTS, new_username) raise Http_error(409, Message.USERNAME_EXISTS) logger.debug(LogMsg.USR_ADDING) model_instance = User() model_instance.username = new_username model_instance.password = data.get('password') model_instance.name = name populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) person_id = data.get('person_id') if person_id: person_is_valid = validate_person(person_id, db_session) logger.debug(LogMsg.PERSON_EXISTS, {'person_id': person_id}) if person_is_valid: model_instance.person_id = person_id else: logger.error(LogMsg.INVALID_USER, {'person_id': person_id}) raise Http_error(404, Message.INVALID_USER) db_session.add(model_instance) logger.debug(LogMsg.DB_ADD, model_to_dict(model_instance)) logger.info(LogMsg.END) return model_instance