Exemplo n.º 1
0
def respUploadedGridfsFile(request, respDict, curGridfs):
    curMimeType = ""
    requestHeaders = request.headers
    if "filename" in requestHeaders:
        filename = requestHeaders[
            "filename"]  # '%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20181%20228160748.jpg'
        filename = unquote(filename)  # '微信图片_20181 228160748.jpg'
        curMimeType = mime.Types.of(filename)[0].content_type
    else:
        filename = getCurDatetimeStr()

    fileBinData = request.stream.read()
    fileLikeObj = io.BytesIO(fileBinData)
    log.debug("filename=%s, curMimeType=%s, len(fileBinData)=%s", filename,
              curMimeType, len(fileBinData))

    newFileObjId = curGridfs.put(fileLikeObj,
                                 filename=filename,
                                 content_type=curMimeType)

    if newFileObjId:
        readOutFile = curGridfs.get(newFileObjId)
        respDict["data"] = {
            "_id": str(readOutFile._id),
            "filename": readOutFile.filename,
            "contentType": readOutFile.contentType,
            "length": readOutFile.length,
            "uploadDate": str(readOutFile.uploadDate),
            "md5": readOutFile.md5,
        }
        return respDict
    else:
        return genRespFailDict(InternalServerError.code,
                               "Fail to create gridfs file %s" % filename)
Exemplo n.º 2
0
def register_extensions(app):
    mongo = create_mongo(app)
    g.mongo = mongo
    log.info("mongo=%s", mongo)
    mongoServerInfo = mongo.server_info()
    # log.debug("mongoServerInfo=%s", mongoServerInfo)

    db_flask_template, collection_car, gridfs_files = create_mongo_collection(
        mongo)
    g.mongoDbFlaskTemplate = db_flask_template
    g.mongoDbFlaskTemplateCollectionCar = collection_car
    g.mongoDbFlaskTemplateGridfsFiles = gridfs_files

    mysql_connection = create_mysql_connection(app)
    g.sqlConn = mysql_connection
    g.sqlConn.isUseLog = False

    # redis_store = FlaskRedis(app)
    # Note: support later get out value is str, not bytes
    redis_store = FlaskRedis(app, charset="utf-8", decode_responses=True)
    g.redisStore = redis_store

    api = create_rest_api(app)
    log.debug("api=%s", api)
    g.api = api

    return app
Exemplo n.º 3
0
def new_mysql_connection():
    mysqlConfigDict = {
        'host': settings.MYSQL_HOST,
        'port': settings.MYSQL_PORT,
        'user': settings.MYSQL_USER,
        'password': settings.MYSQL_PASSWORD,
        'db': settings.MYSQL_DB,
        'charset': settings.MYSQL_CHARSET,
    }
    curReqSqlConn = getMysqlConnection(mysqlConfigDict)
    log.debug("new_mysql_connection: curReqSqlConn=%s", curReqSqlConn)
    request.curReqSqlConn = curReqSqlConn
    log.debug("request.curReqSqlConn=%s", request.curReqSqlConn)
Exemplo n.º 4
0
    def get(self, fileId, fileName=None):
        # 'http://10.108.133.251:34800/file/evaluation/image/5c32f6ae12758802476f7cd2/hide%20in%20the%20box.png'
        # 'http://10.108.133.251:34800/file/storybook/audio/5c482cb6dc54867a9eff34a9/All%20By%20Myself.mp3'
        log.info("FileFlaskTemplateFilesAPI GET: fileId=%s, fileName=%s",
                 fileId, fileName)
        range = None
        if "Range" in request.headers:
            range = request.headers["Range"]
            log.debug("range=%s", range)
        # return getFile(MongoFileType.STORYBOOK_AUDIO.value, fileId, fileName, range)

        # def getFile(fileType, fileId, fileName=None, range=None):
        # log.info("getFile: fileType=%s, fileId=%s, fileName=%s", fileType, fileId, fileName)

        curGridfs = mongoDbFlaskTemplateGridfsFiles
        asAttachment = settings.GRIDFS_FILE_AS_ATTACHMENT
        return getGridfsFile(curGridfs,
                             fileId,
                             fileName,
                             asAttachment=asAttachment,
                             range=range)
