def test_local_app_title(test_client): test_client.get(url_for("hello")) from app import redis as main_redis hits_prev = int(main_redis.get('hits')) test_client.get(url_for("hello")) hits = int(main_redis.get('hits')) assert hits - hits_prev == 1, "Number of hits not incremented"
def _usagecapacity(service): """calculate the current usage of the service.""" usage = 0 capacity = 0 busy = 0 detail = {} for resource in service.list_resources(): detail[resource] = {'busy': '', 'reserved': ''} r_capacity = service.list_resources()[resource] detail[resource]['capacity'] = r_capacity capacity += r_capacity reserved = redis.get("reserved:%s:%s" % (service.name, resource)) if reserved: detail[resource]['reserved'] = reserved r_usage = redis.hgetall("resource:%s:%s" % (service.name, resource)).values() count_usage = {} for r in r_usage: if r in count_usage: count_usage[r] += 1 else: count_usage[r] = 1 detail[resource]['usage'] = [ "%s: %d" % (k, count_usage[k]) for k in count_usage ] usage += len(r_usage) err = redis.get("busy:%s:%s" % (service.name, resource)) if err: detail[resource]['busy'] = err busy = busy + 1 queued = redis.llen("queued:" + service.name) return usage, queued, capacity, busy, detail
def get(self, product_id): if redis.get('catalog.product-' + str(product_id)) != None: return json.loads(redis.get('catalog.product-' + str(product_id))), 201 product = ProductModel.get_product(product_id) if (product == None): return {'message': 'Product not found.'}, 404 product.pop('_id') compact = { 'name': product['name'], 'price': product['price'], 'status': product['status'], 'categories': product['categories'], } redis.set('catalog.product-' + str(product_id), json.dumps({ 'complete': product, 'compact': compact }), ex=(60 * int(os.environ["CACHE_CATALOG_PRODUCTS_EXPIRE"]))) return {"compact": compact, "complete": product}, 200
def getRedisValue(): key = request.args.get("key").strip() if "attr" in key: redis_key = key[2:-2].split("&&attr(") try: redis_value = redis.get(redis_key[0]) except Exception as e: redis_pass_value = "%s获取失败,请检查上下文接口传递参数或检查参数格式!" % (redis_key[0]) resp = {"datas": redis_pass_value, "code": "400"} return make_response(jsonify(resp)) if redis_value: print redis_value new_redis_value = redis_value.decode("utf-8").replace( "false,", "\"false\",").replace("true,", "\"true\",").replace( "null,", "\"null\",") redis_pass_value = str( eval(new_redis_value + redis_key[1].decode("utf-8"))) else: redis_pass_value = "未获取到缓存数据,请检查参数!" else: try: redis_value = redis.get(key) except Exception as e: redis_pass_value = "%s获取失败,请检查上下文接口传递参数或检查参数格式!" % (redis_value) resp = {"datas": redis_pass_value, "code": "400"} return make_response(jsonify(resp)) if redis_value: redis_pass_value = redis_value else: redis_pass_value = "未获取到缓存数据,请检查参数!" resp = {"datas": redis_pass_value, "code": "200"} return make_response(jsonify(resp))
def load_municipalities(): csv = read_csv_from_s3('redshift/attrs/attrs_municipios.csv') df = pd.read_csv(csv, sep=';', header=0, names=[ 'uf_id', 'uf_name', 'mesorregiao_id', 'mesorregiao_name', 'microrregiao_id', 'microrregiao_name', 'municipio_id', 'municipio_name', 'municipio_id_mdic' ], converters={ "uf_id": str, "mesorregiao_id": str, "microrregiao_id": str, "municipio_id": str }) municipalities = {} microregions = {} mesoregions = {} for _, row in df.iterrows(): municipality = { 'id': row['municipio_id'], 'name_pt': row["municipio_name"], 'name_en': row["municipio_name"], 'mesoregion': { 'id': row["mesorregiao_id"], 'name_pt': row["mesorregiao_name"], 'name_en': row["mesorregiao_name"], }, 'microregion': { 'id': row["microrregiao_id"], 'name_pt': row["microrregiao_name"], 'name_en': row["microrregiao_name"], }, 'state': pickle.loads(redis.get('state/' + row['municipio_id'][:2])), 'region': pickle.loads(redis.get('region/' + row['municipio_id'][0])), } municipalities[row['municipio_id']] = municipality microregions[row['microrregiao_id']] = municipality['microregion'] mesoregions[row['mesorregiao_id']] = municipality['mesoregion'] redis.set('municipality/' + str(row['municipio_id']), pickle.dumps(municipality)) redis.set('microregion/' + str(row['microrregiao_id']), pickle.dumps(municipality['microregion'])) redis.set('mesoregion/' + str(row['mesorregiao_id']), pickle.dumps(municipality['mesoregion'])) redis.set('municipality', pickle.dumps(municipalities)) redis.set('microregion', pickle.dumps(microregions)) redis.set('mesoregion', pickle.dumps(mesoregions)) print "Municipalities, microregions and mesoregions loaded."
def _usagecapacity(service): """calculate the current usage of the service.""" usage_gpu = 0 usage_cpu = 0 capacity_gpus = 0 capacity_cpus = 0 busy = 0 detail = {} servers = service.list_servers() for resource in service.list_resources(): detail[resource] = {'busy': '', 'reserved': ''} r_capacity = service.list_resources()[resource] detail[resource]['capacity'] = r_capacity capacity_gpus += r_capacity detail[resource]['ncpus'] = servers[resource]['ncpus'] capacity_cpus += servers[resource]['ncpus'] reserved = redis.get("reserved:%s:%s" % (service.name, resource)) if reserved: detail[resource]['reserved'] = reserved count_map_gpu = Counter() task_type = {} count_map_cpu = {} count_used_gpus = 0 count_used_cpus = 0 r_usage_gpu = redis.hgetall("gpu_resource:%s:%s" % (service.name, resource)).values() for t in r_usage_gpu: task_type[t] = redis.hget("task:%s" % t, "type") count_map_gpu[t] += 1 count_used_gpus += 1 if t not in count_map_cpu: count_map_cpu[t] = int(redis.hget("task:%s" % t, "ncpus")) count_used_cpus += count_map_cpu[t] r_usage_cpu = redis.lrange("cpu_resource:%s:%s" % (service.name, resource), 0, -1) for t in r_usage_cpu: task_type[t] = redis.hget("task:%s" % t, "type") if t not in count_map_cpu: count_map_cpu[t] = int(redis.hget("task:%s" % t, "ncpus")) count_map_gpu[t] = 0 count_used_cpus += count_map_cpu[t] detail[resource]['usage'] = ["%s %s: %d (%d)" % (task_type[k], k, count_map_gpu[k], count_map_cpu[k]) for k in count_map_gpu] detail[resource]['avail_cpus'] = int(redis.get("ncpus:%s:%s" % (service.name, resource))) detail[resource]['avail_gpus'] = r_capacity-count_used_gpus err = redis.get("busy:%s:%s" % (service.name, resource)) if err: detail[resource]['busy'] = err busy = busy + 1 usage_cpu += count_used_cpus usage_gpu += count_used_gpus queued = redis.llen("queued:"+service.name) return ("%d (%d)" % (usage_gpu, usage_cpu), queued, "%d (%d)" % (capacity_gpus, capacity_cpus), busy, detail)
def _usagecapacity(service): """calculate the current usage of the service.""" usage_xpu = Capacity() capacity_xpus = Capacity() busy = 0 detail = {} for resource in service.list_resources(): detail[resource] = {'busy': '', 'reserved': ''} r_capacity = service.list_resources()[resource] detail[resource]['capacity'] = r_capacity capacity_xpus += r_capacity reserved = redis.get("reserved:%s:%s" % (service.name, resource)) if reserved: detail[resource]['reserved'] = reserved count_map_gpu = Counter() count_map_cpu = Counter() task_type = {} count_used_xpus = Capacity() r_usage_gpu = redis.hgetall("gpu_resource:%s:%s" % (service.name, resource)).values() for t in r_usage_gpu: if t not in task_type: task_type[t] = redis.hget("task:%s" % t, "type") count_map_gpu[t] += 1 count_used_xpus.incr_ngpus(1) r_usage_cpu = redis.hgetall("cpu_resource:%s:%s" % (service.name, resource)).values() for t in r_usage_cpu: if t not in task_type: task_type[t] = redis.hget("task:%s" % t, "type") count_map_cpu[t] += 1 count_used_xpus.incr_ncpus(1) detail[resource]['usage'] = [ "%s %s: %d (%d)" % (task_type[t], t, count_map_gpu[t], count_map_cpu[t]) for t in task_type ] detail[resource][ 'avail_gpus'] = r_capacity.ngpus - count_used_xpus.ngpus detail[resource][ 'avail_cpus'] = r_capacity.ncpus - count_used_xpus.ncpus err = redis.get("busy:%s:%s" % (service.name, resource)) if err: detail[resource]['busy'] = err busy = busy + 1 usage_xpu += count_used_xpus queued = redis.llen("queued:" + service.name) return ("%d (%d)" % (usage_xpu.ngpus, usage_xpu.ncpus), queued, "%d (%d)" % (capacity_xpus.ngpus, capacity_xpus.ncpus), busy, detail)
def get_state(): gp.setwarnings(False) gp.setmode(gp.BOARD) gp.setup(PIN, gp.OUT) if redis.get(app.config.get('PIN_STATE')) is None: set_state(False) v = redis.get(app.config.get('PIN_STATE')) if v is None: raise RuntimeError('pinstate failed to set') return v != "False"
def init_wechat_sdk(): """ 初始化微信sdk 设置保存一些参数 """ wechat_client = WeChatClient(current_app.config['APPID'], current_app.config['APPSECRET']) access_token = redis.get("wechat:access_token") jsapi_ticket = redis.get("wechat:jsapi_ticket") token_expires_at = redis.get("wechat:access_token_expires_at") ticket_expires_at = redis.get("wechat:jsapi_ticket_expires_at") if access_token and jsapi_ticket: wechat = { 'client': wechat_client, 'appid': current_app.config['APPID'], 'appsecret': current_app.config['APPSECRET'], 'token': current_app.config['TOKEN'], 'access_token': access_token, 'access_token_expires_at': int(token_expires_at), 'jsapi_ticket': jsapi_ticket, 'jsapi_ticket_expires_at': int(ticket_expires_at) } return wechat # 没有缓存 else: access_token_dic = wechat_client.fetch_access_token() access_token = access_token_dic['access_token'] token_expires_at = access_token_dic['expires_in'] jsapi_ticket_dic = wechat_client.jsapi.get_ticket() jsapi_ticket = jsapi_ticket_dic['ticket'] ticket_expires_at = jsapi_ticket_dic['expires_in'] redis.set("wechat:access_token", access_token, 7000) redis.set("wechat:access_token_expires_at", token_expires_at, 7000) redis.set("wechat:jsapi_ticket", jsapi_ticket, 7000) redis.set("wechat:jsapi_ticket_expires_at", ticket_expires_at, 7000) wechat = { 'client': wechat_client, 'appid': current_app.config['APPID'], 'appsecret': current_app.config['APPSECRET'], 'token': current_app.config['TOKEN'], 'access_token': access_token, 'access_token_expires_at': int(token_expires_at), 'jsapi_ticket': jsapi_ticket, 'jsapi_ticket_expires_at': int(ticket_expires_at) } return wechat
def get_job(): """ 获取配置文件 :return: json """ err_data = {"data": {}, "error_code": 1} operator_code = request.values.get("operator_code", 0) if operator_code == 0: try: post = request.get_data().decode("utf-8") post = json.loads(post) operator_code = post['operator_code'] except Exception as e: debug(e) return Reply.json(err_data) operator_code = "op_{operator_code}".format(operator_code=operator_code) data = redis.get(operator_code) if data is None: return Reply.json(err_data) try: data = json.loads(data) except Exception as e: data = err_data debug(e) return Reply.json(data)
def verify_password(username_or_token, password): try: if not username_or_token: return False if not password: # token g.current_user = User.verify_auth_token(username_or_token) g.token_used = True if g.current_user: token_id = 'Token-User-ID-' + str(g.current_user.id) token = redis.get(token_id) if token and token.decode('utf-8') == username_or_token: return True return False user = User.query.filter_by(name=username_or_token).first() if not user: return False g.current_user = user g.token_used = True if user.verify_auth_password(password): return True return False except: return False
def api(data, id=None): data = singularize(data) if id: data = data + '/' + id return jsonify(pickle.loads(redis.get(data)))
def input(): if request.method == 'POST': msg = request.form.get('msg') messages = [(key, redis.get(key)) for key in sorted(redis.scan_iter())] key = hashlib.sha1(str(messages).encode('utf-8')).hexdigest() redis.setex(key, timedelta(minutes=5), value=msg) return render_template('input.html', title='Input')
def index(): keys: list = redis.keys(f'{current_user.id}_*') data: list = [] for key in keys: data.append(json.loads(redis.get(key))) return render_template('blog/index.html', data=data if data is not None else [])
def home(): form = URLShortenerForm() if form.validate_on_submit(): print("Form validated") cached = redis.get(form.url.data) if cached: short_url = cached else: url = Url.query.filter_by(value=form.url.data).first() if url: redis.set(form.url.data, url.key) short_url = url.key else: generated_key = generate_key(form.url.data) redis.set(form.url.data, generated_key) url = Url(key=generated_key, value=form.url.data) db.session.add(url) db.session.commit() short_url = generated_key flash( 'Short url: ' + url_for('.url_parser', key=short_url, _external=True)[:-1], 'success') return redirect(url_for('.home')) return render_template('home.html', title='TinyUrl Flask App', form=form)
def auth_library_result(openid=None): """查询借书卡绑定结果""" if is_user_exists(openid): redis_prefix = 'wechat:user:auth:library:' errmsg = redis.get(redis_prefix + openid) if errmsg: redis.delete(redis_prefix + openid) return jsonify({'errmsg': errmsg}) else: abort(404) else: abort(404) """ 修改用户分组 """ if form.validate_on_submit(): groupemp = [] groupemp_list = form.group.data.split(',') for t in groupemp_list: tag = Group.query.filter_by(name=t).first() if tag is None: tag = Group() tag.name = t groupemp.append(tag) user.user_group = groupemp user.save() return redirect(url_for('wechat.user')) value = ",".join([i.name for i in user.user_group]) return render_template('wechat/editgroup.html', form=form, user=user, value=value)
def get_team_id(sport, team): redis_key = app.config["REDIS_KEY_TEAMS"].replace( app.config["REDIS_KEY_TOKEN_SPORT"], "nfl" if "fb" == sport else sport) # First, check if the redis key exists and if it doesn't, recreate # if not redis.exists(redis_key): teams_helper(sport) # If we can't find it now, then something is definitely wrong. teams = redis.get(redis_key) if not teams: return None teams = loads(teams) # Replace usual tokens which represent whitespace with an actual # space. team = sub(r"(\+|_|-)", ' ', team) # Search for the requested team ID rv = [ idx for idx, val in enumerate(teams["data"]) if val.lower().startswith(team) ] # If there is more than 1 result or if there are no results if len(rv) > 1 or not rv: return None # Add 1 to fix the off-by-one error. Internally, the team with the # ID #1 is actually 0. return 1 + int(rv.pop())
def check_token(*args, **kwargs): data = request.get_json() token = None if data: token = data.get("token") if request.cookies: token = request.cookies.get("token") if token == None: return_data = dict(Messages.error_invalid_token) resp = jsonify(return_data) resp.status_code = 200 return resp # Token has been 'logged out' if redis.get(token) != None: return_data = dict(Messages.error_invalid_token) resp = jsonify(return_data) resp.status_code = 200 return resp try: data = jwt.decode(token, app.jwt_key) except (ValueError, jwt.exceptions.DecodeError, jwt.ExpiredSignatureError): return_data = dict(Messages.error_invalid_token) resp = jsonify(return_data) resp.status_code = 200 return resp kwargs["username"] = data["username"] kwargs["token"] = token kwargs["token_expire"] = data["exp"] return func(*args, **kwargs)
def get_app_data(socket, uuid, app_uuid, app_info, global_data): key = app_uuid + "_result" if redis.exists(key) == 0: s, result = execute( app_dir=app_info["app_dir"], data=app_info["data"], global_data=global_data ) if not s: return s, result global_data.update(result["data"]) output = result["output"] redis.set(key, output, ex=3) print("uuid : ", app_uuid) print("name : ", app_info["data"]["node_name"]) print("result : ", output) print("===================================") add_execute_logs( socket=socket, uuid=uuid, app_uuid=app_uuid, app_name=app_info["data"]["node_name"], result=output, ) return s, result else: return True, redis.get(key).decode()
def get_lock(key, timeout=10, user_wait_timeout=15): import time _lock, lock_key = 0, "%s_dynamic_lock" % key wait_timestamp = int(time.time()) + user_wait_timeout while _lock != 1: timestamp = int(time.time() + timeout) # 延时11秒 _lock = redis.setnx(lock_key, timestamp) # 如果持有锁,当前时间大于过期时间,说明已经超时了 if _lock == 1: redis.expire(lock_key, int(time.time()) + timeout) return 'success', lock_key lock_key_time = redis.get(lock_key) if lock_key_time: if int(time.time()) > int(lock_key_time): new_time = redis.getset(lock_key, int(time.time()) + timeout) if not new_time or lock_key_time == new_time: redis.expire(lock_key, int(time.time()) + timeout) return 'success', lock_key else: if int(time.time()) > wait_timestamp: return 'timeout', lock_key time.sleep(0.1) else: if int(time.time()) > wait_timestamp: return 'timeout', lock_key time.sleep(0.3)
def decorated_function(*args, **kwargs): cache_key = key % request.path rv = redis.get(cache_key) if rv: return pickle.loads(rv) rv = f(*args, **kwargs) redis.setex(cache_key, timeout, pickle.dumps(rv)) return rv
def profile(username): users_json = redis.get('users') users = json.loads(users_json) user = None if username in users: user = users[username] return render_template('public/dynamic.html', user=user, username=username)
def get_wechat_access_token(): """ 从redis获取access_token """ access_token = redis.get("wechat:access_token") if access_token: return access_token else: current_app.logger.warning("从缓存获取 access_token 失败") return None
def api_statistics(realm: str): """ Fetch statistics on COVID at Yale or in Connecticut. :param realm: the context for which to get statistics - 'yale' or 'connecticut'. """ if realm not in ('yale', 'connecticut'): abort(404) statistics = json.loads(redis.get('yale')) return jsonify(statistics)
def get_short_link(short_link): """ get: redirect to full link permanent """ full_link = redis.get(short_link) if full_link is None: abort(404) app.logger.info(f' short link {short_link} redirect to: {full_link}') return redirect(full_link, code=302)
def logs(id): log = redis.get('%s_l' % id) if log is None: return "Nothing found" info = redis.hgetall(id) logs = log.split('\n') return render_template('logs.html', logs=logs, info=info, log_id=id)
def get_ticket(imdbId, season='', episode=''): redisRes = redis.get(imdbId) ticket = redisRes.decode("utf-8") if redisRes else None if not ticket: ticket = req(imdbId) else: ticketUsage = redis.get(ticket) ticketUsage = int(ticketUsage) if ticketUsage else 0 # ticket as 50 usage if ticketUsage >= 49: redis.delete(ticket) ticket = req(imdbId) else: redis.incr(ticket) return str(ticket)
def confirmed(openid): """ 确认已收到id为'media_id'的信息推送 """ user = WechatUser.query.filter_by(openid=openid).first() user_id = user.id last_push_time = redis.get("wechat:last_push_time") # last_push_time = redis.get("wechat:last_pushtext_time") # timeout = int(last_push_time.decode()) - int(get_user_last_interact_time(openid)) # current_app.logger.warning('超时%s' % timeout) last_push_cache = redis.hgetall("wechat:last_push") media_id = last_push_cache[b'media_id'].decode() pushtype = last_push_cache[b'pushtype'].decode() if pushtype == 'text': pushtext = Pushtext.query.filter_by(media_id=media_id).first() to_confirmed_before = pushtext.to_confirmed content = pushtext.content # 这里我是把list转为json存到数据库,所以去出来的时候要转回来 to_confirmed = json.loads(to_confirmed_before) try: if user_id in to_confirmed: to_confirmed.remove(user_id) pushtext.to_confirmed = json.dumps(to_confirmed) pushtext.update() content = "你已确认获悉此条及在其之前发布的通知:\n【%s】"% content return content else: content = "【你已回复'收到', 无需重复回复!】" return content except Exception as e: current_app.logger.warning('用户重复回复了收到,错误信息:%s') % e return '出错, 请稍后在试试' elif pushtype == 'news': pushnews=Pushnews.query.filter_by(media_id=media_id).first() to_confirmed_before = pushnews.to_confirmed title=pushnews.title to_confirmed = json.loads(to_confirmed_before) current_app.logger.warning('to_confirmed_应该是个list %s' % to_confirmed) try: if user_id in to_confirmed: to_confirmed.remove(user_id) pushnews.to_confirmed = json.dumps(to_confirmed) pushnews.update() content = "你已确认获悉此条及在其之前发布的通知:\n【%s】" % title return content else: content = "【你已回复'收到', 无需重复回复!】" return content except Exception as e: current_app.logger.warning('用户重复回复了收到,错误信息:%s') % e cntent="出错, 请稍后在试试" return content
def test_redirect_by_link_success(self): full_link = self.long_link redis.incr('linked_id') counter = int(redis.get('linked_id')) short_link = comp.compress_url(counter) redis.set(short_link, full_link, nx=True) short_link = 'http://0.0.0.0:5000/' + short_link response = self.client.get(short_link) assert response.status_code == 308
def redis_get(key): value = redis.get(key) if value is not None: s = Serializer(current_app.config["SECRET_KEY"]) try: data = s.loads(value) return data except Exception: return None return None
def redis_get(key): value = redis.get(key) if value is not None: s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(value) return data except Exception: return None return None
def app_(guid): if not is_valid_guid(guid): abort(400) # request ongoing? status = redis.get(redis_key('%s.%s' % (guid, 'status'))) if status is None: Popen(['python2', 'wpsc/checker.py', guid]) # results have expired, rebuild return render_template('app.html', guid=guid)
def get_content_list(url, key=None): if key and redis.exists(key): txt = redis.get(key).decode() else: txt = get_content_text(url) lis = txt.split('\n') li = [] for l in lis: if l != '' and l != '\t': li.append(l) return li
def serie_info(imdbId): redisRes = redis.get(imdbId + '-info') serie_info = redisRes.decode("utf-8") if redisRes else None if not serie_info: serie_info = omdb.info(imdbId) redis.set(imdbId + '-info', json.dumps(serie_info)) else: serie_info = json.loads(serie_info) return jsonify(serie_info)
def fetch_from_redis() -> list or None: if current_app.config.get("NO_REDIS"): return None try: return loads( redis.get( name=f"{edu}#{school}#{date}" ) ) except (ConnectionError, TypeError, Exception): return None
def get_post(post_id): rpost = redis.get(str(post_id)) rpost = None if rpost is None: post = Post.query.filter_by(id = post_id).first() if post is None: abort(404) redis.set(str(post_id), pickle.dumps(post)) else: post = pickle.loads(rpost) return get_view(post)
def search(query): query_re = re.compile(query) cache = redis.get(query) if cache: results = pickle.loads(cache) else: results = list(mongo.db.flower.find( {"$or": [ {"name": query_re}, {"description": query_re}]})) redis.setex(query, (60*60*5), pickle.dumps(results)) print "Redis: {} Memory".format(redis.info()['used_memory_human']) return jsonify(**{'results': results})
def check(guid): if not is_valid_guid(guid): abort(400) # request ongoing? status = redis.get(redis_key('%s.%s' % (guid, 'status'))) if status is None: abort(500) # this should not happen, unless load average is too high # error occured? if status == 'error': return jsonify(status='error') elif status == 'processing': percent = redis.get(redis_key('%s.%s' % (guid, 'percent'))) return jsonify(status='processing', percent=percent) elif status == 'done': results = {} for key in redis.keys('%s.*' % (redis_key(guid),)): if key not in ['%s.status' % (redis_key(guid),), '%s.percent' % (redis_key(guid),)]: results[key] = redis.hgetall(key) return jsonify(status='done', results=render_template('table.html', results=results)) abort(500) # is this even possible?
def wrapper(*args, **kwargs): key = "{0}: {1}".format(request.remote_addr, request.path) try: remaining = limit - int(redis.get(key)) except (ValueError, TypeError): remaining = limit redis.set(key, 0) expires_in = redis.ttl(key) if not expires_in: redis.expire(key, window) expires_in = window g.rate_limits = (limit, remaining-1, time()+expires_in) if remaining > 0: redis.incr(key, 1) return func(*args, **kwargs) return TOO_MANY_REQUESTS
def login(): redirect_uri = request.args.get('redirect_uri') or '/dashboard' if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') redirect_uri = request.form.get('redirect_uri') _u = users.first(username=username) if _u and _u.verify_password(password): # 获取缓存,动态设置config role_map = redis.get('roles').decode('utf-8') current_app.config['roles'] = json.loads(role_map) # 处理登录、生成token resp = jsonify(code=200, msg='登录成功', redirect_uri=redirect_uri) payload = dict( exp = datetime.datetime.utcnow() + datetime.timedelta(hours=8), rid = _u.role_id, uid = _u.id ) token = jwt.encode(payload, current_app.config['SECRET_KEY'], algorithm='HS256') resp.set_cookie('token', token) return resp else: return jsonify(code=500, msg='用户名或密码错误') return render_template('auth/login.html', redirect_uri=redirect_uri)
def get_user_last_activity(user_id): last_active = redis.get('user-activity/%s' % user_id) if last_active is None: return None return datetime.datetime.utcfromtimestamp(int(last_active))
def from_sha(cls, sha): if redis.sismember("messages", sha): return cls.from_json(literal_eval(redis.get("message:{}".format(sha)))) else: return None
def check_captcha(sha, captcha): return redis.get("token:{}".format(sha)) and sha1(captcha).hexdigest() == sha
def from_sha(cls, sha): token = literal_eval(redis.get("token:{}".format(sha))) if token: return cls(**token) else: return None
def cookies(): cookies = redis.get(config.COOKIE_KEY) if not cookies: raise AuthError('could not load cookies') return json.loads(cookies)
def logdata(id): log = redis.get('%s_l' % id) logs = log.split('\n') return jsonify({'logs': logs})
def handle_stellar_message(message): # What to do when we receive a message # Check it is of transaction type m = json.loads(message) if m['type'] == 'transaction': if m['transaction']['Destination'] == STELLAR_ADDRESS and m['transaction']['TransactionType'] == 'Payment' and m['validated'] is True and m['engine_result_code'] == 0: # Check with stellar to make sure tx is legit tx_hash = m['transaction']['hash'] ledger_index = m['ledger_index'] r = verify_tx(tx_hash, ledger_index) if r['result']['status'] == 'success': # Check tx type and destination address tx = r['result']['tx_json'] if 'ledger_index' in tx and tx['TransactionType'] == 'Payment' and tx['Destination'] == STELLAR_ADDRESS: # tx has been verified, add to database sender_address = tx['Account'] sender = SendbackAccount.query.filter_by(stellar_address=sender_address).first() if sender is None: sender_id = None else: sender_id = sender.id # Handle payments without destination tags if 'DestinationTag' in tx: dt = tx['DestinationTag'] else: dt = None sendback = SendbackTransaction( created_time=datetime.utcnow(), sendback_account=sender_id, account_sender=sender_address, amount_sender=tx['Amount'], destination_sender=tx['Destination'], destination_tag_sender=dt, fee_sender=tx['Fee'], flags_sender=tx['Flags'], sequence_sender=tx['Sequence'], signing_pub_key_sender=tx['SigningPubKey'], tx_signature_sender=tx['TxnSignature'], tx_hash_sender=tx['hash'], ledger_index_sender=tx['ledger_index'] ) db.session.add(sendback) db.session.commit() # Create new Payable amount_owed = sendback.amount_sender - calculate_fee(sendback.amount_sender) dest = sendback.account_sender payable = Payable( created_time=datetime.utcnow(), destination=dest, amount=amount_owed, sendback_transaction=sendback.id, sendback_account=sender_id, tx_signed=False, tx_submitted=False, tx_validated=False ) db.session.add(payable) db.session.commit() seq = redis.get('Sequence') redis.incr('Sequence') pending = PendingTransaction( payable=payable.id, destination=payable.destination, amount=payable.amount, created_time=datetime.utcnow(), tx_signed=False, tx_submitted=False, tx_validated=False, sequence=seq ) db.session.add(pending) db.session.commit() do_payment.delay(pending.id)
def get_token(password): return redis.get(_PASSWORD_PREFIX + password)
def csrf(): csrf = redis.get(config.CSRF_KEY) if not csrf: raise AuthError('could not load csrf token') return csrf