def delete(self, req_id=None): # delete a req if req_id: query = self.query_id(req_id) updobj = yield self.CVRequests.find_one(query) if updobj: # removing cvrequest and cvresult related and they will be added in # a history collection try: # get cvresult if it exists cvres = yield self.CVResults.find_one( {'cvrequest_iid': req_id}) if cvres: info(cvres) idcvres = ObjId(cvres['_id']) del cvres['_id'] info(cvres) newhres = yield self.db.cvresults_history.insert(cvres) info(newhres) cvres = yield self.CVResults.remove({'_id': idcvres}) del updobj['_id'] newhreq = yield self.db.cvrequests_history.insert(updobj) info(newhreq) cvreq = yield self.CVRequests.remove(query) info(cvreq) self.response(200, 'CV Request successfully deleted.') except Exception as e: self.response(500, 'Fail to delete cvrequest.') else: self.response(404, 'CV Request not found.') else: self.response(400, 'Remove requests (DELETE) must have a resource ID.')
def query_id(self, org_id): """This method configures the query that will find an object""" try: query = {'iid': int(org_id)} except Exception as e: try: query = {'_id': ObjId(org_id)} except Exception as e: query = {'name': org_id} return query
def query_id(self, req_id): """The method configures the query to find an object.""" query = None try: query = {'iid': int(req_id)} except Exception as e: try: query = {'_id': ObjId(req_id)} except Exception as e: query = {'name': str(req_id)} return query
def query_id(self, user_id): """This method configures the query that will find an object""" try: query = {'iid': int(user_id)} except Exception as e: try: query = {'_id': ObjId(user_id)} except Exception as e: info(e) query = {'email': user_id} return query
def query_id(self, res_id): """This method configures the query that will find an object""" try: query = {'iid': int(res_id)} except Exception as e: try: query = {'_id': ObjId(res_id)} except Exception as e: self.response(400, 'Invalid id key.') return return query
def changePassword(self, ouser, newpass, callback=None): encpass = self.encryptPassword(newpass) ouser['encrypted_password'] = encpass ouser['updated_at'] = datetime.now() updid = ObjId(ouser['_id']) del ouser['_id'] try: updobj = User(ouser) updobj.validate() # the object is valid, so try to save try: updobj = updobj.to_native() updobj['_id'] = updid saved = yield self.Users.update({'_id': updid}, updobj) info(saved) resp = [200, 'Password changed successfully.'] except Exception as e: resp = [400, 'Fail to update password.'] except ValidationError as e: resp = [400, 'Invalid input data. Errors: ' + str(e) + '.'] callback(resp)
def put(self, animal_id=None): # update an animal # parse data recept by PUT and get only fields of the object update_data = self.parseInput(Animal) fields_allowed_to_be_update = ['name', 'organization_iid', 'primary_image_set_iid', 'dead'] if 'organization_id' in self.input_data.keys(): update_data['organization_iid'] = self.input_data['organization_id'] del self.input_data['organization_id'] check_org = yield self.db.organizations.find_one( {'iid': update_data['organization_iid']}) if not check_org: self.response(400, 'Invalid organization_id.') return if 'primary_image_set_id' in self.input_data.keys(): update_data['primary_image_set_iid'] = self.input_data['primary_image_set_id'] del self.input_data['primary_image_set_id'] check_imageset = yield self.ImageSets.find_one( {'iid': update_data['primary_image_set_iid']}) if not check_imageset: self.response(400, 'Invalid primary_image_set_id.') return # validate the input for update update_ok = False for k in fields_allowed_to_be_update: if k in update_data.keys(): update_ok = True break if animal_id and update_ok: query = self.query_id(animal_id) updobj = yield self.Animals.find_one(query) primimgsetid = int(updobj['primary_image_set_iid']) if updobj: for field in fields_allowed_to_be_update: if field in update_data.keys(): updobj[field] = update_data[field] updobj['updated_at'] = datetime.now() # Check for primery change if 'primary_image_set_iid' in update_data.keys(): newimgsetid = int(update_data['primary_image_set_iid']) # Change joined images to the new primary image set resp = yield self.Images.update_many( {'$and': [{'joined': primimgsetid}, {'image_set_iid': {'$ne': newimgsetid}}]}, {'$set': {'joined': newimgsetid}}) # Removed joined if it is an image from the new primary image set resp = yield self.Images.update_many( {'$and': [{'joined': primimgsetid}, {'image_set_iid': newimgsetid}]}, {'$set': {'joined': 0}}) oldimgset = yield self.ImageSets.find_one({'iid': primimgsetid}) if oldimgset: coverid = yield self.Images.find_one( {'iid': oldimgset['main_image_iid']}) if coverid: if int(coverid['image_set_iid']) != int(oldimgset['iid']): resp = yield self.ImageSets.update( {'iid': oldimgset['iid']}, {'$set': {'main_image_iid': None}}) info(resp) try: updid = ObjId(updobj['_id']) del updobj['_id'] animals = Animal(updobj) animals.collection(self.animals) animals.validate() # the object is valid, so try to save try: updated = yield self.Animals.update( {'_id': updid}, animals.to_native()) info(updated) output = updobj output['obj_id'] = str(updid) # Change iid to id in the output self.switch_iid(output) output['organization_id'] = output['organization_iid'] del output['organization_iid'] output['primary_image_set_id'] = output['primary_image_set_iid'] del output['primary_image_set_iid'] self.finish(self.json_encode( {'status': 'success', 'message': self.animal.capitalize() + ' updated.', 'data': output})) except Exception as e: # duplicated index error self.response(409, 'Duplicated name for %s.' % (self.animal)) except ValidationError as e: # received data is invalid in some way self.response(400, 'Invalid input data. Errors: %s.' % (str(e))) else: self.response(404, self.animal.capitalize() + ' not found.') else: self.response( 400, 'Update requests (PUT) must have a Lion Id and update pairs for key and value.')
def put(self, user_id=None): # update an user # parse data recept by PUT and get only fields of the object update_data = self.parseInput(User) fields_allowed_to_be_update = [ 'email', 'organization_iid', 'admin', 'password' ] if 'organization_id' in self.input_data.keys(): orgiid = self.input_data['organization_id'] orgexists = yield self.Orgs.find_one({'iid': orgiid}) if orgexists: update_data['organization_iid'] = orgiid else: self.response(409, "Organization referenced doesn't exist.") return # validate the input for update update_ok = False for k in fields_allowed_to_be_update: if k in self.input_data.keys(): update_ok = True break if user_id and update_ok: query = self.query_id(user_id) updobj = yield self.Users.find_one(query) if updobj: for field in fields_allowed_to_be_update: if field in update_data.keys(): updobj[field] = update_data[field] if 'password' in self.input_data.keys(): updobj['encrypted_password'] = self.encryptPassword( self.input_data['password']) updobj['updated_at'] = datetime.now() updid = ObjId(updobj['_id']) del updobj['_id'] try: updobj = User(updobj) updobj.validate() # the object is valid, so try to save try: updobj = updobj.to_native() updobj['_id'] = updid saved = yield self.Users.update({'_id': updid}, updobj) info(saved) output = updobj output['obj_id'] = str(updid) del output['_id'] # Change iid to id in the output self.switch_iid(output) del output['encrypted_password'] output['organization_id'] = output['organization_iid'] del output['organization_iid'] self.finish( self.json_encode({ 'status': 'success', 'message': 'User updated.', 'data': output })) except Exception as e: # duplicated index error self.response(409, 'Invalid data for update.') except ValidationError as e: # received data is invalid in some way self.response( 400, 'Invalid input data. Errors: ' + str(e) + '.') else: self.response(404, 'User not found.') else: self.response( 400, 'Update requests (PUT) must have a resource ID and update pairs for key and value.' )