49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
|
from django.shortcuts import render
|
||
|
from django.http import HttpResponse
|
||
|
from apps.synthea.forms import SyntheaForm
|
||
|
|
||
|
from .lib.utils import run_synthea
|
||
|
|
||
|
import mimetypes
|
||
|
# Create your views here.
|
||
|
|
||
|
def index(request):
|
||
|
template_name = 'synthea/index.html'
|
||
|
return render(request,template_name,{})
|
||
|
|
||
|
def show_synthea_form(request):
|
||
|
template_name = 'synthea/generator_form.html'
|
||
|
|
||
|
# if this is a POST request we need to process the form data
|
||
|
if request.method == 'POST':
|
||
|
# create a form instance and populate it with data from the request:
|
||
|
form = SyntheaForm(request.POST)
|
||
|
# check whether it's valid:
|
||
|
if form.is_valid():
|
||
|
# process the data in form.cleaned_data as required
|
||
|
|
||
|
try:
|
||
|
zipfile = run_synthea(
|
||
|
form.cleaned_data['state'],
|
||
|
form.cleaned_data['population'],
|
||
|
form.cleaned_data['gender'],
|
||
|
form.cleaned_data['age'],
|
||
|
form.cleaned_data['module']
|
||
|
)
|
||
|
|
||
|
mime_type, _ = mimetypes.guess_type(zipfile)
|
||
|
response = HttpResponse(zipfile.open('rb'), content_type=mime_type)
|
||
|
response['Content-Disposition'] = f'attachment; filename={zipfile.name}'
|
||
|
|
||
|
return response
|
||
|
|
||
|
except Exception as ex:
|
||
|
print(ex)
|
||
|
|
||
|
# if a GET (or any other method) we'll create a blank form
|
||
|
else:
|
||
|
form = SyntheaForm()
|
||
|
|
||
|
return render(request,template_name,{
|
||
|
'form':form
|
||
|
})
|