# coding=utf-8 from django.db import models from web.models import StudyColumns, StudyNotificationParameters from django.core.validators import MaxValueValidator, MinValueValidator import re FOLLOW_UP_INCREMENT_IN_YEARS = 'years' FOLLOW_UP_INCREMENT_IN_DAYS = 'days' FOLLOW_UP_INCREMENT_UNIT_CHOICE = { FOLLOW_UP_INCREMENT_IN_YEARS: 'Years', FOLLOW_UP_INCREMENT_IN_DAYS: 'Days' } class Study(models.Model): class Meta: app_label = 'web' name = models.CharField(max_length=255, verbose_name='Name') nd_number_study_subject_regex = models.CharField( max_length=255, verbose_name='Study Subject ND Number Regex', default=r'^ND\d{4}$', help_text='Defines the regex to check the ID used for each study subject. Keep in mind that this regex should be valid for all previous study subjects in the database.') columns = models.OneToOneField( StudyColumns, on_delete=models.CASCADE, ) notification_parameters = models.OneToOneField( StudyNotificationParameters, on_delete=models.CASCADE, ) auto_create_follow_up = models.BooleanField( default=True, verbose_name="Auto create follow up visit" ) visits_to_show_in_subject_list = models.IntegerField( verbose_name='Number of visits to show in the subject list', default=5, validators=[MaxValueValidator(100), MinValueValidator(1)] ) default_voucher_expiration_in_months = models.IntegerField( verbose_name='Duration of the vouchers in months', default=3, validators=[MinValueValidator(1)] ) default_visit_duration_in_months = models.IntegerField( verbose_name='Duration of the visits in months', help_text='Duration of the visit, this is, the time interval, in months, when the appointments may take place', default=6, validators=[MinValueValidator(1)] ) default_delta_time_for_patient_follow_up = models.IntegerField( verbose_name='Time difference between patient visits', help_text='Time difference between visits used to automatically create follow up visits', default=1, validators=[MinValueValidator(1)] ) default_delta_time_for_control_follow_up = models.IntegerField( verbose_name='Time difference between control visits', help_text='Time difference between visits used to automatically create follow up visits', default=4, validators=[MinValueValidator(1)] ) default_delta_time_for_follow_up_units = models.CharField(max_length=10, choices=FOLLOW_UP_INCREMENT_UNIT_CHOICE.items(), verbose_name='Units for the follow up incrementals', help_text='Units for the number of days between visits for both patients and controls', default=FOLLOW_UP_INCREMENT_IN_YEARS, blank=False ) def check_nd_number(self, nd_number): regex = re.compile(self.nd_number_study_subject_regex) return regex.match(nd_number) is not None def __str__(self): return "%s" % self.name def __unicode__(self): return "%s" % self.name @property def has_voucher_types(self): return self.columns.voucher_types @property def has_vouchers(self): return self.columns.vouchers @staticmethod def get_by_id(study_id): study = Study.objects.filter(id=study_id) if len(study) > 0: return study[0] else: return None