Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: nullable date-of-birth #909

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ephios/core/migrations/0017_alter_userprofile_date_of_birth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.7 on 2023-03-10 16:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0016_alter_userprofile_phone"),
]

operations = [
migrations.AlterField(
model_name="userprofile",
name="date_of_birth",
field=models.DateField(null=True, verbose_name="date of birth"),
),
]
9 changes: 6 additions & 3 deletions ephios/core/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uuid
from datetime import date
from itertools import chain
from typing import Optional

import guardian.mixins
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -92,7 +93,7 @@ class UserProfile(guardian.mixins.GuardianUserMixin, PermissionsMixin, AbstractB
is_staff = BooleanField(default=False, verbose_name=_("Staff user"))
first_name = CharField(_("first name"), max_length=254)
last_name = CharField(_("last name"), max_length=254)
date_of_birth = DateField(_("date of birth"))
date_of_birth = DateField(_("date of birth"), null=True, blank=False)
phone = CharField(_("phone number"), max_length=254, blank=True, null=True)
calendar_token = CharField(_("calendar token"), max_length=254, default=secrets.token_urlsafe)

Expand Down Expand Up @@ -123,13 +124,15 @@ def get_short_name(self):
return self.first_name

@property
def age(self):
def age(self) -> Optional[int]:
if self.date_of_birth is None:
return None
jeriox marked this conversation as resolved.
Show resolved Hide resolved
today, born = date.today(), self.date_of_birth
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

@property
def is_minor(self):
return self.age < 18
return self.date_of_birth is not None and self.age < 18

def as_participant(self):
from ephios.core.signup.participants import LocalUserParticipant
Expand Down