(self, podcast, url, defaults={})
Create an Episode for a given URL

        This is the only place where new episodes are created
Create an Episode for a given URL
Create an Episode for a given URL
def get_or_create_for_url(self, podcast, url, defaults={}):
        """Create an Episode for a given URL

        This is the only place where new episodes are created"""

        if not url:
            raise ValueError("The URL must not be empty")

        # TODO: where to specify how uuid is created?
        import uuid

        url = utils.to_maxlength(URL, "url", url)

        try:
            url = URL.objects.get(url=url, scope=podcast.as_scope)
            created = False
            episode = url.content_object

            if episode is None:

                with transaction.atomic():
                    episode = Episode.objects.create(
                        podcast=podcast, id=uuid.uuid1(), **defaults
                    )

                    url.content_object = episode
                    url.save()
                    created = True

            return GetCreateResult(episode, created)

        except URL.DoesNotExist:
            # episode did not exist, try to create it
            try:
                with transaction.atomic():
                    episode = Episode.objects.create(
                        podcast=podcast, id=uuid.uuid1(), **defaults
                    )

                    url = URL.objects.create(
                        url=url, order=0, scope=episode.scope, content_object=episode
                    )

                    # Keep episode_count up to date here; it is not
                    # recalculated when updating the podcast because counting
                    # episodes can be very slow for podcasts with many episodes
                    Podcast.objects.filter(pk=podcast.pk).update(
                        episode_count=F("episode_count") + 1
                    )

                    return GetCreateResult(episode, True)

            # URL could not be created, so it was created since the first get
            except IntegrityError:
                episode = Episode.objects.get(
                    urls__url=url, urls__scope=podcast.as_scope
                )
                return GetCreateResult(episode, False)
def get_or_create_for_url ( self , podcast , url , defaults = { } ) : if not url : raise ValueError ( "The URL must not be empty" ) # TODO: where to specify how uuid is created? import uuid url = utils . to_maxlength ( URL , "url" , url ) try : url = URL . objects . get ( url = url , scope = podcast . as_scope ) created = False episode = url . content_object if episode is None : with transaction . atomic ( ) : episode = Episode . objects . create ( podcast = podcast , id = uuid . uuid1 ( ) , * * defaults ) url . content_object = episode url . save ( ) created = True return GetCreateResult ( episode , created ) except URL . DoesNotExist : # episode did not exist, try to create it try : with transaction . atomic ( ) : episode = Episode . objects . create ( podcast = podcast , id = uuid . uuid1 ( ) , * * defaults ) url = URL . objects . create ( url = url , order = 0 , scope = episode . scope , content_object = episode ) # Keep episode_count up to date here; it is not # recalculated when updating the podcast because counting # episodes can be very slow for podcasts with many episodes Podcast . objects . filter ( pk = podcast . pk ) . update ( episode_count = F ( "episode_count" ) + 1 ) return GetCreateResult ( episode , True ) # URL could not be created, so it was created since the first get except IntegrityError : episode = Episode . objects . get ( urls__url = url , urls__scope = podcast . as_scope ) return GetCreateResult ( episode , False )