Skip to content

Commit

Permalink
fix(birthdays): clean up user birthday fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Laur04 authored and anonymoose2 committed May 14, 2020
1 parent e15e04c commit b47f849
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 89 deletions.
1 change: 0 additions & 1 deletion intranet/apps/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class ProfileEditForm(forms.ModelForm):
graduation_year = forms.IntegerField(label="Graduation Year")
gender = forms.ChoiceField(choices=GENDERS, label="Sex (M or F)")
counselor_id = forms.IntegerField(label="Counselor ID", required=False)
birthday = forms.DateField(label="Birth Date", required=False)

class Meta:
model = get_user_model()
Expand Down
55 changes: 1 addition & 54 deletions intranet/apps/users/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pylint: disable=too-many-lines; Allow more than 1000 lines
import logging
from base64 import b64encode
from datetime import datetime, timedelta
from datetime import timedelta
from typing import Collection, Dict, Optional, Union

from dateutil.relativedelta import relativedelta
Expand Down Expand Up @@ -85,24 +85,6 @@ def user_with_name(self, given_name: Optional[str] = None, last_name: Optional[s
except (User.DoesNotExist, User.MultipleObjectsReturned):
return None

def users_with_birthday(self, month: int, day: int) -> Union[Collection["User"], QuerySet]: # pylint: disable=unsubscriptable-object
"""Return a ``QuerySet`` of user objects who have a birthday on a given date and have made their birthday public.
Args:
month: The month to check for a birthday.
day: The day to chack for a birthday.
Returns:
A ``QuerySet`` of user objects who have a birthday on a given date and have made their birthday public.
"""
return User.objects.filter(
properties___birthday__month=month,
properties___birthday__day=day,
properties__self_show_birthday=True,
properties__parent_show_birthday=True,
)

def get_students(self) -> Union[Collection["User"], QuerySet]: # pylint: disable=unsubscriptable-object
"""Get user objects that are students (quickly)."""
users = User.objects.filter(user_type="student", graduation_year__gte=settings.SENIOR_GRADUATION_YEAR)
Expand Down Expand Up @@ -249,18 +231,6 @@ def address(self) -> Optional["Address"]:
"""
return self.properties.address

@property
def birthday(self) -> Optional[datetime.date]:
"""Returns a ``datetime.date`` representing this user's birthday, or ``None`` if it is not
set or the current user does not have permission to access it.
Returns:
A ``datetime.date`` representing this user's birthday, or ``None`` if it is not set or
the current user does not have permission to access it.
"""
return self.properties.birthday

@property
def schedule(self) -> Optional[Union[QuerySet, Collection["Section"]]]: # pylint: disable=unsubscriptable-object
"""Returns a QuerySet of the ``Section`` objects representing the classes this student is
Expand Down Expand Up @@ -595,20 +565,6 @@ def is_female(self) -> bool:
"""
return self.gender is False

@property
def age(self) -> Optional[int]:
"""Returns a user's age, based on their birthday.
Returns:
The user's age as an integer, or None if their birthday is not set.
"""
birthday = self.birthday
if birthday:
return (datetime.today().date() - birthday).days // 365

return None

@property
def can_view_eighth(self) -> bool:
"""Checks if a user has the show_eighth permission.
Expand Down Expand Up @@ -1051,7 +1007,6 @@ class UserProperties(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="properties", on_delete=models.CASCADE)

_address = models.OneToOneField("Address", null=True, blank=True, on_delete=models.SET_NULL)
_birthday = models.DateField(null=True)
_schedule = models.ManyToManyField("Section", related_name="_students")
""" User preference permissions (privacy options)
When setting permissions, use set_permission(permission, value , parent=False)
Expand All @@ -1072,9 +1027,6 @@ class UserProperties(models.Model):
self_show_telephone = models.BooleanField(default=False)
parent_show_telephone = models.BooleanField(default=False)

self_show_birthday = models.BooleanField(default=False)
parent_show_birthday = models.BooleanField(default=False)

self_show_eighth = models.BooleanField(default=False)
parent_show_eighth = models.BooleanField(default=False)

Expand All @@ -1086,8 +1038,6 @@ def __getattr__(self, name):
return object.__getattribute__(self, name)
if name == "address":
return self._address if self.attribute_is_visible("show_address") else None
if name == "birthday":
return self._birthday if self.attribute_is_visible("show_birthday") else None
if name == "schedule":
return self._schedule if self.attribute_is_visible("show_schedule") else None
raise AttributeError("{!r} object has no attribute {!r}".format(type(self).__name__, name))
Expand All @@ -1096,9 +1046,6 @@ def __setattr__(self, name, value):
if name == "address":
if self.attribute_is_visible("show_address"):
self._address = value
if name == "birthday":
if self.attribute_is_visible("show_birthday"):
self._birthday = value
super(UserProperties, self).__setattr__(name, value) # pylint: disable=no-member; Pylint is wrong

def __str__(self):
Expand Down
1 change: 0 additions & 1 deletion intranet/apps/users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class Meta:
"emails",
"grade",
"graduation_year",
"birthday",
"user_type",
"phones",
"websites",
Expand Down
35 changes: 2 additions & 33 deletions intranet/apps/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,34 +249,6 @@ def test_user_with_name(self):
for user in users:
user.delete()

def test_users_with_birthday(self):
user = self.login()
properties = user.properties

properties.birthday = datetime.date(2019, 1, 1)
properties.self_show_birthday = False
properties.parent_show_birthday = False
properties.save()

self.assertNotIn(user.id, [u.id for u in get_user_model().objects.users_with_birthday(1, 1)])

properties.parent_show_birthday = True
properties.save()
self.assertNotIn(user.id, [u.id for u in get_user_model().objects.users_with_birthday(1, 1)])

properties.parent_show_birthday = False
properties.self_show_birthday = True
properties.save()
self.assertNotIn(user.id, [u.id for u in get_user_model().objects.users_with_birthday(1, 1)])

properties.parent_show_birthday = True
properties.self_show_birthday = True
properties.save()
self.assertIn(user.id, [u.id for u in get_user_model().objects.users_with_birthday(1, 1)])

self.assertNotIn(user.id, [u.id for u in get_user_model().objects.users_with_birthday(1, 2)])
self.assertNotIn(user.id, [u.id for u in get_user_model().objects.users_with_birthday(2, 1)])

def test_notification_email(self):
# Test default user notification email property
user = self.login()
Expand Down Expand Up @@ -642,13 +614,10 @@ def test_profile_view(self):
def test_privacy_options(self):
self.assertEqual(set(PERMISSIONS_NAMES.keys()), {"self", "parent"})
for k in ["self", "parent"]:
self.assertEqual(
set(PERMISSIONS_NAMES[k]), {"show_pictures", "show_address", "show_telephone", "show_birthday", "show_eighth", "show_schedule"}
)
self.assertEqual(set(PERMISSIONS_NAMES[k]), {"show_pictures", "show_address", "show_telephone", "show_eighth", "show_schedule"})

self.assertEqual(set(self.user.permissions.keys()), {"self", "parent"})
for k in ["self", "parent"]:
self.assertEqual(
set(self.user.permissions[k].keys()),
{"show_pictures", "show_address", "show_telephone", "show_birthday", "show_eighth", "show_schedule"},
set(self.user.permissions[k].keys()), {"show_pictures", "show_address", "show_telephone", "show_eighth", "show_schedule"},
)

0 comments on commit b47f849

Please sign in to comment.