def _cancle_celery_task(conid): exist_task_id = conn.get(conid) if exist_task_id: exist_task_id = str(exist_task_id, encoding='utf-8') current_app.logger.info('已有任务id: {}'.format(exist_task_id)) celery.AsyncResult(exist_task_id).revoke() conn.delete(conid)
def get_usersid(self): # current_app.logger.info('start get user sids {}'.format(datetime.now())) usersids = conn.get('usersid') or {} # usersids 格式 {userid: sid} if usersids: usersids = ast.literal_eval(str(usersids, encoding='utf-8')) # current_app.logger.info('get usersids {}'.format(usersids)) # current_app.logger.info('end get user sids {}'.format(datetime.now())) return usersids
def on_setsession(self, token): from planet.common.request_handler import token_to_user_ current_app.logger.info(token) user = token_to_user_(token) current_app.logger.info(user) usersid = conn.get('usersid') current_app.logger.info('get sids', usersid) current_app.logger.info('get connect sid ', request.sid) # from flaskrun import sids current_app.logger.info( 'request ', request.headers, ) # join_room(request.sid) if user: sessiondict = {user.id: request.sid} # 创建session session.update({'id': user.id}) if not usersid: conn.set('usersid', sessiondict) else: usersid = ast.literal_eval(str(usersid, encoding='utf-8')) current_app.logger.info('pre append ', usersid) # sids.append(request.sid) usersid.update(sessiondict) current_app.logger.info('after ', usersid) conn.set('usersid', usersid) # res = json.loads(json.dumps(Success('{} is connect '.format(user.username)), cls=JSONEncoder)) # current_app.logger.info(res, type(res)) # res = json.loads(Success('{} is connect '.format(user.username)), cls=JSONEncoder) # current_app.logger.info(res, type(res)) # self.socketio.emit('server_response', Success('{} is connect '.format(user.username))) emit('server_response', Success('{} is connect '.format(user.username))) return return_res( Success('{} is connect '.format(user.username), data=user.id)) # conn.set('sid', session.sid) # else: # # session['id'] = 'random' # # session['username'] = '******' # # conn.set('') # return '{} is connect '.format('unknown') # self.socketio.emit('server_response', AuthorityError('token 失效')) emit('server_response', AuthorityError('token 失效')) return return_res(AuthorityError('token 失效'))
def update(self): form = SceneUpdateForm().valid_data() psid, pspic, psname, pssort = form.psid.data, form.pspic.data, form.psname.data, form.pssort.data pstimelimited, psstarttime, psendtime = form.pstimelimited.data, form.psstarttime.data, form.psendtime.data isdelete = form.isdelete.data with db.auto_commit(): pssort = self._check_sort(pssort) product_scene = ProductScene.query.filter(ProductScene.isdelete == False, ProductScene.PSid == psid ).first_('不存在的场景') if isdelete: SceneItem.query.filter_by(PSid=psid).delete_() product_scene.isdelete = True admin_action = AdminActions.create({ 'ADid': request.user.id, 'AAaction': 2, 'AAmodel': ProductScene, 'AAdetail': request.detail, 'AAkey': psid }) db.session.add(admin_action) conn.delete(psid) else: product_scene.update({ "PSpic": pspic, "PSname": psname, "PSsort": pssort, "PStimelimited": pstimelimited, "PSstarttime": psstarttime, "PSendtime": psendtime, }, null='not') db.session.add(product_scene) BASEADMIN().create_action(AdminActionS.update.value, 'ProductScene', psid) if form.pstimelimited.data: from planet.extensions.tasks import cancel_scene_association, celery current_app.logger.info('更新限时场景结束时间为 : {} '.format(psendtime)) # celery.control.revoke(task_id=psid, terminate=True, signal='SIGKILL') exist_task = conn.get(psid) if exist_task: exist_task = str(exist_task, encoding='utf-8') current_app.logger.info('场景已有任务id: {}'.format(exist_task)) celery.AsyncResult(exist_task).revoke() scene_task_id = cancel_scene_association.apply_async(args=(psid,), eta=psendtime - timedelta(hours=8), ) conn.set(psid, scene_task_id) return Success('更新成功', {'psid': psid})
def reset_password(self): form = SupplizerResetPasswordForm().valid_data() mobile = form.suloginphone.data password = form.supassword.data if is_supplizer(): code = form.code.data correct_code = conn.get(mobile + '_code') if correct_code: correct_code = correct_code.decode() current_app.logger.info('correct code is {}, code is {}'.format( correct_code, code)) if code != correct_code: raise ParamsError('验证码错误') if not is_admin(): raise AuthorityError() with db.auto_commit(): Supplizer.query.filter(Supplizer.isdelete == False, Supplizer.SUloginPhone == mobile).update({ 'SUpassword': generate_password_hash(password) }) return Success('修改成功')
def send_reset_password_code(self): """发送修改验证码""" if not is_supplizer(): raise AuthorityError() form = SupplizerSendCodeForm().valid_data() mobile = form.suloginphone.data Supplizer.query.filter( Supplizer.isdelete == False, Supplizer.SUloginPhone == mobile).first_('不存在的供应商') exist_code = conn.get(mobile + '_code') if exist_code: return DumpliError('重复发送') nums = [str(x) for x in range(10)] code = ''.join([random.choice(nums) for _ in range(6)]) key = mobile + '_code' conn.set(key, code, ex=60) # 60s过期 params = {"code": code} app = current_app._get_current_object() send_task = Thread(target=self._async_send_code, args=(mobile, params, app), name='send_code') send_task.start() return Success('发送成功')
def _cancle_celery(self, conid): exist_task = conn.get(conid) if exist_task: exist_task = str(exist_task, encoding='utf-8') current_app.logger.info('已有任务id: {}'.format(exist_task)) celery.AsyncResult(exist_task).revoke()