synthea_webservice/webservice/apps/api/views.py

97 lines
2.8 KiB
Python

from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import FileResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import schema
from rest_framework import status
from django.urls import reverse
from lib.utils.general import get_ip_address
from apps.synthea.api.serializers import SyntheaSerializer
from apps.synthea.lib.utils import available_states, available_modules,run_synthea
import mimetypes
@schema(None)
class Info(APIView):
"""
Show some API information. Also this can be used to check if the Hawk credentials are working.
Make sure your request does contain the header 'Content-Type': 'application/json'
"""
def get(self, request, format=None):
"""
Default API get action will return the following information in a dict:
- Connected user
- Used authentication scheme
- The remote IP of the connection
- The used content type
- The full url to the API documentation (OpenAPI)
- If a super token is used
"""
data = {
'type' : 'anonymous',
'auth' : 'none',
'remote_ip' : get_ip_address(request),
'content_type' : request.content_type,
'openapi' : request.build_absolute_uri(reverse('api:schema-redoc')),
}
if request.user.is_authenticated:
data['user'] = request.user.username
data['type'] = 'authenticated'
data['auth'] = str(request.successful_authenticator)
if request.user.token.is_supertoken:
data['type'] = 'supertoken'
else:
try:
assert request.user.researcher
data['type'] = 'researcher'
except AttributeError:
pass
return Response(data)
#@schema(None)
class States(APIView):
def get(self, request, format=None):
return Response(available_states())
class Modules(APIView):
def get(self, request, format=None):
return Response(available_modules())
class Generate(APIView):
def post(self, request, format=None):
print('Post data')
print(request.data)
serializer = SyntheaSerializer(data=request.data)
if serializer.is_valid():
synthea = serializer.save()
zipfile = synthea.generate()
print(zipfile)
response = FileResponse(zipfile.open('rb'), content_type='application/zip')
response['Content-Disposition'] = f'attachment; filename={zipfile.name}'
return response
#return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)