WellService/proxy/views.py
2024-08-19 11:14:42 +03:30

123 lines
No EOL
4.5 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.db import IntegrityError
from django.forms.models import model_to_dict
from django.core import serializers
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
import json
import requests
from .models import Well
from .serializers import WellSerializer
@api_view(['POST'])
def create_well(request):
try:
data = json.loads(request.body.decode('utf-8'))
instance = Well.objects.get(
license_code=data["license_code"])
if instance:
return Response(
{"message":f'{data["license_code"]} already exists'},
status=status.HTTP_409_CONFLICT
)
serializer = WellSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(
serializer.data,
status=status.HTTP_201_CREATED
)
else:
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST
)
except Exception as e:
print(e)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['GET'])
def send_well_by_well_id(request, obj_id):
try:
well = Well.objects.filter(id=obj_id).first()
if well is None:
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
serializer = WellSerializer(well)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as ex:
print(ex)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['GET'])
def send_wells(request):
try:
# to do: add owner
# to do: add filter
# to do: add log_type=0
# to do: add no return data
wells = Well.objects.all()
serializer = WellSerializer(wells, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as ex:
print(ex)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['DELETE'])
def delete_well_by_well_id(request, obj_id):
try:
instance = Well.objects.get(id=obj_id, log_type=0)
instance.log_type = 1
instance.save()
return Response({"detail": f"Successfully updated log type for well {instance.license_code}"}, status=status.HTTP_200_OK)
except Well.DoesNotExist:
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e:
print(e)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['PUT'])
def update_well_by_well_id(request, obj_id):
try:
instance = Well.objects.get(id=obj_id, log_type=0)
instance.log_type = 2
instance.save()
data = json.loads(request.body.decode('utf-8'))
data["log_type"] = 0
serializer = WellSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
except Well.DoesNotExist:
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e:
print(e)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['PUT'])
def edit_well_by_well_id(request, obj_id):
try:
instance = Well.objects.get(id=obj_id, log_type=0)
instance.log_type = 3
instance.save()
data = json.loads(request.body.decode('utf-8'))
data["log_type"] = 0
serializer = WellSerializer(data=data)
if serializer.is_valid():
serializer["log_of"] = instance.id
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
except Well.DoesNotExist:
return Response({"error": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
except Exception as e:
print(e)
return Response({"error": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)