Skip to content

Commit

Permalink
fix get_events by day
Browse files Browse the repository at this point in the history
  • Loading branch information
mateow99 committed Mar 20, 2024
1 parent 4b61746 commit a8a5dc0
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/eatery/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from eatery.models import Eatery
from event.models import Event
from event.serializers import EventSerializer, EventSerializerSimple, EventReadSerializer
from django.utils import timezone
from datetime import date, timedelta
from time import mktime
from datetime import timedelta, datetime
from zoneinfo import ZoneInfo

class EaterySerializer(serializers.ModelSerializer):
id = serializers.IntegerField()
Expand Down Expand Up @@ -65,10 +64,12 @@ class EaterySerializerByDay(serializers.ModelSerializer):
events = serializers.SerializerMethodField()

def get_events(self, obj):
day = self.context.get("day")
today = (timezone.now() - timedelta(hours=5)).date() + timedelta(days=day)
today_unix = mktime(today.timetuple()) + timedelta(hours=5).seconds
events = Event.objects.filter(eatery=obj.id, start__gte=today_unix, start__lte=today_unix + timedelta(days=1).total_seconds())
day_offset = self.context.get("day")
now = datetime.now(ZoneInfo("America/New_York"))
day = now.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=day_offset)
day_unix = int(day.timestamp())
day_end_unix = int((day + timedelta(days=1)).timestamp())
events = Event.objects.filter(eatery=obj.id, start__gte=day_unix, start__lt=day_end_unix)
serializer = EventReadSerializer(instance=events, many=True)
return serializer.data

Expand Down

0 comments on commit a8a5dc0

Please sign in to comment.