|
| 1 | +import json |
| 2 | + |
| 3 | +from django.db import models |
| 4 | +from django.forms import ValidationError |
| 5 | +from django.utils import timezone |
| 6 | + |
| 7 | +from .base import BaseModel |
| 8 | +from .response_set import ResponseSet |
| 9 | + |
| 10 | +from django.core.validators import MaxValueValidator, MinValueValidator |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +# def validate_less_than_age(self, value): |
| 15 | +# print(f"self : {self}") |
| 16 | +# print(f"value : {self.value}") |
| 17 | +# print("what") |
| 18 | +# print(f"dob : {self.response_set.date_of_birth}") |
| 19 | +# print(hasattr(self.response_set, "date_of_birth")) |
| 20 | +# if hasattr(self.response_set, "date_of_birth") : |
| 21 | +# if value < self.get_age : |
| 22 | +# raise ValidationError( |
| 23 | +# "Must be less than your age", |
| 24 | +# code="less_than_age", |
| 25 | +# ) |
| 26 | + |
| 27 | +class AgeWhenStartedSmokingResponse(BaseModel): |
| 28 | + response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name='age_when_started_smoking_response') |
| 29 | + value = models.PositiveIntegerField(validators=[ |
| 30 | + MinValueValidator(1, message="The age you started smoking must be between 1 and your current age"), |
| 31 | + # validate_less_than_age |
| 32 | + ] |
| 33 | + ) |
| 34 | + |
| 35 | + # def clean(self): |
| 36 | + # super().clean() |
| 37 | + # print(f"clean 1:{self.response_set}") |
| 38 | + # if hasattr(self.response_set, "date_of_birth_response") : |
| 39 | + # print(f"clean 2:{self.response_set.date_of_birth_response.value}") |
| 40 | + # today = timezone.now() |
| 41 | + # born = self.response_set.date_of_birth_response.value |
| 42 | + # age = today.year - born.year - ((today.month, today.day) < (born.month, born.day)) |
| 43 | + # print(f"value : {self.value}") |
| 44 | + # print(f"Age {age}") |
| 45 | + # if (self.value and self.value < age): |
| 46 | + # raise ValidationError( |
| 47 | + # "The age you started smoking must be the same as, or younger than your current age" |
| 48 | + # ) |
| 49 | + #if submitted_response_sets_in_last_year: |
| 50 | + # raise ValidationError( |
| 51 | + # "Responses have already been submitted for this user" |
| 52 | + # ) |
| 53 | + |
0 commit comments