Example #1
0
File: tests.py Project: fk-lx/mygpo
    def test_unify(self):

        from mygpo.core.models import Episode
        a = Episode(_id='a', slug='1')
        b = Episode(_id='b', merged_slugs=['1'])
        c = Episode(_id='c', merged_slugs=['1'])

        dups_list = list(get_duplicate_slugs([a, b, c]))

        # only one duplicate slug is reported
        self.assertEquals(len(dups_list), 1)

        slug, dups = dups_list[0]

        self.assertEquals(slug, '1')
        self.assertEquals(len(dups), 2)
        self.assertEquals(dups[0], b)
        self.assertEquals(dups[1], c)
        self.assertEquals(dups, [b, c])
Example #2
0
def unify_slugs(podcast):
    """ Removes duplicate slugs of a podcast's episodes """

    logger.warn("unifying slugs for podcast %s", podcast)
    episodes = episodes_for_podcast_uncached(podcast)
    logger.info("found %d episodes", len(episodes))

    common_title = podcast.get_common_episode_title()
    actions = Counter()

    # get episodes with duplicate slugs
    for slug, dups in get_duplicate_slugs(episodes):
        actions["dup-slugs"] += 1
        # and remove their slugs
        logger.info("Found %d duplicates for slug %s", len(dups), slug)
        for dup in dups:
            actions["dup-episodes"] += 1

            # check if we're removing the "main" slug
            if dup.slug == slug:

                # if possible, replace it with a "merged" slug
                if dup.merged_slugs:
                    dup.slug = dup.merged_slugs.pop()
                    actions["replaced-with-merged"] += 1
                    logger.info("Replacing slug with merged slug %s", dup.slug)

                # try to find a new slug
                else:
                    dup.slug = EpisodeSlug(dup, common_title, override_existing=True).get_slug()
                    actions["replaced-with-new"] += 1
                    logger.info("Replacing slug with new slug %s", dup.slug)

            # if the problematic slug is a merged one, remove it
            if slug in dup.merged_slugs:
                actions["removed-merged"] += 1
                logger.info("Removing merged slug %s", slug)
                dup.merged_slugs.remove(slug)

            dup.save()

    return actions, podcast