def find_list(followType, userId, lastTime=None, count=10, is_user=True, skipNum = 0): classHelper = ClassHelper(FollowHelper.classame) query = [] matchDict = {} matchEffect = {} backDict = {} infoDict = {} noUserDict = {} if followType == "Followee": # 关注 # == & is matchDict = {"$match": {"user": userId}} matchEffect = {"$match": {'effect': {'$gte': 1}}} backDict = {"$lookup": { "from": "Followee", "localField": "followee", "foreignField": "user", "as": "status" }} infoDict = {"$lookup": { "from": "UserInfo", "localField": "followee", "foreignField": "user", "as": "UserInfo" }} noUserDict = {"$lookup": { "from": "BackupUser", "localField": "backupFollowee", "foreignField": "_sid", "as": "backupInfo" }} elif followType == "Follower": # 粉丝 if is_user: fieldname = "followee" else: fieldname = "backupFollowee" matchDict = {"$match": {fieldname: userId}} matchEffect = {"$match": {'effect': {'$gte': 1}}} backDict = {"$lookup": { "from": "Followee", "localField": "user", "foreignField": "followee", "as": "status" }} infoDict = {"$lookup": { "from": "UserInfo", "localField": "user", "foreignField": "user", "as": "UserInfo" }} else: log.err( "param error,%s",ERR_PARA.message ) return None sortDict = {"$sort": {"updateAt": -1}} # sort is fixed to -1 startidDict = {} if lastTime: startidDict = {"$match": {'updateAt':{"$lt": isodate(lastTime)}}} skipDict = {"$skip": skipNum} if followType == "Followee": # == & is limit = min( count, 100 ) elif followType == "Follower": limit = min( count, 20 ) limitDict = {"$limit": limit} displayDict = {'$project': { 'status': 1, 'backupInfo.nickName': 1, 'backupInfo._id': 1,'backupInfo.rect': 1,'backupInfo.editRect': 1,'backupInfo.editAvatar': 1, 'backupInfo.avatar': 1, 'UserInfo.nickName': 1, 'UserInfo.user': 1, 'UserInfo._id': 1, 'UserInfo.avatar': 1, 'user': 1, 'followee': 1, 'backupFollowee': 1, "updateAt": 1,"effect": 1 } } try: query.append( matchDict ) query.append( matchEffect ) query.append( sortDict ) if startidDict != {}: query.append( startidDict ) query.append( limitDict ) query.append( backDict ) query.append( infoDict ) query.append( skipDict ) if noUserDict: query.append( noUserDict ) query.append( displayDict ) followList = classHelper.aggregate( query ) if followList is not None: for num in range( len( followList ) ): followList[num].pop( 'status', None ) try: if followList[num]["UserInfo"]: followList[num]["UserInfo"] = followList[num]["UserInfo"][0] followList[num]["UserInfo"].pop( "acl", None ) followList[num]["UserInfo"].pop( "createAt", None ) #userInput = followList[num]["UserInfo"]["user"] #followList[num].update( classHelper.getIFCCount( userId=userInput ) ) else: followList[num]["UserInfo"] = {} if "backupInfo" in followList[num] and followList[num]["backupInfo"]: followList[num]["backupInfo"] = followList[num]["backupInfo"][0] followList[num]["backupInfo"].pop( "acl", None ) followList[num]["backupInfo"].pop( "createAt", None ) #userInput = followList[num]["backupInfo"]["_id"] #followList[num].update( classHelper.getIFCCount( backupUser=userInput ) ) else: followList[num]["backupInfo"] = {} except Exception, ex: log.err( "FollowerHandler Err: %s", ex ) return None else:
def get(self, action): if action == "albumUser": #弃用,用下面的那个 classHelper = ClassHelper("Face") result = classHelper.find({ "$and": [{ 'createAt': { '$gt': '2017-09-20 00:00:00' } }, { 'createAt': { '$lt': '2017-10-01 00:00:00' } }] }) print "++++++++++++++++++++++++" for res in result: print 'test', res break elif action == "albumUserTwo": classHelper = ClassHelper('Media') items = classHelper.aggregate([ { # '$match': {'uploader': "59dc3decca7143413c03a62f", 'publish': False} '$match': { 'uploader': self.user['_id'], 'publish': False } }, { '$project': { 'faces': 1 } }, { '$unwind': "$faces" }, { "$lookup": { "from": "Face", "localField": "faces", "foreignField": "_sid", "as": "faceInfo" } }, { '$unwind': "$faceInfo" }, { "$project": { "faces": 1, "possible": "$faceInfo.possible", 'media': "$faceInfo.media" } }, { "$unwind": "$possible" }, { '$group': { "_id": { 'userId': "$possible.user", "backupUserId": "$possible.backupUser" }, "media": { '$first': "$media" }, "count": { '$sum': 1 } } }, { "$sort": { "count": -1 } }, { '$lookup': { "from": "UserInfo", "localField": "_id.userId", "foreignField": "user", "as": "userInfo" } }, { '$lookup': { "from": "BackupUser", "localField": "_id.backupUserId", "foreignField": "_sid", "as": "backupUserInfo" } } ]) userInfo = [] for obj in items: try: if obj['userInfo']: userInfo.append({ 'nickName': obj['userInfo'][0].get('nickName', None), 'userId': obj["_id"]['userId'], 'count': obj['count'] }) else: userInfo.append({ 'nickName': obj['backupUserInfo'][0].get('nickName', None), 'backupUserId': obj["_id"]['backupUserId'], 'count': obj['count'] }) except Exception, e: log.err( "MyHandler-->get user(backup) info err for userId:%s", obj['_id']) data = copy.deepcopy(ERR_SUCCESS) data.message['data'] = userInfo self.write(json.dumps(data.message, cls=MeEncoder))
def get(self, className, objectId=None): # if className == "User": # self.write(ERR_CLASS_PERMISSION.message) # return if objectId: try: ObjectId(objectId) except Exception: self.write(ERR_OBJECTID_MIS.message) return obj = MeObject(className) if not obj.get(objectId): self.write(ERR_OBJECTID_MIS.message) else: mo = obj.get(objectId) # self.filter_field(mo) self.write(json.dumps(mo, cls=MeEncoder)) else: classHelper = ClassHelper(className) query = {} objs = None if self.request.arguments.has_key('aggregate'): query = eval(self.get_argument('aggregate')) objs = classHelper.aggregate(query) else: if self.request.arguments.has_key('where'): query = eval(self.get_argument('where')) try: if query.has_key('_id'): ObjectId(query['_id']) if query.has_key('$or'): for item in query['$or']: if "_id" in item: item["_id"] = ObjectId(item["_id"]) except Exception: self.write(ERR_OBJECTID_MIS.message) return if self.request.arguments.has_key('keys'): keys = eval(self.get_argument('keys')) else: keys = None try: sort = json.loads(self.get_argument('sort', '{}')) idSort = sort.get('_id', -1) sort = sort or None except Exception, e: self.write(ERR_INVALID.message) print e return skip = 0 limit = 20 try: if self.request.arguments.has_key('skip'): skip = int(self.get_argument('skip')) if self.request.arguments.has_key('limit'): limit = int(self.get_argument('limit')) except Exception: self.write(ERR_INVALID.message) return if limit > 100: self.write(ERR_INVALID.message) return objs = classHelper.find(query, keys, sort, limit, skip) objects = [] for obj in objs: mo = MeObject(className, obj, False) mo.overLoadGet = False acl = MeACL(mo['acl']) # if not acl.readAccess(self.user): # continue # self.filter_field(mo) objects.append(mo) self.write(json.dumps(objects, cls=MeEncoder))
def get(self, className, objectId=None): admin = False if self.get_current_user() in BaseConfig.adminUser: admin = True if not admin: if className in BaseConfig.accessNoClass: self.write(ERR_CLASS_PERMISSION.message) return if className not in BaseConfig.projectClass: #不存在的class self.write(ERR_PATH_PERMISSION.message) return verify = self.verify_cookie(className) if not verify: self.write(ERR_UNAUTHORIZED.message) return if objectId: try: ObjectId(objectId) except Exception: self.write(ERR_OBJECTID_MIS.message) return obj = MeObject(className) if not obj.get(objectId): self.write(ERR_OBJECTID_MIS.message) else: mo = obj.get(objectId) self.filter_field(mo) self.write(json.dumps(mo, cls=MeEncoder)) else: classHelper = ClassHelper(className) query = {} objs = None if self.request.arguments.has_key('aggregate'): query = eval(self.get_argument('aggregate')) objs = classHelper.aggregate(query) else: if self.request.arguments.has_key('where'): query = eval(self.get_argument('where')) try: if query.has_key('_id'): ObjectId(query['_id']) if query.has_key('$or'): for item in query['$or']: if "_id" in item: item["_id"] = ObjectId(item["_id"]) except Exception: self.write(ERR_OBJECTID_MIS.message) return if self.request.arguments.has_key('keys'): keys = eval(self.get_argument('keys')) else: keys = None try: sort = json.loads(self.get_argument('sort', '{}')) idSort = sort.get('_id', -1) sort = sort or None except Exception, e: self.write(ERR_INVALID.message) print e return try: if self.request.arguments.has_key('startId'): startId = self.get_argument('startId') if idSort == -1: query["_id"] = {"$lt": ObjectId(startId)} elif idSort == 1: query["_id"] = {"$gt": ObjectId(startId)} if self.request.arguments.has_key('limit'): limit = int(self.get_argument('limit')) else: limit = 20 except Exception: self.write(ERR_INVALID.message) return skip = 0 try: if self.request.arguments.has_key('skip'): skip = int(self.get_argument('skip')) except Exception: self.write(ERR_INVALID.message) return if limit > 100: self.write(ERR_INVALID.message) return objs = classHelper.find(query, keys, sort, limit, skip) objects = [] for obj in objs: mo = MeObject(className, obj, False) mo.overLoadGet = False if self.get_current_user() and not admin: acl = MeACL(mo['acl']) if not acl.readAccess(self.user): continue self.filter_field(mo) objects.append(mo) self.write(json.dumps(objects, cls=MeEncoder))