def api_daily_incr(): current = redis_store.exists("api_daily") redis_store.incr("api_daily", amount=1) if not current: now = datetime.now() expire = now.replace(day=(now.day + 1), hour=0, minute=0, second=0, microsecond=0) redis_store.expireat("api_daily", expire)
def api_failure_incr(): current = redis_store.exists("api_failure") redis_store.incr("api_failure", amount=1) if not current: now = datetime.now() expire = now.replace(minute=(now.minute + (now.minute % 15)), second=0, microsecond=0) redis_store.expireat("api_failure", expire)
def content(): if request.method == 'POST': var1 = request.form.get('conf') if var1 == 'running': print("PARAM = " + var1) user = session.get('loginusername', None) if user and redis_store.exists('loginusername:'******'content.html', user=user, isactive=True) return redirect(url_for('index'))
def delete(self, access_token): """DELETE /token/xxx :return: """ if not redis_store.exists(access_token): return {'message': 'access_token does not exist.'} redis_store.delete(access_token) return {'message': access_token + ' is deleted'}
def before_request(): user = session.get('loginusername', None) if user: # if browser session exists, but the timeout expires, then # logout the user if not redis_store.exists('loginusername:'******'loginusername') else: # else, reset the timeout since there is # browser activity redis_store.setex('loginusername:' + user, 60, datetime.datetime.now())
def limit_handler(keyname, amount_limit): incr_amount = 1 # 每次增加数量 # 判断key是否存在 if not redis_store.exists(keyname): # setnx可以防止并发时多次设置key,如果字段已经存在,setnx 命令将不执行任何操作 redis_store.setnx(keyname, 0) # 数据插入后再判断是否大于限制数 if redis_store.incrby(keyname, incr_amount) <= amount_limit: return True return False
def login(): if request.method == 'POST': user = request.form.get('loginusername') password = request.form.get('loginpassword') if ACCESS_CONTROL.get(user, None) and password == ACCESS_CONTROL[user]: if not redis_store.exists('loginusername:'******'loginusername'] = user redis_store.setex('loginusername:'******'content')) flash( "User {} is currently logged in another session".format(user)) if 'loginusername' not in session: return render_template('login.html') return redirect(url_for('index'))
def admin(): url = quote(request.url) appid = config.DevConfig.appID if request.method == 'GET': type = request.args.get('type') code = request.args.get('code') openid = request.args.get('openid') if type == 'test': info = LostAndFound.query.filter_by(state=0).all() return render_template('admin.html', info=info, openid=openid) if code: if redis_store.exists(code): openid = redis_store.mget(code) else: url = wx_models.get_wx_permission(code) return redirect(url % ('admin')) if openid is None: return redirect( config.DevConfig.CODE_URL % (config.DevConfig.appID, url, 'snsapi_userinfo', 'STATE')) if openid and Admin.query.filter_by(user_id=openid).first(): info = LostAndFound.query.filter_by(state=0).all() info = reversed(info) return render_template('admin.html', info=info, openid=openid, appid=appid) else: return render_template('error.html') if request.method == 'POST': data = request.form.get('value') data = ast.literal_eval(data) id = data['id'] user_id = ['user_id'] if bool(data['state']): state = LostAndFoundState.NORMAL LostAndFound.set_state(id, state) return jsonify({'state': 200, 'msg': '已通过'}) else: state = LostAndFoundState.DELETE LostAndFound.set_state(id, state) return jsonify({'state': 200, 'msg': '已打回'})
def index(): if request.method == 'GET': code = request.args.get('code') openid = request.args.get('openid') nickname = request.args.get('nicknama') sex = request.args.get('nickname') url = quote(request.url) if code: if redis_store.exists(code): openid = redis_store.mget(code) else: url = wx_models.get_wx_permission(code) return redirect(url % ('index')) if openid: info = LostAndFound.get_main_list(LostAndFoundState.NORMAL, 30) info = reversed(info) return render_template('index.html', openid=openid, nickname=nickname, sex=sex, appid=config.DevConfig.appID, info=info) if openid is None: return redirect( config.DevConfig.CODE_URL % (config.DevConfig.appID, url, 'snsapi_userinfo', 'STATE')) if request.method == 'POST': data = request.form.get('value') data = ast.literal_eval(data) id = data['id'] user_id = ['user_id'] if bool(data['state']): return else: state = LostAndFoundState.DELETE LostAndFound.set_state(id, state) return jsonify({'state':200,'msg':'删除成功'})
def login(): if request.method == 'POST': user = request.form.get('loginusername') password = request.form.get('loginpassword') if ACCESS_CONTROL.get(user, None) and password == ACCESS_CONTROL[user]: if not redis_store.exists('loginusername:'******'loginusername'] = user redis_store.setex('loginusername:'******'next') #print("** next_page = {}".format(next_page)) #if not next_page or url_parse(next_page).netloc != '': # print("*** NO NEXT PAGE detected ***") # next_page = url_for('content') #return redirect(next_page) return redirect(url_for('content')) flash( "User {} is currently logged in another session".format(user)) if 'loginusername' not in session: return render_template('login.html') return redirect(url_for('index'))
def login(): if current_user.is_authenticated: return redirect(url_for('main.index')) form = LoginForm() # заполняем SelectField пользователями сайта choices = [(key, redis_store.hgetall(key)['username']) for key in redis_store.hkeys('site_users')] if len(choices) > 0: form.username.choices = choices if form.validate_on_submit(): user = redis_store.exists(form.username.data) password_hash = '' if redis_store.hget( form.username.data, 'password_hash') == None else redis_store.hget( form.username.data, 'password_hash') if not user or not check_password_hash(password_hash, form.password.data): flash('Неверный логин или пароль!') return redirect(url_for('auth.login')) user_data = redis_store.hgetall(form.username.data) user_id = form.username.data username = user_data['username'] # email = user_data['email'] password_hash = user_data['password_hash'] operating_mode = user_data['operating_mode'] role = user_data['role'] user = User(user_id=user_id, username=username, password_hash=password_hash, operating_mode=operating_mode, role=role) login_user(user) next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('main.index') return redirect(next_page) return render_template('auth/login.html', form=form)
def index(): user = session.get('loginusername', None) if user and redis_store.exists('loginusername:'******'index.html', user=user, isactive=True) return render_template('index.html', user=user, isactive=False)