synthea_webservice/webservice/apps/synthea/models.py

73 lines
2.9 KiB
Python

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _
from django_cryptography.fields import encrypt
from lib.utils.general import get_random_string
from lib.models.base import MetaDataModel
from .lib.utils import run_synthea
import uuid
# Create your models here.
class Synthea(MetaDataModel):
"""Synthea model that holds some iformation about a generated output.
Attributes
----------
id : uuid
A unique ID for every Synthea run. Leave empty for auto generating a new value.
state : str
The state for which you want to generate Synthea patient data
population : int
The amount of patients you want to generate
gender : datetime
Generate only male (m), only female(f) or leave empty for male and female
age : str
The age range for the patients. Enter like [min_age]-[max_age]
module : str
The module to use for patient generating. Leave empty for random use by Synthea
log : str
The outcome of a single patient generating run. This can be read in the admin area.
Returns:
Synthea: A new Synthea model
"""
class Meta:
verbose_name = _('synthea')
verbose_name_plural = _('synthea')
id = models.UUIDField(_('ID'), primary_key=True, unique=True, default=uuid.uuid4, editable=False, help_text=_('A unique id'))
state = models.CharField(_('State'), max_length=200, help_text=_('The state for which synthea generate data.'))
population = models.PositiveSmallIntegerField(_('Population'), default=50, help_text=_('The size of the population'))
gender = models.CharField(_('Gender'), blank=True,max_length=1, help_text=_('Select the gender type'))
age = models.CharField(_('Age range'), blank=True,default='18-100', max_length=10, help_text=_('Select the age range. Enter [min age]-[max age]'))
module = models.CharField(_('Module'),blank=True, max_length=50, help_text=_('Select the module'))
log = models.TextField(_('Log'),blank=True, help_text=_('Synthea logfile output'))
def generate(self):
"""Run the patient generation. This will return a logfile and a zipfile location for download.
The log will be stored in the model when done. This log can then be seen/readed in the admin section of Django
Returns:
str: The zip file location on disk.
"""
# Start generating patient data.
log,zip_file = run_synthea(
self.state,
self.population,
self.gender,
self.age,
self.module
)
# Store the log from the Synthea run in the database.
self.log = log
self.save()
# Return the zip file locaton for download
return zip_file