From 67dab09a93e4d1857ecd680b3143da34f7d26f4b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Thu, 18 Jul 2013 18:29:21 +0200 Subject: [PATCH] add admin task for unifying the slugs of episodes --- mygpo/admin/tasks.py | 48 +++++++++++++++++++ mygpo/admin/templates/admin/overview.html | 1 + .../admin/{merge-status.html => task-status.html} | 10 ++-- .../admin/templates/admin/unify-slugs-select.html | 30 ++++++++++++ mygpo/admin/urls.py | 15 +++++- mygpo/admin/views.py | 55 ++++++++++++++++++++-- mygpo/core/slugs.py | 49 +++++++++++++++++-- mygpo/core/tests.py | 27 ++++++++++- mygpo/utils.py | 7 +++ 9 files changed, 229 insertions(+), 13 deletions(-) rename mygpo/admin/templates/admin/{merge-status.html => task-status.html} (81%) create mode 100644 mygpo/admin/templates/admin/unify-slugs-select.html diff --git a/mygpo/admin/tasks.py b/mygpo/admin/tasks.py index 0e998768..e84f4c9e 100644 --- a/mygpo/admin/tasks.py +++ b/mygpo/admin/tasks.py @@ -1,8 +1,10 @@ from collections import Counter from mygpo.cel import celery +from mygpo.core.slugs import get_duplicate_slugs, EpisodeSlug from mygpo.maintenance.merge import PodcastMerger from mygpo.db.couchdb.podcast import podcasts_by_id +from mygpo.db.couchdb.episode import episodes_for_podcast_uncached from celery.utils.log import get_task_logger logger = get_task_logger(__name__) @@ -26,3 +28,49 @@ def merge_podcasts(podcast_ids, num_groups): logger.info('merging result: %s', actions) return actions, podcast + + +@celery.task +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 diff --git a/mygpo/admin/templates/admin/overview.html b/mygpo/admin/templates/admin/overview.html index 0ee08c66..25742578 100644 --- a/mygpo/admin/templates/admin/overview.html +++ b/mygpo/admin/templates/admin/overview.html @@ -16,6 +16,7 @@