def post(self, request): serializer = FolderSerializer(data=request.data) if serializer.is_valid(): serializer.save() folder_es_obj = CreateFolders(serializer.data) elastic_utils_obj = Elastic() elastic_utils_obj.index_data(folder_es_obj) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def call_elastic_search_to_update_tag(existing_tags): try: elastic_search_dict = {} elastic_search_metadata = elastic_search_dict["metadata"] = {} elastic_search_metadata["tags"] = existing_tags # Call to elastic_search to update index_obj = ElasticSearchCreateAsset(elastic_search_dict) elastic_obj = Elastic() res = elastic_obj.update_data(index_obj) return res except: return "Error while updating tags"
def call_elastic_search_to_create(data, asset_type): elastic_search_dict = {} elastic_search_dict["folder_id"] = data['folder_id'] elastic_search_dict["id"] = data['id'] elastic_search_dict["name"] = data['asset_name'] elastic_search_dict["asset_path"] = data['asset_path'] elastic_search_dict["thumbnail_path"] = data['thumbnail'] elastic_search_metadata = elastic_search_dict["metadata"] = {} elastic_search_metadata["file_type"] = asset_type elastic_search_metadata["status"] = "pending" elastic_search_metadata["tags"] = " " elastic_search_metadata["activate_on"] = data['activate_on'] elastic_search_metadata["expire_on"] = data['expire_on'] index_obj = ElasticSearchCreateAsset(elastic_search_dict) elastic_obj = Elastic() res = elastic_obj.index_data(index_obj) return res
def delete(self, request, format=None): # To delete perticular asset asset_id = self.request.GET.getlist('id', None) if asset_id: instance = list( CreateAsset.objects.filter(id__in=asset_id).delete()) if (instance[0]): # call_elastic_search to delete for element in asset_id: elastic_search_dict = {} elastic_search_dict["id"] = element delete_obj = ElasticSearchUpdateAsset(elastic_search_dict) elastic_obj = Elastic() res = elastic_obj.delete_data(delete_obj) return Response("200") return Response("404 Id does not exists") else: return Response("Does not contain required parameter")
def call_to_update_assets(request): asset_id = request.data['id'] activate_on = request.data['activate_on'] expire_on = request.data['expire_on'] asset_name = request.data['name'] # tag_name = request.data['tags'] # Update asset_table response_asset = update_asset_table(asset_id, activate_on, expire_on, asset_name) # return Response(response_asset) if (response_asset is 'Id not found to update' or response_asset is None): return "Id not found / Error" else: # create tag and get id # tag_id = create_tag(tag_name) # if(tag_id): # update asset_tags # asset_tag = create_asset_tag(asset_id, tag_id) # print(asset_tag) # Get current existing Tags from Elastic search search_tag = { "query": { "bool": { "must": [{ "term": { "id": { "value": asset_id } } }] } } } index_obj = ElasticSearchGetId(search_tag) elastic_obj = Elastic() res = elastic_obj.primary_search(index_obj) # call elastic search to update elastic_serach_result = call_elastic_search_to_update(response_asset) # return Response("Elastic search updated") return response_asset
def call_elastic_search_to_update(response_asset): try: elastic_search_dict = {} elastic_search_metadata = elastic_search_dict["metadata"] = {} elastic_search_dict["folder_id"] = response_asset['folder_id'] elastic_search_dict["id"] = response_asset['id'] elastic_search_dict["name"] = response_asset['asset_name'] elastic_search_dict["asset_path"] = response_asset['asset_path'] elastic_search_dict["thumbnail_path"] = response_asset['thumbnail'] elastic_search_metadata["status"] = "pending" elastic_search_metadata["activate_on"] = response_asset['activate_on'] elastic_search_metadata["expire_on"] = response_asset['expire_on'] # elastic_search_metadata["tags"] = existing_tags # Call to elastic_search to update index_obj = ElasticSearchCreateAsset(elastic_search_dict) elastic_obj = Elastic() res = elastic_obj.update_data(index_obj) return res except Exception: return "Error while updating"
def delete(self, request): keys = self.request.GET.getlist("id") for key in keys: try: UUID(key, version=4) except ValueError: return Response({"message": "invalid id"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: inst = Folders.objects.get(id=key) serializer = FolderSerializer(inst) folder_es_obj = DeleteFolders(serializer.data) except Folders.DoesNotExist: return Response({"message": "id does not exist"}, status=status.HTTP_204_NO_CONTENT) try: elastic_utils_obj = Elastic() elastic_utils_obj.delete_data(folder_es_obj) except ConnectionError: return Response( {"message": "Couldn't connect to elasticsearch"}) inst.delete() return Response(str(keys) + "/tDeleted")
def put(self, request): pk = request.data.get("id") parent = request.data.get('parent') try: UUID(pk, version=4) except ValueError: return Response({"message": "invalid id"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) if parent is not None: try: UUID(parent, version=4) except ValueError: return Response({"message": "invalid parent_id"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) inst = Folders.objects.get(id=pk) ser = FolderSerializer(inst, data=request.data) if ser.is_valid(): ser.save() folder_es_obj = CreateFolders(ser.data) elastic_utils_obj = Elastic() elastic_utils_obj.update_data(folder_es_obj) return Response(ser.data) return Response(ser.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): if "id" and "tag" in request.data: asset_id = request.data['id'] tag_name = request.data['tag'] tag_id = create_tag(tag_name) if (tag_id): asset_tag = create_asset_tag(asset_id, tag_id) # Get current existing Tags from Elastic search search_tag = { "query": { "bool": { "must": [{ "term": { "id": { "value": asset_id } } }] } } } index_obj = ElasticSearchGetId(search_tag) elastic_obj = Elastic() res = elastic_obj.primary_search(index_obj) existing_tags = res["hits"]["hits"][0]["_source"]["metadata"][ "tags"] existing_tags = existing_tags + ',' + tag_name # call elastic search to update elastic_serach_result = call_elastic_search_to_update_tag( existing_tags) # return Response("Elastic search updated") return Response("200 Tags created and search updated") else: return Response("500 Error while creating Tags") else: return Response("Provide valid parameter")
}) fobj1 = createFolders({ "id": "F01", "name": "BMW", "organisation_id": "O01" }) fobj2 = createFolders({ "id": "F02", "name": "Audi", "organisation_id": "O02" }) delete_obj_query = deleteFolders({ 'qid': {"folder_id":"F02"} }) elastic_obj = Elastic() #################################### # insert data to es elastic_obj.index_data(obj1) elastic_obj.index_data(obj2) elastic_obj.index_data(obj3) elastic_obj.index_data(obj4) elastic_obj.index_data(fobj1) elastic_obj.index_data(fobj2) ################################### # delete from es # elastic_obj.delete_by_query(delete_obj_query)