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

is_instructional is useful again! #284

Merged
merged 7 commits into from
Oct 10, 2024
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
72 changes: 20 additions & 52 deletions core/api/views/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,28 @@ class EventsList(ListAPIViewWithFallback):
pagination_class = None

def get_queryset(self):
start = timezone.now()
if self.request.data.get("start"):
start = self.request.data.get("start")
elif self.request.query_params.get("start"):
start = self.request.query_params.get("start")
start = (
self.request.data.get("start")
or self.request.query_params.get("start")
or timezone.now()
)
end = self.request.data.get("end") or self.request.query_params.get("end")

events = models.Event.objects.filter(end_date__gte=start)

if end:
events = events.filter(start_date__lte=end)

if not self.request.user.is_anonymous:
if self.request.data.get("end"):
end = self.request.data.get("end")
events = (
models.Event.objects.filter(
end_date__gte=start, start_date__lte=end
)
.filter(
Q(is_public=True) | Q(organization__member=self.request.user.id)
)
.distinct()
.order_by("start_date")
)
elif self.request.query_params.get("end"):
end = self.request.query_params.get("end")
events = (
models.Event.objects.filter(
end_date__gte=start, start_date__lte=end
)
.filter(
Q(is_public=True) | Q(organization__member=self.request.user.id)
)
.distinct()
.order_by("start_date")
)
else:
JasonLovesDoggo marked this conversation as resolved.
Show resolved Hide resolved
events = (
models.Event.objects.filter(end_date__gte=start)
.filter(
Q(is_public=True) | Q(organization__member=self.request.user.id)
)
.distinct()
.order_by("start_date")
)
events = events.filter(
Q(is_public=True)
| Q(organization__member=self.request.user.id)
| Q(organization__supervisors=self.request.user.id)
| Q(organization__execs=self.request.user.id)
).distinct()
else:
if self.request.data.get("end"):
end = self.request.data.get("end")
events = models.Event.objects.filter(
end_date__gte=start, start_date__lte=end, is_public=True
).order_by("start_date")
elif self.request.query_params.get("end"):
end = self.request.query_params.get("end")
events = models.Event.objects.filter(
end_date__gte=start, start_date__lte=end, is_public=True
).order_by("start_date")
else:
events = models.Event.objects.filter(
end_date__gte=start, is_public=True
).order_by("start_date")
events = events.filter(Q(is_public=True))

events = events.order_by("start_date")

return events
1 change: 1 addition & 0 deletions core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def __init__(self, *args, **kwargs):

self.fields["schedule_format"].initial = "default"
self.fields["term"].initial = models.Term.get_current()
self.fields["is_instructional"].disabled = True

if "instance" in kwargs and kwargs["instance"] is not None:
instance = kwargs["instance"]
Expand Down
3 changes: 1 addition & 2 deletions core/management/commands/add_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def handle(self, *args, **options):
"term": options["term"] if options["term"] else None,
"organization": None,
"schedule_format": "default",
"is_instructional": True,
"is_public": True,
"should_announce": False,
}
Expand Down Expand Up @@ -182,7 +181,7 @@ def handle(self, *args, **options):
event_data["term"]
)

for key in ["is_instructional", "is_public", "should_announce"]:
for key in ["is_public", "should_announce"]:
event_data[key] = self._get_boolean(key, event_data[key])

event = Event(**event_data)
Expand Down
85 changes: 57 additions & 28 deletions core/models/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import datetime as dt

from django.conf import settings
from django.core.exceptions import MultipleObjectsReturned
from django.core.exceptions import MultipleObjectsReturned, ValidationError
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from .. import utils

Expand Down Expand Up @@ -34,13 +35,14 @@ def is_current(self, target_date=None):
return self.start_date <= target_date < self.end_date

def day_is_instructional(self, target_date=None):
target_date = utils.get_localdate(date=target_date, time=[11, 0, 0])
target_date_start = utils.get_localdate(date=target_date, time=[0, 0, 0])
target_date_end = utils.get_localdate(date=target_date, time=[23, 59, 59])
return (
target_date.weekday() < 5
and not self.events.filter(
is_instructional=False,
start_date__lte=target_date,
end_date__gte=target_date,
start_date__lt=target_date_end,
end_date__gt=target_date_start,
).exists()
)

Expand All @@ -51,9 +53,9 @@ def day_num(self, target_date=None):
"calendar_days": self.__day_num_calendar_days,
}
target_date = utils.get_localdate(date=target_date, time=[23, 59, 59])
if (
not self.is_current(target_date.date()) or target_date.weekday() >= 5
): # TODO: check for pa days
JasonLovesDoggo marked this conversation as resolved.
Show resolved Hide resolved
if not self.is_current(target_date.date()) or not self.day_is_instructional(
target_date
):
return None
return methods[tf.get("day_num_method", "consecutive")](tf, target_date)

