def client(): db_fd, meetme.app.config['DATABASE'] = tempfile.mkstemp() client = meetme.app.test_client() with meetme.app.app_context(): flask.current_app() yield client os.close(db_fd) os.unlink(meetme.app.config['DATABASE'])
def get_comment_like(): # 校验是否登陆 user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') # 获取参数:comment_id, news_id, action data_json = request.json comment_id = data_json.get('comment_id') news_id = data_json.get('news_id') action = data_json.get('action') # 校验参数是否齐全 if not all([comment_id, news_id, action]): return jsonify(errno=RET.PARAMERR, errmsg='参数不全') # 校验参数是否正确 if action not in ('add', 'remove'): return jsonify(errno=RET.PARAMERR, errmsg='参数错误') # 存储数据 try: # 根据评论id获取评论对象 comment = Comment.query.get(comment_id) except Exception as e: current_app(e) return jsonify(errno=RET.DBERR, errmsg='查询数据库错误') if not comment: # 校验评论是否存在 return jsonify(errno=RET.NODATA, errmsg='评论数据不存在') # 根据用户id和评论id查询是否已经点赞 comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=g.user.id).first() if action == 'add': if not comment_like: # 没有点赞 # 增加数据 comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id =g.user.id db.session.add(comment_like) comment.like_count += 1 # 点赞数量+1 else: if comment_like: # 已经点赞 db.session.delete(comment_like) # 取消点赞 comment.like_count -= 1 # 点赞数量-1 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='数据库查询错误') return jsonify(errno=RET.OK, errmsg='OK')
def application(environ, start_response): global app # Load virtualenv if necessary if (int(environ.get('ARA_WSGI_USE_VIRTUALENV', 0)) == 1 and environ.get('ARA_WSGI_VIRTUALENV_PATH')): # Backwards compatibility, we did not always suffix activate_this.py activate_this = environ.get('ARA_WSGI_VIRTUALENV_PATH') if 'activate_this.py' not in activate_this: activate_this = os.path.join(activate_this, 'bin/activate_this.py') if six.PY2: execfile(activate_this, dict(__file__=activate_this)) # nosec else: exec(open(activate_this).read()) # nosec if 'ANSIBLE_CONFIG' in environ: os.environ['ANSIBLE_CONFIG'] = environ['ANSIBLE_CONFIG'] else: if 'ANSIBLE_CONFIG' not in os.environ: log.warning('ANSIBLE_CONFIG environment variable not found.') from ara.webapp import create_app # flake8: noqa from flask import current_app # flake8: noqa with app_making_lock: if app is None: app = create_app() with app.app_context(): return current_app(environ, start_response)
def get_relation_db(): """ 获取公司关系的database :return: """ url = current_app().config['DATABASE_RELATIONS_URL'] return get_db(url)
def get_factor_db(): """ 获取影响因子的database :return: """ url = current_app().config['DATABASE_FACTORS_URL'] return get_db(url)
def application(environ, start_response): if 'ANSIBLE_CONFIG' in environ: os.environ['ANSIBLE_CONFIG'] = environ['ANSIBLE_CONFIG'] else: if 'ANSIBLE_CONFIG' not in os.environ: log.warn('ANSIBLE_CONFIG environment variable not found.') with app_making_lock: if app is None: app = create_app() with app.app_context(): return current_app(environ, start_response)
def get_db(url): """ 获取数据库的实例 :param url: :return: """ app = current_app() user = app.config['DATABASE_USER'] password = app.config['DATABASE_PASSWORD'] driver = GraphDatabase.driver(url, auth=(user, password)) return driver
def down_api(): """ 发送请求下载压缩包 :return: """ try: userid = request.form.get("userid") sendid = request.form.get("sendid") STR = CheckTel(userid) TAG = STR.down_api(sendid) TAG = True if TAG: STR.check_after() count = com_reserve.find({"push": "0", "tel_check": "实号"}).count() dict = {} dict["code"] = 200 dict["msg"] = f"可以推送的数据总数有{count}条" return json.dumps(dict) except Exception as e: current_app(e)
def application(environ, start_response): if 'ANSIBLE_CONFIG' in environ: os.environ['ANSIBLE_CONFIG'] = environ['ANSIBLE_CONFIG'] else: if 'ANSIBLE_CONFIG' not in os.environ: log.warn('ANSIBLE_CONFIG environment variable not found.') if not current_app: ctx = app.app_context() ctx.push() return app(environ, start_response) else: return current_app(environ, start_response)
def get_file(form_field): # check if the post request has the file part if form_field not in request.files: raise ROGERUsageError('No \'%s\' part' % form_field) file = request.files[form_field] # if user does not select file, browser also # submit a empty part without filename if file.filename == '': raise ROGERUsageError('No file specified for `%s`' % form_field) if not allowed_file(file.filename): raise ROGERUsageError("Invalid file name in `%s`: %s" % (form_field, file.filename)) filename = secure_filename(file.filename) file.save(os.path.join(current_app().config['ROGER_DATA_FOLDER'], filename)) return file
def setUp(self): self.app = current_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all()
def setUp(self): app = current_app() app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql:///tentest" db.create_all()
from flask import Flask, current_app app = Flask(__name__) # ctx = app.app_context() # ctx.push() # a = current_app() # b = a.config['DEBUG'] # ctx.pop() with app.app_context(): a = current_app() b = a.config['DEBUG'] print(b) # 上下文管理器类 class MyResource: def __init__(self): my_value = "上下文为函数提供的'全局'变量" def __enter__(self): print("连接资源") return self # 给as后的变量传递对象 """ exc_type, exc_val, exc_tb 用于接收异常信息 没有发生异常则三个参数为None 发生异常则接收异常信息 """