|
1 | | -import json |
2 | | - |
3 | 1 | from django.db import models |
4 | 2 | from django.forms import ValidationError |
5 | | -from django.utils import timezone |
6 | 3 |
|
7 | 4 | from .base import BaseModel |
8 | 5 | from .response_set import ResponseSet |
9 | 6 |
|
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 | | -# ) |
| 7 | +from django.core.validators import MinValueValidator |
26 | 8 |
|
27 | 9 | class AgeWhenStartedSmokingResponse(BaseModel): |
28 | | - response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name='age_when_started_smoking_response') |
| 10 | + response_set = models.OneToOneField(ResponseSet, on_delete=models.CASCADE, related_name="age_when_started_smoking_response") |
29 | 11 | 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 |
| 12 | + MinValueValidator(1, message="The age you started smoking must be between 1 and your current age") |
32 | 13 | ] |
33 | 14 | ) |
34 | 15 |
|
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 | | - |
| 16 | + def clean(self): |
| 17 | + super().clean() |
| 18 | + if hasattr(self.response_set, "date_of_birth_response") : |
| 19 | + if (self.value and self.value > self.response_set.date_of_birth_response.age_in_years()): |
| 20 | + raise ValidationError({ |
| 21 | + "value":ValidationError( |
| 22 | + "age started smoking must be less than current age", |
| 23 | + code="age_started_smoking_greater_than_current_age") |
| 24 | + } |
| 25 | + ) |
| 26 | + else: |
| 27 | + raise ValidationError({ |
| 28 | + "value":ValidationError( |
| 29 | + "date of birth not set", |
| 30 | + code="no_date_of_birth") |
| 31 | + } |
| 32 | + ) |
0 commit comments