def get_available_episodes(self): return Episodes.objects.filter( Q(AlbumId__HiddenAlbum=False), Q(DateReleased__lt=TimeUtils.user_now()), Q(AlbumId__AlbumId__in=self.purchased_albums_ids) | Q(AlbumId__PurchaseStatus=Albums.PURCHASE_TYPE_NOT_FOR_SALE) )
def get_status(self): if self.is_current(): return self.STATUS_CURRENT elif self.EndDate > TimeUtils.user_now().date(): return self.STATUS_NEXT else: return self.STATUS_EXPIRED
def next_end_time(self): end_datetime = self._get_end_time() if end_datetime < TimeUtils.user_now(): end_datetime += timedelta(days=1) return end_datetime
def get_available_timeslots(self): today_utc = TimeUtils.user_now() weekday = str(today_utc.weekday()) return TimeSlots.objects.select_related(depth=2)\ .filter(EndDate__gte=today_utc, StartDate__lte=today_utc, days__contains=weekday)\ .order_by('StartTime')
def _get_end_time(self): end_datetime = TimeUtils.user_now().replace(hour=self.EndTime.hour, minute=self.EndTime.minute, second=self.EndTime.second) if self.EndTime < self.StartTime: end_datetime += timedelta(days=1) return end_datetime
def get_context_data(self): timeslots = self.get_available_timeslots() timeslots = [self.jsonify(timeslot) for timeslot in timeslots] timeslots.sort(cmp=GetTimeslots.cmp_timeslots) return { 'CurrentTime': TimeUtils.user_now(), 'Timeslots': timeslots, }
def default(self, o): if isinstance(o, datetime): return str(TimeUtils.get_timestamp(o)) elif isinstance(o, decimal.Decimal): return str(o) elif isinstance(o, CustomFieldFile): return o.get_url() else: return super(ApiJSONEncoder, self).default(o)
def next_start_time(self): user_now = TimeUtils.user_now() start_datetime = user_now.replace(hour=self.StartTime.hour, minute=self.StartTime.minute, second=self.StartTime.second) if self._get_end_time() < user_now: start_datetime += timedelta(days=1) return start_datetime
def get_autoepisode(self, start_episode_id, start_time): d1 = TimeUtils.user_now() delta = d1 - datetime(start_time.year, start_time.month, start_time.day) video = self.caching.get_episode(start_episode_id) episodes = Episodes.objects.filter(AlbumId=video.AlbumId)\ .order_by('EpisodeNo') if len(episodes) > delta.days: return episodes[delta.days] return None
def clean_timezone(self): param_name = 'tz' if param_name not in self.params: raise ParameterExpected(parameter=param_name) timezone = self.params[param_name] try: user_timezone = pytz.timezone(timezone) user_now = TimeUtils.get_utc_now_as_local(user_timezone) LocalUserMiddleware.update(user_timezone=user_timezone, user_now=user_now) except pytz.exceptions.UnknownTimeZoneError: raise BadRequest(message='Unknown timezone.')
def is_episode_on_air(self, episode): user_now = TimeUtils.user_now() today = user_now.date() date_released = episode.DateReleased.date() if date_released > today: return False if date_released <= today: return True timeslot_videos = self._get_timeslot_videos(episode) for video in timeslot_videos: if video.TimeSlotsId.next_start_time < user_now: return True return bool(timeslot_videos)
def get_album_status(cls, album, released, updated): user_now = TimeUtils.user_now() if not album.episodes.all(): status = album.STATUS_NORMAL elif released > user_now: status = album.STATUS_COMING_SOON else: # TODO: move PiptureSettings from huge models file and remove inline import # I use inline import to avoid importing pipture.models in this file #from restserver.pipture.models import PiptureSettings premiere_days = PiptureSettings.get().PremierePeriod premiere_period = timedelta(days=premiere_days) if updated >= user_now - premiere_period: status = album.STATUS_PREMIERE else: status = album.STATUS_NORMAL return status
def is_current(self): return self.next_start_time < TimeUtils.user_now() < self.next_end_time
def is_episode_available(self, episode): return (episode.AlbumId.PurchaseStatus == Albums.PURCHASE_TYPE_NOT_FOR_SALE and episode.DateReleased < TimeUtils.user_now()) \ or self.is_episode_purchased(episode)