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)
def respFindOneCar(findParam, respDict): foundCarObj = collectionCar.find_one(findParam) if not foundCarObj: return genRespFailDict(NotFound, "Can not find car from %s" % findParam) foundCarDict = filterCarDict(foundCarObj) respDict["data"] = foundCarDict return jsonify(respDict)
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)
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)
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")
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")
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)