Expand Down Expand Up @@ -91,18 +93,16 @@ def __day_num_consecutive(self, tf, target_date):
return (len(cycle_day_type_set) - 1) % tf["cycle"]["length"] + 1

def day_schedule_format(self, target_date=None):
tds = utils.get_localdate(date=target_date, time=[0, 0, 0]) # target date start
tde = utils.get_localdate(
date=target_date, time=[23, 59, 59]
) # target date end
target_date_start = utils.get_localdate(date=target_date, time=[0, 0, 0])
target_date_end = utils.get_localdate(date=target_date, time=[23, 59, 59])

schedule_formats = settings.TIMETABLE_FORMATS[self.timetable_format][
"schedules"
]
schedule_format_set = set(
self.events.filter(start_date__lte=tde, end_date__gte=tds).values_list(
"schedule_format", flat=True
)
self.events.filter(
start_date__lte=target_date_end, end_date__gte=target_date_start
).values_list("schedule_format", flat=True)
).intersection(set(schedule_formats.keys()))
for schedule_format in list(schedule_formats.keys())[::-1]:
if schedule_format in schedule_format_set:
Expand Down Expand Up @@ -149,25 +149,37 @@ def day_schedule(self, target_date=None):
class MisconfiguredTermError(Exception):
pass

def save(self, *args, **kwargs):
def clean(self):
if self.start_date > self.end_date:
raise self.MisconfiguredTermError("Start date must be before end date")
raise ValidationError(
{
"start_date": _("Start date must be before end date"),
"end_date": _("Start date must be before end date"),
}
)

# check for overlapping terms
for term in Term.objects.all():
if term.id == self.id:
continue

if term.start_date <= self.start_date < term.end_date:
raise self.MisconfiguredTermError(
"Current term's date range overlaps with existing term"
)

if term.start_date < self.end_date <= term.end_date:
raise self.MisconfiguredTermError(
"Current term's date range overlaps with existing term"
if (
term.start_date <= self.start_date < term.end_date
and term.start_date < self.end_date <= term.end_date
):
raise ValidationError(
{
"start_date": _(
"Current term's date range overlaps with existing term"
),
"end_date": _(
"Current term's date range overlaps with existing term"
),
}
)

def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)

@classmethod
Expand Down Expand Up @@ -223,16 +235,15 @@ class Event(models.Model):

schedule_format = models.CharField(max_length=64, default="default")
is_instructional = models.BooleanField(
default=True,
help_text="Whether this event changes the day's schedule or not. Leave checked if not direct cause. (don't change for presentations, etc.)",
help_text="Whether or not school is running on this day. Automatically changes depending on the schedule format and should not be manually edited.",
)
is_public = models.BooleanField(
default=True,
help_text="Whether if this event pertains to the general school population, not just those in the organization.",
help_text="Whether or not this event is viewable to the general school population, not just those in the organization.",
)
should_announce = models.BooleanField(
default=False,
help_text="Whether if this event should be announced to the general school population VIA the important events feed.",
help_text="Whether or not this event should be announced to the general school population VIA the important events feed.",
)

tags = models.ManyToManyField(
Expand All @@ -254,6 +265,15 @@ def get_events(cls, user=None):

return events

def clean(self):
if self.start_date > self.end_date:
raise ValidationError(
{
"start_date": _("Start date must be before end date"),
"end_date": _("Start date must be before end date"),
}
)

def save(self, *args, **kwargs):
if not timezone.is_aware(self.end_date):
# Convert naive datetime to aware datetime
Expand All @@ -265,4 +285,13 @@ def save(self, *args, **kwargs):
self.start_date = timezone.make_aware(
self.start_date, timezone.get_current_timezone()
)

self.clean()

schedule_formats = settings.TIMETABLE_FORMATS[self.term.timetable_format][
"schedules"
]
self.is_instructional = len(schedule_formats[self.schedule_format]) > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't love this implementation, could be more explicit but it'll pass

# PA days and holidays do not have time data

super().save(*args, **kwargs)
1 change: 0 additions & 1 deletion core/utils/test_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def create_winter_break(school_org: Organization, term: Term):
end_date=now + datetime.timedelta(days=10),
organization=school_org,
term=term,
is_instructional=False,
)
event.save()

Expand Down
2 changes: 2 additions & 0 deletions metropolis/timetable_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@
"position": [{4, 6}, {3, 6}],
},
],
"pa-day": [],
JasonLovesDoggo marked this conversation as resolved.
Show resolved Hide resolved
"holiday": [],
},
"courses": 4,
"positions": {1, 2, 3, 4, 5, 6},
Expand Down
Loading