def signup(data, db_session, *args, **kwargs): logger.info(LogMsg.START, data) schema_validate(data, SIGN_UP_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) cell_no = data.get('cell_no') logger.debug(LogMsg.SIGNUP_GETTING_TOKEN_FROM_REDIS, cell_no) signup_token = redis.get(cell_no) if signup_token is None: logger.error(LogMsg.SIGNUP_TOKEN_NOT_IN_REDIS, cell_no) raise Http_error(404, Message.SIGNUP_TOKEN_NOT_EXISTS) signup_token = json.loads(signup_token.decode("utf-8")).get( 'signup_token', None) if (signup_token is None) or (signup_token != data.get('signup_token')): logger.error( LogMsg.SIGNUP_TOKEN_INVALID, { 'redis_signup_token': signup_token, 'data_token': data.get('signup_token') }) raise Http_error(404, Message.INVALID_SIGNUP_TOKEN) user_data = { k: v for k, v in data.items() if k in ['username', 'password'] } person_data = {k: v for k, v in data.items() if k not in user_data.keys()} del person_data['signup_token'] logger.debug(LogMsg.PERSON_GENERATING, person_data) person = add_person(db_session, person_data, SIGNUP_USER) if user_data: user_data.update({'person_id': person.id}) logger.debug(LogMsg.USER_GENERATING, user_data) user = add_user(db_session, user_data, SIGNUP_USER) full_name = '{} {}'.format(data.get('last_name', ''), data.get('name', '')) user_welcoming_data = { 'receptor': cell_no, 'token': full_name, 'type': 'sms', 'template': 'fajrwelcoming' } send_message(user_welcoming_data) logger.debug(LogMsg.SMS_SENT, user_welcoming_data) result = {'user': user_to_dict(user), 'person': model_to_dict(person)} logger.debug(LogMsg.SIGNUP_SUCCESS, result) logger.info(LogMsg.END) return result
def group_msg(self, rev): if "[CQ:at,qq={}]".format(self_qq) in rev["raw_message"]: try: rev['raw_message'] = rev['raw_message'].split(" ")[1] except: pass return send_message(talk_to_user(rev, self.talk_data), rev["group_id"], "group") if randint(1, 10) < 4 or rev['raw_message'] in ban_words: return send_message(talk_to_gourp(rev, self.talk_data), rev["group_id"], "group") return True
def register(data, db_session): valid_registering_intervall = value('valid_registering_intervall', 120) logging.info(Msg.START + ' data is: ' + str(data)) cell_no = data.get('cell_no') if cell_no is None: logging.error(Msg.DATA_MISSING.format('cell_no')) raise Http_error(404, Msg.DATA_MISSING.format('cell_no')) logging.debug(Msg.CHECK_USER_EXISTANCE) if check_user(cell_no, db_session): logging.error(Msg.USER_XISTS) raise Http_error(409, Msg.USER_XISTS) logging.debug(Msg.CHECK_REDIS_FOR_EXISTANCE) if redis.get(cell_no): logging.error(Msg.REGISTER_XISTS) raise Http_error(403, Msg.REGISTER_XISTS) logging.debug(Msg.GENERATING_REGISTERY_CODE.format(cell_no)) password = random.randint(1000, 9999) message = 'BELLEZZA your activation code is : {}'.format(password) data = {'cell_no': cell_no, 'message': message} logging.debug(Msg.SEND_CODE_BY_SMS.format(cell_no)) sent_data = send_message(data) logging.debug(Msg.SMS_SENT.format(cell_no)) redis.set(cell_no, password, ex=valid_registering_intervall) return sent_data
def activation_account(data, db_session): try: SMSSchema().load(data) except ValidationError as err: return err.messages cache = RedisCache() cell_number = data.get('cell_number') if user_by_cell_number(cell_no=cell_number, db_session=db_session): raise HTTPError(409, body='USER_ALREADY_EXISTS') if cache.get_cache_multiple_value(cell_number, 'activation_code'): raise HTTPError(403, ' Message.ALREADY_HAS_VALID_KEY') activation_code = generate_otp_code() if send_message(cell_number=cell_number, activation_code=activation_code): cache.set_cache_multiple_value(key=cell_number, value=activation_code, custom_value_name='activation_code', ttl=valid_registering_interval) else: raise HTTPError(403, ' Message.Didnt send') result = {'msg': ' Message.MESSAGE_SENT' + cell_number} return result
def register(data, db_session): logger.info(LogMsg.START, data) schema_validate(data, REGISTER_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) cell_no = data.get('cell_no') logger.debug(LogMsg.CHECK_USER_EXISTANCE) if check_by_cell_no(cell_no, db_session): logger.error(LogMsg.USER_XISTS) raise Http_error(409, Message.USER_ALREADY_EXISTS) logger.debug(LogMsg.CHECK_REDIS_FOR_EXISTANCE) cell_data = redis.get(cell_no) if cell_data: logger.error(LogMsg.REGISTER_XISTS) activation_code = (json.loads(cell_data.decode('utf-8'))).get( 'activation_code', None) if activation_code: logger.error(LogMsg.USER_HAS_ACTIVATION_CODE) raise Http_error(403, { 'msg': Message.ALREADY_HAS_VALID_KEY, 'time': redis.ttl(cell_no) }) else: logger.error(LogMsg.USER_HAS_SIGNUP_TOKEN) redis.delete(cell_no) logger.debug(LogMsg.GENERATING_REGISTERY_CODE, cell_no) password = str(random.randint(1000, 9999)) data = { 'receptor': cell_no, 'token': password, 'type': 'sms', 'template': 'ratingregister' } logger.debug(LogMsg.SEND_CODE_BY_SMS.format(cell_no)) sent_data = send_message(data) redis.set(cell_no, json.dumps({'activation_code': password}), ex=valid_registering_intervall) result = { 'msg': Message.MESSAGE_SENT, 'cell_no': cell_no, 'time': redis.ttl(cell_no) } logger.debug(LogMsg.SMS_SENT, result) logger.info(LogMsg.END) return result
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 phone_activation(data, db_session): try: SMSSchema().load(data) except ValidationError as err: return err.messages cache = RedisCache() phone = data.get('phone') is_object_exist_409(User, db_session, User.phone == phone) if cache.get_cache_multiple_value(phone, 'activation_code'): raise HTTPError(403, ' Phone already has valid activation code') activation_code = generate_otp_code() if send_message(cell_number=phone, activation_code=activation_code): cache.set_cache_multiple_value(key=phone, value=activation_code, custom_value_name='activation_code', ttl=valid_registering_interval) else: raise HTTPError(403, ' Message.Didnt send') result = {'msg': ' Message.MESSAGE_SENT' + phone} return result
def private_msg(self, rev): if rev["sub_type"] != "friend": return send_message('你还不是我的好友呀', rev['user_id'], "private") return send_message(talk_to_user(rev, self.talk_data), rev["user_id"], "private")