Exemplo n.º 5
0
    def updateUserTime(self, userId, createdTime=None, updatedTime=None, lastActiveTime=None, curSqlConn=None):
        if not curSqlConn:
            curSqlConn = sqlConn

        updateTimeDictList = []

        if createdTime:
            createdTimeSql = "`createdTime`='%s'" % createdTime
            updateTimeDictList.append(createdTimeSql)

        if updatedTime:
            updatedTimeSql = "`updatedTime`='%s'" % updatedTime
            updateTimeDictList.append(updatedTimeSql)

        if lastActiveTime:
            lastActiveTimeSql = "`lastActiveTime`='%s'" % lastActiveTime
            updateTimeDictList.append(lastActiveTimeSql)

        updateTimeSql = ",".join(updateTimeDictList)
        log.debug("updateTimeSql=%s", updateTimeSql)
        updateUserTimeSql = "UPDATE `user` SET %s WHERE id=%d" % (updateTimeSql, userId)
        log.debug("updateUserTimeSql=%s", updateUserTimeSql)
        # updateUserTimeOk, resultDict = sqlConn.executeSql(updateUserTimeSql)
        updateUserTimeOk, resultDict = curSqlConn.executeSql(updateUserTimeSql)
        log.debug("%s -> %s, %s", updateUserTimeSql, updateUserTimeOk, resultDict)
Exemplo n.º 6
0
    def respFullUserInfo(self, respDict, existedUserId, curSqlConn=None):
        log.debug("respFullUserInfo: respDict=%s, existedUserId=%s", respDict, existedUserId)
        if not curSqlConn:
            curSqlConn = sqlConn

        existedUserSql = "SELECT * FROM `user` WHERE id = '%s'" % existedUserId
        log.debug("existedUserSql=%s", existedUserSql)
        existedUserOk, resultDict = curSqlConn.executeSql(existedUserSql)
        log.debug("%s -> %s, %s", existedUserSql, existedUserOk, resultDict)
        if (not existedUserOk) or (not resultDict["data"]):
            return genRespFailDict(NotFound.code, "Can not found user from user id %s" % existedUserId)

        existedUser = resultDict["data"][0]
        log.debug("existedUser=%s", existedUser)

        respDict["data"] = existedUser
        return jsonify(respDict)
Exemplo n.º 7
0
    def get(self):
        log.info("UserAPI GET")

        # curReqSqlConn = getMysqlConnection()
        curReqSqlConn = request.curReqSqlConn
        # log.debug("curReqSqlConn=%s", curReqSqlConn)

        respDict = {
            "code": 200,
            "message": "Get user ok",
            "data": {}
        }

        parser = reqparse.RequestParser()
        parser.add_argument('id', type=str, help="user id")

        parsedArgs = parser.parse_args()
        log.debug("parsedArgs=%s", parsedArgs)

        if not parsedArgs:
            return genRespFailDict(BadRequest.code, "Fail to parse input parameters")

        userIdStr = parsedArgs["id"]
        if not userIdStr:
            return genRespFailDict(BadRequest.code, "Fail to get user for empty user id")

        userId = int(userIdStr)

        existedUserSql = "SELECT * FROM `user` WHERE id = %d" % userId
        log.debug("existedUserSql=%s", existedUserSql)
        existedUserOk, resultDict = sqlConn.executeSql(existedUserSql)
        log.debug("%s -> %s, %s", existedUserSql, existedUserOk, resultDict)
        if (not existedUserOk) or (not resultDict["data"]):
            return genRespFailDict(NotFound.code, "Can not found user from id %s" % userId)

        existedUser = resultDict["data"][0]
        log.debug("existedUser=%s", existedUser)

        # update lastActiveTime
        lastActiveTime = getCurUtcTime()
        self.updateUserTime(curSqlConn=curReqSqlConn, userId=userId, lastActiveTime=lastActiveTime)

        respDict["data"] = existedUser
        return jsonify(respDict)
