Skip to content

Commit

Permalink
auto generate grad year
Browse files Browse the repository at this point in the history
closes #224
  • Loading branch information
JasonLovesDoggo committed Jan 6, 2024
1 parent 4eaf95b commit 75a5d92
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 32 deletions.
3 changes: 2 additions & 1 deletion core/api/views/objects/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ class NewSerializer(serializers.ModelSerializer):
required=True,
)
password = serializers.CharField(required=True, write_only=True)

# Default `create` and `update` behavior...
def create(self, validated_data) -> User:
password = validated_data.pop("password")
password = validated_data.pop("password")
user = User(**validated_data)
if validated_data["email"].endswith(settings.TEACHER_EMAIL_SUFFIX):
user.is_teacher = True
Expand Down
4 changes: 2 additions & 2 deletions core/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from django.db.models import Q, CharField
from django.utils import timezone

from .choices import graduating_year_choices, timezone_choices
from . import timezone_choices, graduating_year_choices
from .course import Term
from .post import Announcement
from ..utils import calculate_years
from ..utils.choices import calculate_years
from ..utils.fields import SetField, ChoiceArrayField


Expand Down
1 change: 0 additions & 1 deletion core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"announcements": AnnouncementsSitemap,
"clubs": ClubsSitemap,
"flatpages": FlatpagesSitemap,

}
},
name="django.contrib.sitemaps.sitemaps",
Expand Down
62 changes: 62 additions & 0 deletions core/utils/choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import annotations

from typing import Literal, Optional, List, Tuple

import pytz
from django.utils import timezone

timezone_choices = [(i, i) for i in pytz.common_timezones]

announcement_status_choices = [
("d", "Draft"),
("p", "Pending Approval"),
("a", "Approved"),
("r", "Rejected"),
]

announcement_status_initial_choices = [
("d", "Draft (don't send)"),
("p", "Send to supervisor for review"),
]


def calculate_student_years() -> List[Tuple[int | str, int | None]]:
current_year = timezone.now().year
current_month = timezone.now().month

# If the current month is between January and July, use the previous year
if current_month >= 9:
current_year += 1
year_ranges = [
(year, year)
for year in range(current_year, current_year + 4) # generate next years to
]
year_ranges.insert(0, (None, "Does not apply"))
return year_ranges


graduating_year_choices = calculate_student_years()


def calculate_years(
fmt: Literal["generate", "is_alumni"], user_years: Optional[List] = None
) -> List[Tuple[str, str]] | bool:
if fmt == "is_alumni" and user_years is None:
raise ValueError("user_years must be provided when fmt is is_alumni")

current_year = timezone.now().year
current_month = timezone.now().month

# If the current month is between January and July, use the previous year
if 1 <= current_month <= 7:
current_year -= 1
if fmt == "generate":
year_ranges = [
f"{year}-{str(year + 1)}"
for year in range(2021, current_year + 2) # generate next years to
]
years = zip(year_ranges, year_ranges)
return list(years)
elif fmt == "is_alumni":
current_year_range = f"{current_year}-{str(current_year + 1)}"
return current_year_range not in user_years
25 changes: 0 additions & 25 deletions core/utils/local_date.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import datetime
from typing import List, Literal, Tuple, Optional

from django.utils import timezone

Expand All @@ -14,27 +13,3 @@ def get_localdate(date=None, time=None):
datetime.datetime.combine(date, datetime.time(*time))
)
return date


def calculate_years(
fmt: Literal["generate", "is_alumni"], user_years: Optional[List] = None
) -> List[Tuple[str, str]] | bool:
if fmt == "is_alumni" and user_years is None:
raise ValueError("user_years must be provided when fmt is is_alumni")

current_year = timezone.now().year
current_month = timezone.now().month

# If the current month is between January and July, use the previous year
if 1 <= current_month <= 7:
current_year -= 1
if fmt == "generate":
year_ranges = [
f"{year}-{str(year + 1)}"
for year in range(2021, current_year + 2) # generate next years to
]
years = zip(year_ranges, year_ranges)
return list(years)
elif fmt == "is_alumni":
current_year_range = f"{current_year}-{str(current_year + 1)}"
return current_year_range not in user_years
3 changes: 2 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def main():
"forget to activate a virtual environment?"
) from exc
warnings.filterwarnings(
"ignore", lineno=37, module="dateutil.tz", category=DeprecationWarning)
"ignore", lineno=37, module="dateutil.tz", category=DeprecationWarning
)
execute_from_command_line(sys.argv)


Expand Down
2 changes: 1 addition & 1 deletion metropolis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@
ALLOWED_HIJACKERS = [746, 165] # Jason Cameron & Ken Shibata


DEFAULT_TIMEZONE = "America/Toronto" # default timezone for users
DEFAULT_TIMEZONE = "America/Toronto" # default timezone for users

ANNOUNCEMENT_APPROVAL_BCC_LIST = []

Expand Down
3 changes: 2 additions & 1 deletion metropolis/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from django.core.wsgi import get_wsgi_application

warnings.filterwarnings(
"ignore", lineno=37, module="dateutil.tz", category=DeprecationWarning)
"ignore", lineno=37, module="dateutil.tz", category=DeprecationWarning
)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "metropolis.settings")

Expand Down

0 comments on commit 75a5d92

Please sign in to comment.