view.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:Feature-Request-Python-Flask 作者: jmcgrath207 项目源码 文件源码
def api_key():
    """" Request url for API key for Restful API"""

    if request.method == "POST":
        content = request.get_json(silent=True)
        user = User.query.filter_by(Client_id=content['Client_id']).first()
        if not user and app.config['NO_PASSWORD'] == True:
            ##Allow anyone to create their own account
            pass_hash = bcrypt_sha256.encrypt(content['Password'], rounds=12)
            user = User(Client_id= content['Client_id'],Password= pass_hash,api_key='')
            Sample_date = Client_View(client_id = content['Client_id'],
                                         case_name= 'sample case',
                                         priority= '1',
                                         target_date = '10/7/2016',
                                         product_area = 'Engineering',
                                          status = 'In Progress',
                                         description= 'something'
                                         )
            db.session.add(user)
            db.session.commit()
            db.session.add(Sample_date)
            db.session.commit()
        """ If user is a vaild account, proceed with verifying \
        their credentials and provide them the API key"""
        if user:
            if bcrypt_sha256.verify(content['Password'], user.Password) and user.Client_id == content['Client_id']:
                signer = TimestampSigner(SECRET_KEY)
                API_KEY = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(30)])
                user.api_key = signer.sign(API_KEY)
                user.current_login_ip = request.remote_addr
                db.session.commit()
                return make_response(jsonify({'API KEY': user.api_key}), 200)
        return make_response(jsonify({'Failure': 'Incorrect Client id OR Password'}), 400)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号