def __get_events(after, before, json_mode=False): background_colours = {"L": "#F9AD81", "B": "#C4DF9B", "S": "#8493CA"} text_colours = {"L": "black", "B": "black", "S": "black"} next_schedules, next_dates = Schedule.between(after=after, before=before) schedules = [] dates = [] episodes = [] event_list = [] for x in range(len(next_schedules)): for y in range(len(next_dates[x])): # next_events.append([next_schedules[x], next_dates[x][y]]) schedule = next_schedules[x] schedules.append(schedule) date = next_dates[x][y] dates.append(date) episode = None # if schedule == live if next_schedules[x].type == 'L': try: episode = Episode.objects.get(issue_date=date) except Episode.DoesNotExist: pass # broadcast elif next_schedules[x].source: try: source_date = next_schedules[x].source.date_before(date) if source_date: episode = Episode.objects.get(issue_date=source_date) except Episode.DoesNotExist: pass episodes.append(episode) if episode: url = reverse( 'programmes:episode_detail', args=(schedule.programme.slug, episode.season, episode.number_in_season,) ) else: url = reverse('programmes:detail', args=(schedule.programme.slug,)) event_entry = { 'id': schedule.id, 'start': str(date), 'end': str(date + schedule.runtime()), 'allDay': False, 'title': schedule.programme.name, 'type': schedule.type, 'textColor': text_colours[schedule.type], 'backgroundColor': background_colours[schedule.type], 'url': url } event_list.append(event_entry) if json_mode: return event_list else: if schedules: dates, schedules, episodes = (list(t) for t in zip(*sorted(zip(dates, schedules, episodes)))) return zip(schedules, dates, episodes) return None
def test_day_schedule(self): schedules, dates = Schedule.between(datetime.datetime(2014, 1, 6), datetime.datetime(2014, 1, 7)) self.assertEqual(4, len(schedules)) schedule_1 = Schedule.objects.get(programme=Programme.objects.get(name="Programme 00:00 - 09:00"), day=MO) schedule_2 = Schedule.objects.get(programme=Programme.objects.get(name="Programme 09:00 - 10:00"), day=MO) schedule_3 = Schedule.objects.get(programme=Programme.objects.get(name="Programme 10:00 - 12:00"), day=MO) schedule_4 = Schedule.objects.get(programme=Programme.objects.get(name="Programme 00:00 - 09:00"), day=TU) self.assertTrue(schedule_1 in schedules) self.assertTrue(schedule_2 in schedules) self.assertTrue(schedule_3 in schedules) self.assertTrue(schedule_4 in schedules)
def recording_schedules(request): start = datetime.datetime.strptime(request.GET.get('start'), '%Y-%m-%d %H:%M:%S') next_hours = request.GET.get("next_hours", None) if not next_hours: next_hours = PodcastConfiguration.objects.get().next_events else: next_hours = int(next_hours) json_list = [] next_schedules, next_dates = Schedule.between(start, start + relativedelta(hours=next_hours), live=True) schedules = [] dates = [] for x in range(len(next_schedules)): for y in range(len(next_dates[x])): schedules.append(next_schedules[x]) dates.append(next_dates[x][y]) # sort if schedules: dates, schedules = (list(t) for t in zip(*sorted(zip(dates, schedules)))) for x in range(len(schedules)): schedule = schedules[x] date = dates[x] # Create the episode to retrieve season and episode number try: episode = Episode.objects.select_related('programme').get(issue_date=date, programme=schedule.programme) except Episode.DoesNotExist: episode = Episode.create_episode(date, schedule.programme) start_date = date + datetime.timedelta(seconds=PodcastConfiguration.objects.get().start_delay) duration = schedule.runtime().seconds - PodcastConfiguration.objects.get().start_delay - PodcastConfiguration.objects.get().end_delay # start = date.strftime("%Y-%m-%dT%H:%M:%S"+utc_str) json_entry = { 'id': schedule.programme.id, 'issue_date': date.strftime('%Y-%m-%d %H-%M-%S'), 'start': start_date.strftime('%Y-%m-%d %H-%M-%S'), 'duration': str(duration), 'genre': schedule.programme.get_category_display(), 'programme_name': schedule.programme.slug, 'title': episode.title, 'author': schedule.programme.name, 'album': _('Season') + ' ' + str(episode.season), 'track': episode.number_in_season } json_list.append(json_entry) return HttpResponse(json.dumps(json_list), content_type='application/json')
def test_day_schedule(self): schedules, dates = Schedule.between(datetime.datetime(2014, 1, 6), datetime.datetime(2014, 1, 7)) self.assertEqual(4, len(schedules)) schedule_1 = Schedule.objects.get( programme=Programme.objects.get(name="Programme 00:00 - 09:00"), day=MO) schedule_2 = Schedule.objects.get( programme=Programme.objects.get(name="Programme 09:00 - 10:00"), day=MO) schedule_3 = Schedule.objects.get( programme=Programme.objects.get(name="Programme 10:00 - 12:00"), day=MO) schedule_4 = Schedule.objects.get( programme=Programme.objects.get(name="Programme 00:00 - 09:00"), day=TU) self.assertTrue(schedule_1 in schedules) self.assertTrue(schedule_2 in schedules) self.assertTrue(schedule_3 in schedules) self.assertTrue(schedule_4 in schedules)
def __get_events(after, before, json_mode=False): background_colours = {"L": "#F9AD81", "B": "#C4DF9B", "S": "#8493CA"} text_colours = {"L": "black", "B": "black", "S": "black"} next_schedules, next_dates = Schedule.between(after=after, before=before) schedules = [] dates = [] episodes = [] event_list = [] for x in range(len(next_schedules)): for y in range(len(next_dates[x])): # next_events.append([next_schedules[x], next_dates[x][y]]) schedule = next_schedules[x] schedules.append(schedule) date = next_dates[x][y] dates.append(date) episode = None # if schedule == live if next_schedules[x].type == 'L': try: episode = Episode.objects.get(issue_date=date) except Episode.DoesNotExist: pass # broadcast elif next_schedules[x].source: try: source_date = next_schedules[x].source.date_before(date) if source_date: episode = Episode.objects.get(issue_date=source_date) except Episode.DoesNotExist: pass episodes.append(episode) if episode: url = reverse('programmes:episode_detail', args=( schedule.programme.slug, episode.season, episode.number_in_season, )) else: url = reverse('programmes:detail', args=(schedule.programme.slug, )) event_entry = { 'id': schedule.id, 'start': str(date), 'end': str(date + schedule.runtime()), 'allDay': False, 'title': schedule.programme.name, 'type': schedule.type, 'textColor': text_colours[schedule.type], 'backgroundColor': background_colours[schedule.type], 'url': url } event_list.append(event_entry) if json_mode: return event_list else: if schedules: dates, schedules, episodes = (list(t) for t in zip( *sorted(zip(dates, schedules, episodes)))) return zip(schedules, dates, episodes) return None