Example #1
0
    def week(self):
        """
        Returns the week of the event relative to the first official season event as an integer
        Returns None if the event is not of type NON_CMP_EVENT_TYPES or is not official
        """
        if self.event_type_enum not in EventType.NON_CMP_EVENT_TYPES or not self.official:
            return None

        # Cache week_start for the same context
        cache_key = '{}_week_start:{}'.format(self.year, ndb.get_context().__hash__())
        week_start = context_cache.get(cache_key)
        if week_start is None:
            e = Event.query(
                Event.year==self.year,
                Event.event_type_enum.IN(EventType.NON_CMP_EVENT_TYPES),
                Event.start_date!=None
            ).order(Event.start_date).fetch(1, projection=[Event.start_date])
            if e:
                first_start_date = e[0].start_date
                diff_from_wed = (first_start_date.weekday() - 2) % 7  # 2 is Wednesday
                week_start = first_start_date - datetime.timedelta(days=diff_from_wed)
            else:
                week_start = None
        context_cache.set(cache_key, week_start)

        if self._week is None and week_start is not None:
            days = (self.start_date - week_start).days
            self._week = days / 7

        return self._week
    def week(self):
        """
        Returns the week of the event relative to the first official season event as an integer
        Returns None if the event is not of type NON_CMP_EVENT_TYPES or is not official
        """
        if self.event_type_enum not in EventType.NON_CMP_EVENT_TYPES or not self.official:
            return None

        # Cache week_start for the same context
        cache_key = '{}_week_start:{}'.format(self.year,
                                              ndb.get_context().__hash__())
        week_start = context_cache.get(cache_key)
        if week_start is None:
            e = Event.query(
                Event.year == self.year,
                Event.event_type_enum.IN(EventType.NON_CMP_EVENT_TYPES),
                Event.start_date != None).order(Event.start_date).fetch(
                    1, projection=[Event.start_date])
            if e:
                first_start_date = e[0].start_date
                diff_from_wed = (first_start_date.weekday() -
                                 2) % 7  # 2 is Wednesday
                week_start = first_start_date - datetime.timedelta(
                    days=diff_from_wed)
            else:
                week_start = None
        context_cache.set(cache_key, week_start)

        if self._week is None and week_start is not None:
            days = (self.start_date - week_start).days
            self._week = days / 7

        return self._week
Example #3
0
    def week(self):
        """
        Returns the week of the event relative to the first official season event as an integer
        Returns None if the event is not of type NON_CMP_EVENT_TYPES or is not official
        """
        if self.event_type_enum not in EventType.NON_CMP_EVENT_TYPES or not self.official:
            return None

        if self._week:
            return self._week

        # Cache week_start for the same context
        from context_cache import context_cache
        cache_key = '{}_season_start:{}'.format(self.year,
                                                ndb.get_context().__hash__())
        season_start = context_cache.get(cache_key)

        if season_start is None:
            e = Event.query(
                Event.year == self.year,
                Event.event_type_enum.IN(EventType.NON_CMP_EVENT_TYPES),
                Event.start_date != None).order(Event.start_date).fetch(
                    1, projection=[Event.start_date])
            if e:
                first_start_date = e[0].start_date

                days_diff = 0
                # Before 2018, event weeks start on Wednesdays
                if self.year < 2018:
                    days_diff = 2  # 2 is Wednesday

                # Find the closest start weekday (Monday or Wednesday) to the first event - this is our season start
                diff_from_week_start = (first_start_date.weekday() -
                                        days_diff) % 7
                diff_from_week_start = min(
                    [diff_from_week_start, diff_from_week_start - 7], key=abs)

                season_start = first_start_date - datetime.timedelta(
                    days=diff_from_week_start)
            else:
                season_start = None
        context_cache.set(cache_key, season_start)

        if self._week is None and season_start is not None:
            # Round events that occur just before the official start-of-season to the closest week
            days = max((self.start_date - season_start).days, 0)
            self._week = days / 7

        return self._week
    def week(self):
        """
        Returns the week of the event relative to the first official season event as an integer
        Returns None if the event is not of type NON_CMP_EVENT_TYPES or is not official
        """
        if self.event_type_enum not in EventType.NON_CMP_EVENT_TYPES or not self.official:
            return None

        # Cache week_start for the same context
        cache_key = '{}_week_start:{}'.format(self.year,
                                              ndb.get_context().__hash__())
        week_start = context_cache.get(cache_key)
        if week_start is None:
            e = Event.query(
                Event.year == self.year,
                Event.event_type_enum.IN(EventType.NON_CMP_EVENT_TYPES),
                Event.start_date != None).order(Event.start_date).fetch(
                    1, projection=[Event.start_date])
            if e:
                first_start_date = e[0].start_date

                days_diff = 0
                # Before 2020, event weeks start on Wednesdays
                if self.year < 2020:
                    days_diff = 2  # 2 is Wednesday
                diff_from_week_start = (first_start_date.weekday() -
                                        days_diff) % 7
                week_start = first_start_date - datetime.timedelta(
                    days=diff_from_week_start)
            else:
                week_start = None
        context_cache.set(cache_key, week_start)

        if self._week is None and week_start is not None:
            days = (self.start_date - week_start).days
            week = days / 7
            # In 2020 there's an empty week in between Week 8 and Week 9 - mathmatically it's Week 10, but we treat it
            # as Week 9 - we should make sure we take that in to account. We look for > 7 because weeks are zero-indexed
            if self.year == 2020 and week > 7:
                week -= 1
            self._week = week

        return self._week