def destroy(self, request, *args, **kwargs): try: res = es.get(index=data_index, doc_type="data", id=kwargs["pk"]) data = res["_source"] data.pop("S-last-modified") data["S-delete-time"] = datetime.datetime.now().isoformat() data["S-delete-people"] = request.user.username res = es.create(index=deleted_data_index, doc_type="deleted-data", id=kwargs["pk"], body=data) es.delete(index=data_index, doc_type="data", id=kwargs["pk"]) es.delete_by_query( index=record_data_index, doc_type="record-data", body={"query": { "term": { "S-data-id": kwargs["pk"] } }}) except NotFoundError as exc: raise exceptions.ParseError( "Document {} was not found in Type {} of Index {}".format( kwargs["pk"], "data", table.name)) return Response(res, status=status.HTTP_204_NO_CONTENT)
def update(self, request, *args, **kwargs): try: res = es.get(index=data_index, doc_type="data", id=kwargs["pk"]) except NotFoundError as exc: raise exceptions.NotFound("Document {} was not found in Type {} of Index {}".format(kwargs["pk"],"data", data_index)) partial = kwargs.get("partial", False) serializer = self.get_serializer(data=request.data, partial=partial) serializer.is_valid(raise_exception=True) his_data = res["_source"] data = copy.copy(his_data) is_equal = True for k, v in serializer.validated_data.items(): if k[0] == "S": continue if isinstance(serializer.fields.fields[k], serializers.DateTimeField): if isinstance(v, type([])): v = list(map(lambda x: x.isoformat(), v)) elif v != None: v = v.isoformat() data[k] = v if his_data[k] != v: is_equal = False if is_equal: raise exceptions.ParseError(detail="No field changes") his_data.pop("S-creator") his_data.pop("S-creation-time") his_data["S-data-id"] = kwargs["pk"] his_data["S-changer"] = data["S-last-modified"] his_data["S-update-time"] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") data["S-last-modified"] = request.user.username his_data.pop("S-last-modified") es.index(index=record_data_index, doc_type="record-data", id=str(uuid.uuid1()).replace("-", ""), body=his_data) res = es.index(index=data_index, doc_type="data", id=kwargs["pk"], body=data) return Response(res)
def retrieve(self, request, *args, **kwargs): try: res = es.get(index=data_index, doc_type="data", id=kwargs["pk"]) except NotFoundError as exc: raise exceptions.NotFound( "Document {} was not found in Type {} of Index {}".format( kwargs["pk"], "data", data_index)) return Response(res)