Exemplo n.º 8
0
def create_app(config_object, init_extensions=True):
    # global log
    log.debug("Flask create_app: config_object=%s, init_extensions=%s",
              config_object, init_extensions)

    app = Flask(config_object.FLASK_APP_NAME,
                static_folder=settings.FILE_STATIC_FOLDER)
    log.info("flask app init complete: app=%s", app)
    CORS(app)
    log.debug("flask CORS app init complete")

    log.debug("before init log: app.logger=%s",
              app.logger)  # <Logger flask.app (DEBUG)>
    app.logger = log
    log.debug("after  init log: app.logger=%s",
              app.logger)  # <Logger RobotQA (DEBUG)>

    app.config.from_object(config_object)
    log.info("flask app config from_object complete")
    with app.app_context():
        g.app = app

        log.debug("after load from object: app.config=%s", app.config)
        log.debug('app.config["DEBUG"]=%s, app.config["MONGODB_HOST"]=%s',
                  app.config["DEBUG"], app.config["MONGODB_HOST"])

        if init_extensions:
            register_extensions(app)
            log.info("flask app extensions init completed")

    return app
Exemplo n.º 9
0
    def post(self):
        log.info("UserAPI POST")

        respDict = {
            "code": 200,
            "message": "Create user ok",
            "data": {}
        }

        # respUserDict = {
        #     "id": "",
        #
        #     "createTime": "",
        #     "updatedTime": "",
        #     "lastActiveTime": "",
        #
        #     "phone": "",
        #     "name": "",
        # }

        reqParaJson = request.get_json()
        log.debug("reqParaJson=%s", reqParaJson)

        if not reqParaJson:
            return genRespFailDict(BadRequest.code, "Invalid input parameters")

        keyList = []
        valueList = []

        if "phone" in reqParaJson:
            phone = reqParaJson["phone"]

            if phone:
                # check existed or not
                foundByPhoneSql = "SELECT * FROM `user` WHERE phone = '%s'" % phone
                existedUserOk, resultDict = sqlConn.executeSql(foundByPhoneSql)
                log.debug("%s -> %s, %s", foundByPhoneSql, existedUserOk, resultDict)
                if existedUserOk and resultDict["data"]:
                    # found existed user
                    return genRespFailDict(BadRequest.code, "Fail to create user for duplicated phone %s" % phone)

            keyList.append("`phone`")
            valueList.append("'%s'" % phone)

        if "name" in reqParaJson:
            name = reqParaJson["name"]

            keyList.append("`name`")
            valueList.append("'%s'" % name)

        curTime = getCurUtcTime()
        log.debug("curTime=%s", curTime)

        keyList.append("`createdTime`")
        valueList.append("'%s'" % curTime)
        keyList.append("`updatedTime`")
        valueList.append("'%s'" % curTime)
        keyList.append("`lastActiveTime`")
        valueList.append("'%s'" % curTime)

        keyListStr = ",".join(keyList) # '`phone`,`name`,`createdTime`,`updatedTime`,`lastActiveTime`'
        valueListStr = ",".join(valueList) # '\\'13800001111\\',\\'crifan\\',\\'2019-01-22 11:14:29.246945\\',\\'2019-01-22 11:14:29.246945\\',\\'2019-01-22 11:14:29.246945\\''

        # create user
        createUserSql = "INSERT INTO `user` (%s) VALUES(%s)" % (keyListStr, valueListStr)
        createUserOk, resultDict = sqlConn.executeSql(createUserSql)
        log.debug("%s -> %s, %s", createUserSql, createUserOk, resultDict)

        if createUserOk:
            newUserId = mysqlGetLastInsertId(sqlConn)

            return self.respFullUserInfo(respDict, newUserId)
        else:
            return genRespFailDict(InternalServerError.code, "Fail to create new user")
