def put(self, className, objectId=None): try: if not objectId and not self.request.arguments.has_key('where'): self.write(ERR_OBJECTID_MIS.message) return try: if objectId: try: ObjectId(objectId) except Exception: self.write(ERR_OBJECTID_MIS.message) return obj = json.loads(self.request.body) except Exception, e: log.err("JSON Error:[%d/%s] , error:%s", len(self.request.body), self.request.body, str(e)) self.write(ERR_INVALID.message) return if self.request.arguments.has_key('where'): classHelper = ClassHelper(className) query = eval(self.get_argument('where')) if "$set" in obj: obj['$set'] = self.check_field(className, obj['$set']) else: obj = self.check_field(className, obj) if not obj: return returnObj = classHelper.updateOneCreate(query, obj) data = copy.deepcopy(ERR_SUCCESS) data.message['data'] = self.filter_field(returnObj) self.write(json.dumps(data.message, cls=MeEncoder)) elif type(obj) == list: objectIdError = False for index in range(len(obj) - 1): try: for key, value in obj[index].items(): if value.has_key('_sid'): ObjectId(value['_sid']) value = self.sentiveCheck(key, value) meobj = MeObject(key, value) meobj.save() except bson.errors.InvalidId: objectIdError = True break except Exception, e: log.err("Error:%s , error:%s", self.request.body, str(e)) if objectIdError: self.write(ERR_OBJECTID_MIS.message) return mainObj = obj[len(obj) - 1] mainObj = self.sentiveCheck(className, mainObj) classHelper = ClassHelper(className) # 只返回了更新时间 try: data = classHelper.update(objectId, mainObj) # 默认返回整个对象 self.write(json.dumps(data, cls=MeEncoder)) except Exception, e: log.err("ClassV2Handler-->put error, %s", e) self.write(ERR_DB_OPERATION.message)
def post(self, className): try: try: obj = json.loads(self.request.body) obj = self.check_field(className, obj) if not obj: return except Exception, e: log.err("JSON Error:%s , error:%s", self.request.body, str(e)) self.write(ERR_INVALID.message) return print 'className:' + className if type(obj) == list: objectIdError = False for index in range(len(obj) - 1): try: for key, value in obj[index].items(): if value.has_key('_sid'): ObjectId(value['_sid']) value = self.sentiveCheck(key, value) value = self.blacklistCheck(className, value) if value: meobj = MeObject(key, value) meobj.save() else: self.write(ERR_BLACK_PERMISSION.message) except bson.errors.InvalidId: objectIdError = True break except Exception, e: log.err("Error:%s , error:%s", self.request.body, str(e)) if objectIdError: self.write(ERR_OBJECTID_MIS.message) return mainObj = obj[len(obj) - 1] try: if mainObj.has_key('_sid'): ObjectId(mainObj['_sid']) mainObj = self.sentiveCheck(className, mainObj) mainObj = self.blacklistCheck(className, mainObj) if mainObj: meobj = MeObject(className, mainObj) meobj.save() self.write(json.dumps(meobj, cls=MeEncoder)) else: self.write(ERR_BLACK_PERMISSION.message) except bson.errors.InvalidId: self.write(ERR_OBJECTID_MIS.message)
mainObj = self.blacklistCheck(className, mainObj) if mainObj: meobj = MeObject(className, mainObj) meobj.save() self.write(json.dumps(meobj, cls=MeEncoder)) else: self.write(ERR_BLACK_PERMISSION.message) except bson.errors.InvalidId: self.write(ERR_OBJECTID_MIS.message) else: try: obj = self.sentiveCheck(className, obj) obj = self.blacklistCheck(className, obj) if obj: meobj = MeObject(className, obj) meobj.save() self.filter_field(meobj) self.write(json.dumps(meobj, cls=MeEncoder)) else: self.write(ERR_BLACK_PERMISSION.message) except Exception,e: log.err(json.dumps(e.message)) if e.message["errCode"] == 137: field = re.search(r"index: ([0-9a-zA-Z]+)_-?\d{1} dup key",e.message['info']) if field: field = field.group(1) classHelper = ClassHelper(className) item = classHelper.find_one({field:obj[field]}) item = self.filter_field(item) e.message['data'] = item self.write(e.message)