Core: add jwt auth

This commit is contained in:
Omma 2024-08-21 16:38:37 +03:30
parent 1e0ffd9b2d
commit 495312b060
12 changed files with 234 additions and 17 deletions

View file

@ -7,7 +7,9 @@ from django.core import serializers
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
import json
import requests
@ -15,12 +17,46 @@ from .models import Well
from .serializers import WellSerializer
#To optain the jwt Token for the first time from other services
'''
import requests
url = 'http://<well_service_url>/token/'
credentials = {
"username": "your_username",
"password": "your_password"
}
response = requests.post(url, data=credentials)
token = response.json().get('access')
'''
# Store this token to use in future requests
'''
headers = {
'Authorization': f'Bearer {token}',
}
response = requests.post('http://<well_service_url>/<...>/', headers=headers, json=data_to_send)
'''
#error_list
'''
{
"200_created": ({"data": "<serializer.data>", "message": "<costume message>"}, status=status.HTTP_200_OK),
"200_updated": ({"data": "<serializer.data>", "message": f"<Successfully updated {instance.license_code}>"}, status=status.HTTP_200_OK),
"400":({"message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST),
"404": ({"message": "<Not Found>"}, status=status.HTTP_404_NOT_FOUND),
"409": ({"message":f'<{data["license_code"]} already exists>'}, status=status.HTTP_409_CONFLICT),
"500": ({"message": "<Internal Server Error>"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR),
}
'''
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def create_well(request):
try:
data = json.loads(request.body.decode('utf-8'))
instance = Well.objects.get(
license_code=data["license_code"])
instance = Well.objects.filter(
license_code=data["license_code"], log_type = 0)
if instance:
return Response(
{"message":f'{data["license_code"]} already exists'},
@ -29,33 +65,38 @@ def create_well(request):
serializer = WellSerializer(data=data)
if serializer.is_valid():
serializer.save()
dd
return Response(
serializer.data,
{"data":serializer.data},
status=status.HTTP_201_CREATED
)
else:
return Response(
serializer.errors,
{"message":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)
return Response({"message": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['GET'])
@permission_classes([IsAuthenticated])
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)
return Response({"message": "Not Found"}, status=status.HTTP_404_NOT_FOUND)
serializer = WellSerializer(well)
return Response(serializer.data, status=status.HTTP_200_OK)
return Response({"data":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)
return Response({"message": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def send_wells(request):
#permission_classes = [IsAuthenticated]
try:
# to do: add owner
# to do: add filter
@ -63,24 +104,28 @@ def send_wells(request):
# to do: add no return data
wells = Well.objects.all()
serializer = WellSerializer(wells, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
return Response({"data":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)
return Response({"message": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@permission_classes([IsAuthenticated])
@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)
return Response({"message": f"Successfully removed 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)
return Response({"message": "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)
return Response({"message": "Internal Server Error"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@permission_classes([IsAuthenticated])
@api_view(['PUT'])
def update_well_by_well_id(request, obj_id):
try:
@ -94,13 +139,15 @@ def update_well_by_well_id(request, obj_id):
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response({"message":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)
@permission_classes([IsAuthenticated])
@api_view(['PUT'])
def edit_well_by_well_id(request, obj_id):
try: