def get(self, audience_profile_name): audience_profile = TargetAudienceProfileModel.find_by_name(audience_profile_name) try : if audience_profile: return audience_profile.json() except Exception as e: return {"message": "Record not found'{}'".format(e)}, 404
def get(cls, token): try: target_audience_profile_id = decodeID(token) audience_profile = TargetAudienceProfileModel.find_by_id( target_audience_profile_id) if not audience_profile or audience_profile.token != token: return {"success": False, 'message': 'Record Not Found'}, 404 return audience_profile.json(), 200 except Exception as e: return {"success": False, "message": str(e)}
def put(self, token): try: data = self.parser.parse_args() validateObj = TargetAudienceProfileModel.validateData( data, request) if type(validateObj) is dict: return {"success": False, "errors": validateObj}, 400 target_audience_profile_id = decodeID(token) audience_profile = TargetAudienceProfileModel.find_by_id( target_audience_profile_id) if not audience_profile or audience_profile.token != token: return {"success": False, 'message': 'Record Not Found'}, 404 audience_profile.audience_profile_name = data[ 'audience_profile_name'] audience_profile.business = data['business'] audience_profile.age_group_id = data['age_group_id'] audience_profile.gender_id = data['gender_id'] audience_profile.target_languages = data['target_languages'] audience_profile.target_nationality = data['target_nationality'] audience_profile.target_education = data['target_education'] audience_profile.target_occupation = data['target_occupation'] audience_profile.target_interest = data['target_interest'] audience_profile.modified_on = datetime.now() audience_profile.modified_by = 1 name = data['audience_profile_name'] if name.strip(): audience_profile.save_to_db() else: return { "success": False, "message": "String Should not be empty" } return { "success": True, "message": "Record updated successfully." }, 200 except Exception as e: return {"success": False, "message": str(e)}
def post(self, audience_profile_name): db.create_all() db.session.commit() if TargetAudienceProfileModel.find_by_name(audience_profile_name): return {'message': "An Record with name '{}' already exists.".format(audience_profile_name)}, 400 data = self.parser.parse_args() audience_profile = TargetAudienceProfileModel(audience_profile_name,**data,) audience_profile.created_by = 1 audience_profile.created_on = datetime.now() audience_profile.modified_by = 0 audience_profile.deleted_by = 0 try: audience_profile.save_to_db() except Exception as e: return {"message": "An error occurred while inserting the Record.'{}'".format(e)} return audience_profile.json(), 201
def delete(cls, audience_profile_name): audience_profile = TargetAudienceProfileModel.find_by_name(audience_profile_name) if audience_profile: audience_profile.deleted_by =1 audience_profile.deleted_on = datetime.now() audience_profile.save_to_db() #audience_profile.delete_from_db() return {'message': 'Record deleted'} else : return {"Message":"Record Not FOUND"}
def delete(cls, token): try: target_audience_profile_id = decodeID(token) audience_profile = TargetAudienceProfileModel.find_by_id( target_audience_profile_id) if not audience_profile or audience_profile.token != token: return {"success": False, 'message': 'Record Not Found'}, 404 audience_profile.deleted_by = 1 audience_profile.deleted_on = datetime.now() audience_profile.save_to_db() #audience_profile.delete_from_db() return {"success": True, 'message': 'Record deleted.'}, 200 except Exception as e: return {"success": False, "message": str(e)}
def put(self, audience_profile_name): data = self.parser.parse_args() audience_profile=TargetAudienceProfileModel.find_by_name(audience_profile_name) if audience_profile: audience_profile.business = data['business'] audience_profile.age_group_id = data['age_group_id'] audience_profile.gender_id = data['gender_id'] audience_profile.target_languages = data['target_languages'] audience_profile.target_nationality = data['target_nationality'] audience_profile.target_education = data['target_education'] audience_profile.target_occupation = data['target_occupation'] audience_profile.target_interest = data['target_interest'] audience_profile.modified_on = datetime.now() audience_profile.modified_by = 1 else : return {"Message":"Record Not FOUND"} audience_profile.save_to_db() return audience_profile.json()
def post(self): data = self.parser.parse_args() audience_profile = TargetAudienceProfileModel(**data) audience_profile.created_by = 1 audience_profile.created_on = datetime.now() audience_profile.modified_by = 0 audience_profile.deleted_by = 0 audience_profile.modified_on = None audience_profile.deleted_on = None try: validateObj = TargetAudienceProfileModel.validateData( data, request) if type(validateObj) is dict: return {"success": False, "errors": validateObj}, 400 if TargetAudienceProfileModel.find_by_name( data['audience_profile_name']): return { "success": False, "message": "A audience_profile with that Record Name already exists" }, 400 name = data['audience_profile_name'] if name.strip(): audience_profile.save_to_db() audience_profile.token = encodeID( audience_profile.target_audience_profile_id) audience_profile.save_to_db() else: return { "success": False, "message": "String Should not be empty" } except Exception as e: return { "message": "An error occurred creating the Record.'{}'".format(e) }, 500 return audience_profile.json(), 201