def preSave(doc, usr): response = {} modelClass = getattr(models, doc['_c']) # collNam = modelClass.meta['collection'] # collNamTmp = collNam + '_tmp' # collTmp = db[collNamTmp] # coll = db[collNam] # validate errors = validate_partial(modelClass, doc) if errors: response['errors'] = errors['errors'] response['total_errors'] = errors['count'] return {'response': response, 'status': 400} # init model instance model = modelClass(**doc) # if there is a vNam class property if hasattr(model, 'vNam') and 'dNam' in model._fields: model.dNam = model.vNam if hasattr(model, 'vNamS') and 'dNamS' in model._fields: model.dNamS = model.vNamS doc = doc_remove_empty_keys(to_python(model, allow_none=True)) # logit update doc = logit(usr, doc) response['doc'] = doc return {'response': response, 'status': 200}
def put(self, **kwargs): """Update a doc""" db = self.db # TODO: accomodate where clause to put changes to more than one doc. usrOID = self.usr['OID'] data = kwargs['data'] _c = data['_c'] modelClass = getattr(models, _c) #attrNam = doc['attrNam'] if 'attrNam' in doc_keys else None #attr_c = doc['attr_c'] if attrNam else None #attrEid = doc['attrEid'] if attrNam else None #attrVal = doc['attrVal'] if attrNam else None collNam = modelClass.meta['collection'] collNamTmp = collNam + '_tmp' collTmp = db[collNamTmp] coll = db[collNam] response = {} docs = [] status = 200 where = data['where'] patch = data['patch'] eId = data['eId'] if 'eId' in data else None if eId and len(patch.keys()) > 1: # TODO Handle error pass # if element eId was passed, expect to put/patch change to one element in a ListType attribute/field if eId and len(patch) == 1: elem = patch.popitem() attrNam = elem[0] # enhance to support putting/updating multiple list elements attrVal = elem[1][0] resp = preSave(attrVal, self.usr) if not resp['status'] == 200: return {'response': resp, 'status': 400} attrVal = resp['response']['doc'] # http://docs.mongodb.org/manual/applications/update/ # patch update in tmp collection attrEl = attrNam + '.$' doc = collTmp.find_and_modify( query = where, update = { "$set": { attrEl: attrVal }}, new = True ) response['collection'] = collNamTmp response['total_invalid'] = 0 response['id'] = id.__str__() # remove this, not needed response['doc'] = doc return {'response': response, 'status': 200} else: # validate patch # init modelClass for this doc patch_errors = validate_partial(modelClass, patch) if patch_errors: response['errors'] = patch_errors['errors'] response['total_errors'] = patch_errors['count'] status = 400 return prep_response(response, status = status) # logit update patch = logit(self.usr, patch) # patch update in tmp collection doc = collTmp.find_and_modify( query = where, update = {"$set": patch}, new = True ) # init model instance model = modelClass(**doc) # if there is a vNam class property if hasattr(model, 'vNam') and 'dNam' in model._fields: doc = collTmp.find_one(where) _id = doc['_id'] model.dNam = model.vNam if hasattr(model, 'vNamS') and 'dNamS' in model._fields: model.dNamS = model.vNamS doc = doc_remove_empty_keys(to_python(model, allow_none=True)) collTmp.update(where, doc) # gotta put the _id back doc['_id'] = _id response['collection'] = collNamTmp response['total_invalid'] = 0 response['id'] = id.__str__() # remove this, not needed response['doc'] = doc return {'response': response, 'status': status}