remove unnecessary imports
[mygpo.git] / mygpo / data / management / commands / merge-duplicates.py
blob7af6d7db267dc1e4ac9a306109f2165a2a77befe
1 from django.core.management.base import BaseCommand
2 from mygpo.api.models import Podcast, Episode
3 from mygpo.api.sanitizing import rewrite_podcasts, rewrite_episodes
5 class Command(BaseCommand):
7 def handle(self, *args, **options):
9 if len(args) == 0:
10 podcast_urls = []
11 for p in Podcast.objects.only('id', 'url'):
12 if Podcast.objects.filter(url__exact=p.url).exclude(id=p.id).exists():
13 print 'found podcast url %s' % p.url
14 podcast_urls.append(p.url)
16 episode_ids = []
17 for e in Episode.objects.only('id', 'url'):
18 if Episode.objects.filter(url__exact=e.url, podcast=e.podcast).exclude(id=e.id).exists():
19 print 'found episode url %s' % e.url
20 episode_ids.append(e.id)
22 else:
23 pocast_urls = args
25 for url in podcast_urls:
26 p = Podcast.objects.filter(url=url).order_by('id')[0]
27 for p2 in Podcast.objects.filter(url=url).exclude(id=p.id).order_by('id'):
28 print 'merging podcast %s with %s' % (p.id, p2.id)
29 rewrite_podcasts(p2, p)
30 p2.delete()
32 for e_id in episode_ids:
33 e = Episode.objects.get(id=e_id)
34 for e2 in Episode.objects.filter(url=e.url, podcast=e.podcast).exlcude(id=e.id).order_by('id'):
35 print 'merging episode %s with %s' % (e.id, e2.id)
36 rewrite_episodes(e2, e)
37 e2.delete()