updated schedule date to use local time

This commit is contained in:
Edward Tirado Jr 2025-07-03 01:29:35 -05:00
parent b69b2d367e
commit 447fe17825
3 changed files with 26 additions and 19 deletions

View file

@ -1,4 +1,7 @@
from zoneinfo import ZoneInfo
from django.contrib import admin
from django.utils import timezone
from movie_manager.models import Movie, MovieList, Schedule, Showing
@ -21,4 +24,15 @@ class ScheduleAdmin(admin.ModelAdmin):
@admin.register(Showing)
class ShowingAdmin(admin.ModelAdmin):
list_display = ["showtime", "movie"]
list_display = ["local_showtime", "movie"]
def local_showtime(self, obj):
if obj.showtime:
target_tz = ZoneInfo("America/Chicago")
with timezone.override(target_tz):
local_time = timezone.localtime(obj.showtime)
return local_time.strftime("%Y-%m-%d %H:%M")
return "Invalid datetime"
local_showtime.short_description = "Showtime (Local)"
local_showtime.admin_order_field = "showtime"

View file

@ -37,14 +37,15 @@ class ShowingViewsetTestCase(APITestCase):
self.assertEqual(response_data.get("showtime"), showing_time)
self.assertEqual(response_data.get("movie").get("title"), "Test Movie")
@freeze_time("2025-07-02 23:59:00", tz_offset=-5)
@freeze_time("2025-07-03 04:59:00") # 2025-07-02 23:59 CDT in UTC
def test_it_returns_active_showings(self):
self.client.force_authenticate(user=self.schedule.owner)
showtimes_america_chicago_utc = [
"2025-07-03T04:00:59.000Z", # 7/2/25 11:59pm
"2025-07-01T04:00:59.000Z", # 6/30/25 11:59pm
"2025-07-08T04:00:59.000Z", # 7/7/25 11:59pm
"2025-07-08T04:59:59.000Z", # 2025-07-07 11:59pm
"2025-07-03T04:59:59.000Z", # 2025-07-02 11:59pm
"2025-07-03T04:00:00.000Z", # 2025-07-02 11:00pm
"2025-07-01T04:59:00.000Z", # 2025-06-30 11:59pm
]
for showtime in showtimes_america_chicago_utc:

View file

@ -1,5 +1,3 @@
import datetime
from django.http import JsonResponse
from django.utils import timezone
from knox.auth import TokenAuthentication
@ -25,12 +23,8 @@ class ScheduleViewset(viewsets.ModelViewSet):
# Get the schedule instance
instance = self.get_object()
now = timezone.now()
# get time from start of day
today = timezone.make_aware(datetime.datetime(now.year, now.month, now.day))
upcoming_showings = Showing.objects.filter(
showtime__gte=today, schedule=instance
)
upcoming_showings = Showing.objects.filter(showtime__gte=now, schedule=instance)
serializer = self.get_serializer(instance)
data = serializer.data
@ -39,18 +33,16 @@ class ScheduleViewset(viewsets.ModelViewSet):
data["showings"] = ShowingSerializer(upcoming_showings, many=True).data
if request.GET.get("past_showings") == "true":
past_showings = Showing.objects.filter(
showtime__lt=today, schedule=instance
)
past_showings = Showing.objects.filter(showtime__lt=now, schedule=instance)
# Add both to the response
data["past_showings"] = [
{
"id": showing.id,
"showtime": showing.showtime.isoformat(),
"movie": MovieSerializer(showing.movie).data,
"id": past_showing.id,
"showtime": past_showing.showtime.isoformat(),
"movie": MovieSerializer(past_showing.movie).data,
}
for showing in past_showings
for past_showing in past_showings
]
else:
data["past_showings"] = []