Exemplo n.º 10
0
    def put(self):
        log.info("UserAPI PUT")

        respDict = {
            "code": 200,
            "message": "Update user ok",
            "data": {}
        }

        reqParaJson = request.get_json()
        log.debug("reqParaJson=%s", reqParaJson)

        if not reqParaJson:
            return genRespFailDict(BadRequest.code, "Invalid input parameters")

        if ("id" not in reqParaJson) or (not reqParaJson["id"]):
            return genRespFailDict(BadRequest.code, "Invalid user id for empty")

        userIdStr = reqParaJson["id"]
        userId = int(userIdStr)

        existedUserSql = "SELECT * FROM `user` WHERE id = %d" % userId
        log.debug("existedUserSql=%s", existedUserSql)
        existedUserOk, resultDict = sqlConn.executeSql(existedUserSql)
        log.debug("%s -> %s, %s", existedUserSql, existedUserOk, resultDict)
        if (not existedUserOk) or (not resultDict["data"]):
            return genRespFailDict(NotFound.code, "Can not found user from id %s" % userId)

        # existedUser = resultDict["data"][0]
        # log.debug("existedUser=%s", existedUser)

        keyValueDictList = []

        if "phone" in reqParaJson:
            inputPhone = reqParaJson["phone"]

            if not inputPhone:
                return genRespFailDict(BadRequest.code, "Empty phone number")

            # duplicated bind phone notice
            existedPhoneUserSql = "SELECT * FROM `user` WHERE `phone` = '%s'" % inputPhone
            existedPhoneUserOk, resultDict = sqlConn.executeSql(existedPhoneUserSql)
            log.debug("%s -> %s, %s", existedUserSql, existedPhoneUserOk, resultDict)
            if existedPhoneUserOk and resultDict["data"]:
                existedPhoneUser = resultDict["data"][0]
                log.debug("existedPhoneUser=%s", existedPhoneUser)
                if existedPhoneUser:
                    if existedPhoneUser["id"] != userId:
                        return genRespFailDict(BadRequest.code, "Your phone %d has bind by others" % inputPhone)

            phoneKeyValueDict = {
                "key": "phone",
                "format": "'%s'",
                "value": inputPhone
            }
            keyValueDictList.append(phoneKeyValueDict)

        if "name" in reqParaJson:
            inputName = reqParaJson["name"]
            if not inputName:
                return genRespFailDict(BadRequest.code, "Empty name")

            nameKeyValueDict = {
                "key": "name",
                "format": "'%s'",
                "value": inputName
            }
            keyValueDictList.append(nameKeyValueDict)

        # generate set part sql
        keyValueStrList = []
        for eachKeyValueDict in keyValueDictList:
            eachKeyValueFormat = """`%s`=%s""" % (eachKeyValueDict["key"], eachKeyValueDict["format"]) # '`age`=%d'
            eachKeyValueStr = eachKeyValueFormat % eachKeyValueDict["value"] # '`age`=4'
            keyValueStrList.append(eachKeyValueStr)

        keyValueSql = ",".join(keyValueStrList)
        log.debug("keyValueSql=%s", keyValueSql)

        updateUserSql = "UPDATE `user` SET %s WHERE `id`=%d" % (keyValueSql, userId)
        updateUserOk, resultDict = sqlConn.executeSql(updateUserSql)
        log.debug("%s -> %s, %s", updateUserSql, updateUserOk, resultDict)
        if updateUserOk:
            resultData = resultDict["data"]
            log.debug("resultData=%s", resultData)

            # update lastActiveTime, updatedTime
            curTime = getCurUtcTime()
            log.debug("curTime=%s", curTime)
            self.updateUserTime(
                userId=userId,
                updatedTime=curTime,
                lastActiveTime=curTime)

            return self.respFullUserInfo(respDict, userId)
        else:
            return genRespFailDict(InternalServerError.code, "Update user info failed")
Exemplo n.º 11
0
def close_mysql_connection(curException):
    log.debug("close_mysql_connection: curException=%s", curException)
    log.debug("request.curReqSqlConn=%s", request.curReqSqlConn)
    if request.curReqSqlConn:
        request.curReqSqlConn.close()
        request.curReqSqlConn = None
Exemplo n.º 12
0

################################################################################
# Global Function
################################################################################


################################################################################
# Global Init App
################################################################################

log.info("before create flask app: settings=%s", settings)
app = create_app(settings)
app.app_context().push()
log.info("create flask app complete: app=%s", app)
log.debug("app=%s", app)
log.debug("settings.FLASK_ENV=%s, settings.DEBUG=%s, settings.MONGODB_HOST=%s", settings.FLASK_ENV, settings.DEBUG, settings.MONGODB_HOST)


@app.before_request
def new_mysql_connection():
    mysqlConfigDict = {
        'host': settings.MYSQL_HOST,
        'port': settings.MYSQL_PORT,
        'user': settings.MYSQL_USER,
        'password': settings.MYSQL_PASSWORD,
        'db': settings.MYSQL_DB,
        'charset': settings.MYSQL_CHARSET,
    }
    curReqSqlConn = getMysqlConnection(mysqlConfigDict)
    log.debug("new_mysql_connection: curReqSqlConn=%s", curReqSqlConn)
