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
):
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
)
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)
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
)
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
)