# coding=utf-8 from django.db import models from web.models import StudyColumns, StudyNotificationParameters from django.core.validators import MaxValueValidator, MinValueValidator import re 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)] ) 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