# Path: radioco/apps/schedules/utils.py
# def next_dates(calendar, programme, after):
#     """
#     Returns: A generator with the next dates of a given programme
#     """
#     if not calendar or not calendar.id:
#         return
# 
#     from radioco.apps.schedules.models import Schedule
#     # Only taking into account schedules which belong to the active calendar
#     schedules = Schedule.objects.filter(programme=programme, type='L', calendar=calendar)
# 
#     while True:
#         candidates = [s.date_after(after) for s in schedules]
#         try:
#             next_date = min([_dt for _dt in candidates if _dt is not None])
#         except ValueError:
#             break
# 
#         yield next_date
#         after = next_date + datetime.timedelta(seconds=1)
#         # schedules = filter(lambda t: t[1] is not None, zip(schedules, candidates))
#         schedules = [_tuple[0] for _tuple in zip(schedules, candidates) if _tuple[1] is not None]


import pytz
import datetime
from bs4 import BeautifulSoup
from ckeditor_uploader.fields import RichTextUploadingField
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import FieldError
from django.core.urlresolvers import reverse
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save, pre_save
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from radioco.apps.radioco.utils import field_has_changed
from radioco.apps.schedules.utils import next_dates
            from radioco.apps.global_settings.models import PodcastConfiguration
        return datetime.timedelta(minutes=self._runtime)

    @runtime.setter
    def runtime(self, value):
        self._runtime = value

    @property
    def start_dt(self):
        if not self.start_date:
            return None
        tz = timezone.get_default_timezone()
        return tz.localize(datetime.datetime.combine(self.start_date, datetime.time())).astimezone(pytz.utc)

    @property
    def end_dt(self):
        if not self.end_date:
            return None
        tz = timezone.get_default_timezone()
        return tz.localize(datetime.datetime.combine(self.end_date, datetime.time(23, 59, 59))).astimezone(pytz.utc)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        super(Programme, self).save(*args, **kwargs)

    def rearrange_episodes(self, after, calendar):
        """
        Update the issue_date of episodes from a given date
        """
        episodes = Episode.objects.unfinished(self, after)

# Path: radioco/apps/schedules/utils.py
# def next_dates(calendar, programme, after):
#     """
#     Returns: A generator with the next dates of a given programme
#     """
#     if not calendar or not calendar.id:
#         return
# 
#     from radioco.apps.schedules.models import Schedule
#     # Only taking into account schedules which belong to the active calendar
#     schedules = Schedule.objects.filter(programme=programme, type='L', calendar=calendar)
# 
#     while True:
#         candidates = [s.date_after(after) for s in schedules]
#         try:
#             next_date = min([_dt for _dt in candidates if _dt is not None])
#         except ValueError:
#             break
# 
#         yield next_date
#         after = next_date + datetime.timedelta(seconds=1)
#         # schedules = filter(lambda t: t[1] is not None, zip(schedules, candidates))
#         schedules = [_tuple[0] for _tuple in zip(schedules, candidates) if _tuple[1] is not None]


# Path: radioco/apps/programmes/models.py
import pytz
import datetime
from bs4 import BeautifulSoup
from ckeditor_uploader.fields import RichTextUploadingField
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import FieldError
from django.core.urlresolvers import reverse
from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save, pre_save
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from radioco.apps.radioco.utils import field_has_changed
from radioco.apps.schedules.utils import next_dates
            from radioco.apps.global_settings.models import PodcastConfiguration

        return datetime.timedelta(minutes=self._runtime)

    @runtime.setter
    def runtime(self, value):
        self._runtime = value

    @property
    def start_dt(self):
        if not self.start_date:
            return None
        tz = timezone.get_default_timezone()
        return tz.localize(datetime.datetime.combine(self.start_date, datetime.time())).astimezone(pytz.utc)

    @property
    def end_dt(self):
        if not self.end_date:
            return None
        tz = timezone.get_default_timezone()
        return tz.localize(datetime.datetime.combine(self.end_date, datetime.time(23, 59, 59))).astimezone(pytz.utc)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        super(Programme, self).save(*args, **kwargs)

    def rearrange_episodes(self, after, calendar):
        """
        Update the issue_date of episodes from a given date
        """
        episodes = Episode.objects.unfinished(self, after)

        dates = next_dates(calendar, self, after)