Exemplo n.º 13
0
    def get(self):
        log.info("CarAPI GET")

        respDict = {"code": 200, "message": "Get car ok", "data": {}}

        parser = reqparse.RequestParser()
        # parameters for get single car info
        parser.add_argument('id', type=str, help="str: car id")
        # parameters for get car list
        parser.add_argument('pageNumber',
                            type=int,
                            default=1,
                            help="page number for get car list")
        parser.add_argument('pageSize',
                            type=int,
                            default=settings.CAR_PAGE_SIZE,
                            help="page size for get car list")
        parser.add_argument('searchText',
                            type=str,
                            help="search text for get car list")

        parsedArgs = parser.parse_args()
        log.debug("parsedArgs=%s", parsedArgs)

        if not parsedArgs:
            return genRespFailDict(BadRequest,
                                   "Fail to parse input parameters")

        carId = parsedArgs["id"]

        if carId:
            findParam = None
            if carId:
                carIdObj = ObjectId(carId)
                findParam = {'_id': carIdObj}

            return respFindOneCar(findParam, respDict)
        else:
            # get car list
            pageNumber = parsedArgs["pageNumber"]
            pageSize = parsedArgs["pageSize"]
            if pageNumber < 1:
                return genRespFailDict(BadRequest.code,
                                       "Invalid pageNumber %d" % pageNumber)

            findParam = {}

            searchText = parsedArgs["searchText"]
            if searchText:
                """
                {
                  "_id": "5c779841bfaa442ee1b14f8f",
                  "url": "https://car.autohome.com.cn/pic/series-s24892/403.html#pvareaid=2042220",
                  "brand": "雷克萨斯",
                  "subBrand": "雷克萨斯",
                  "model": "2016款 200 Midnight特别限量版",
                  "series": "雷克萨斯ES"
                }
                """
                searchTextOrParamList = [
                    {
                        "url": {
                            "$regex": searchText,
                            "$options": "im"
                        }
                    },
                    {
                        "brand": {
                            "$regex": searchText,
                            "$options": "im"
                        }
                    },
                    {
                        "subBrand": {
                            "$regex": searchText,
                            "$options": "im"
                        }
                    },
                    {
                        "model": {
                            "$regex": searchText,
                            "$options": "im"
                        }
                    },
                    {
                        "series": {
                            "$regex": searchText,
                            "$options": "im"
                        }
                    },
                ]

                searchTextAndParamList = [{"$or": searchTextOrParamList}]
                if "$and" in findParam:
                    findParam["$and"].extend(searchTextAndParamList)
                else:
                    findParam["$and"] = searchTextAndParamList

            sortBy = "url"
            log.debug("findParam=%s", findParam)
            sortedCarsCursor = collectionCar.find(findParam).sort(
                sortBy, pymongo.ASCENDING)
            totalCount = sortedCarsCursor.count()
            log.debug("search car: %s -> totalCount=%s", findParam, totalCount)
            if totalCount == 0:
                respData = {}
            else:
                # Note: for debug
                # follow will cause error: pymongo.errors.InvalidOperation cannot set options after executing query
                # foundAllCars = list(sortedCarsCursor)
                # log.debug("foundAllCars=%s", foundAllCars)

                totalPageNum = int(totalCount / pageSize)
                if (totalCount % pageSize) > 0:
                    totalPageNum += 1
                if pageNumber > totalPageNum:
                    return genRespFailDict(
                        BadRequest.code,
                        "Current page number %d exceed max page number %d" %
                        (pageNumber, totalPageNum))

                skipNumber = pageSize * (pageNumber - 1)
                limitedCarsCursor = sortedCarsCursor.skip(skipNumber).limit(
                    pageSize)
                carList = list(limitedCarsCursor)
                removeObjIdList = []
                for eachCar in carList:
                    eachCar = filterCarDict(eachCar)
                    removeObjIdList.append(eachCar)

                hasPrev = False
                if pageNumber > 1:
                    hasPrev = True
                hasNext = False
                if pageNumber < totalPageNum:
                    hasNext = True

                respData = {
                    "carList": removeObjIdList,
                    "curPageNum": pageNumber,
                    "numPerPage": pageSize,
                    "totalNum": totalCount,
                    "totalPageNum": totalPageNum,
                    "hasPrev": hasPrev,
                    "hasNext": hasNext,
                }

            respDict["data"] = respData
            return jsonify(respDict)